Jump to content

Recommended Posts

The html Form I'm using works successfully with this php code:

 

<?php
//check if form was sent
if($_POST){
	$to = 's@hmail.com';
	$subject = 'Form1';

	$name = $_POST['name'];
	$email = $_POST['email'];
	$message = $_POST['message'];
	$headers = $name;
	$message .= "\r\n\r\n" . $name;

if( empty($_POST["some_place"]) or $_POST['some_place'] != "glory" )
{
   header("HTTP/1.0 403 Forbidden");
   	}else{
   		mail( $to, $subject, $message, $email, $headers );
   	}
		header('Location: https://.......com');
   exit;
}
?>

The problem is that when the email is received it shows the (from) email address to be my domain account user name @ the server name, like this:
domain1@host3servername.com, where I’d prefer something more like noReply@actualdomain.com

Any help or suggested remedy will be appreciated

Thanks for your reply.

I have PHPMailer installed. Maybe you can help direct me to tie-in this example code to my Form and to the PHPMailer:

<?php
/**
 * PHPMailer simple file upload and send example.
 */
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
$msg = '';
if (array_key_exists('userfile', $_FILES)) {
    // First handle the upload
    // Don't trust provided filename - same goes for MIME types
    // See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
    $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name']));
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        // Upload handled successfully
        // Now create a message
        require '../vendor/autoload.php';
        $mail = new PHPMailer;
        $mail->setFrom('from@example.com', 'First Last');
        $mail->addAddress('whoto@example.com', 'John Doe');
        $mail->Subject = 'PHPMailer file sender';
        $mail->Body = 'My message body';
        // Attach the uploaded file
        $mail->addAttachment($uploadfile, 'My uploaded file');
        if (!$mail->send()) {
            $msg .= "Mailer Error: " . $mail->ErrorInfo;
        } else {
            $msg .= "Message sent!";
        }
    } else {
        $msg .= 'Failed to move file to ' . $uploadfile;
    }
}
?>

And my Form looks like this:

<form action='/submit/submit.php' method='post' name='myform'>
<input type="hidden" id="some-place" name="some_place" value="classified">
<div class="row">
<div class="col-sm-14">
<textarea name='message' placeholder="Message..." class="form-control" rows="9" ></textarea>
</div>
</div>
<div class="row">
<input class="form-control" type="text" name="name" placeholder="Name ">
<input class="form-control" type="email" name="email"  placeholder="Email" required>
</div>
<div class="row">
<input class="btnbtn-action" type='submit' value="Send" onclick="return UpdateTheHiddenField()" >
<br/><br/>
</div>
</div>
</div>
</form>

Any additional guidance is appreciated

Edited by Chrisj

It seems to be pretty straight forward. You replace your mail code with this and use the same $_POST values to set the respective values as shown in the example. The only difference is you set the from address to whatever you want. That being said your ISP may munge it as it passes through the mail server. Nothing you can do about that.

Edited by gw1500se

Thanks for your reply. It's not so straight forward to me. Here's what I tried, without success:

 

<?php
use PHPMailer\PHPMailer\PHPMailer;
$msg = '';
if (array_key_exists('userfile', $_FILES)) {

    $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name']));
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        // Upload handled successfully
        // Now create a message
        require '../vendor/autoload.php';
        $mail = new PHPMailer;
        $mail->setFrom('from@example.com', 'First Last');
        $mail->addAddress('s@hmail.com', 'John Doe');
        $mail->Subject = 'PHPMailer file sender';
        $mail->message = 'My message body';
        // Attach the uploaded file
        $mail->addAttachment($uploadfile, 'My uploaded file');
        if (!$mail->send()) {
            $msg .= "Mailer Error: " . $mail->ErrorInfo;
        } else {
            $msg .= "Message sent!";
        }
    } else {
        $msg .= 'Failed to move file to ' . $uploadfile;
    }
    		header('Location: https://........com');
	   exit;
}

?>

any additional guidance will be appreciated

Thanks for your reply.

I'm now trying the code below, but am getting this error:

Warning: require(../assets/import/PHPMailer/PHPMailerAutoLoad.php): failed to open stream: No such file or directory in /home/public_html/submit/submit.php on line 11

Warning: require(../assets/import/PHPMailer/PHPMailerAutoLoad.php): failed to open stream: No such file or directory in /home/public_html/submit/submit.php on line 11

Fatal error: require(): Failed opening required '../assets/import/PHPMailer/PHPMailerAutoLoad.php' (include_path='.:/opt/cpanel/ea-php70/root/usr/share/pear') in /home/public_html/submit/submit.php on line 11

<?php
/**
 * This example shows how to handle a simple contact form.
 */
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
$msg = '';
//Don't run this unless we're handling a form submission
if (array_key_exists('email', $_POST)) {
    date_default_timezone_set('Etc/UTC');
    require '../assets/import/PHPMailer/PHPMailerAutoLoad.php';
    //Create a new PHPMailer instance
    $mail = new PHPMailer;
    //Tell PHPMailer to use SMTP - requires a local mail server
    //Faster and safer than using mail()
    $mail->isSMTP();
    $mail->Host = 'localhost';
    $mail->Port = 25;
    //Use a fixed address in your own domain as the from address
    //**DO NOT** use the submitter's address here as it will be forgery
    //and will cause your messages to fail SPF checks
    $mail->setFrom('chrisj@hmail.com', 'First Last');
    //Send the message to yourself, or whoever should receive contact for submissions
    $mail->addAddress('chrisj@hmail.com', 'John Doe');
    //Put the submitter's address in a reply-to header
    //This will fail if the address provided is invalid,
    //in which case we should ignore the whole request
    if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
        $mail->Subject = 'PHPMailer contact form';
        //Keep it simple - don't use HTML
        $mail->isHTML(false);
        //Build a simple message body
        $mail->Body = <<<EOT
Email: {$_POST['email']}
Name: {$_POST['name']}
Message: {$_POST['message']}
EOT;
        //Send the message, check for errors
        if (!$mail->send()) {
            //The reason for failing to send will be in $mail->ErrorInfo
            //but you shouldn't display errors to users - process the error, log it on your server.
            $msg = 'Sorry, something went wrong. Please try again later.';
        } else {
            $msg = 'Message sent! Thanks for contacting us.';
        }
    } else {
        $msg = 'Invalid email address, message ignored.';
    }
}
?>

Any additional guidance is appreciated.

Yes, thanks, got it.

Now I see this:

Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer\PHPMailer' not found in /home/public_html/submit/submit.php:13 Stack trace: #0 {main} thrown in /home/public_html/submit/submit.php on line 13

any additional help is appreciated

So what happened to using the info I posted on how to get the right paths?

Sorry - wrong topic.   Thought I showed you how to use a DOCUMENT_ROOT to base your relative paths off of.

Edited by ginerjm

Thanks for the replies.

 

In tried this:

use assets\import\PHPMailer;

and I see this error: 
Fatal error: Uncaught Error: Class 'assets\import\PHPMailer' not found in /home/public_html/submit/submit.php:18 Stack trace: #0 {main} thrown in ...

 

and I tried this:

use assets/import/PHPMailer;

and I see this error:  Parse error: syntax error, unexpected '/', expecting ',' or ';' in

 

any additional help will be welcomed.

On 3/10/2019 at 4:54 PM, Chrisj said:

use PHPMailer\PHPMailer\PHPMailer;

This is for PHPMailer 6.

On 3/10/2019 at 4:54 PM, Chrisj said:

require '../assets/import/PHPMailer/PHPMailerAutoLoad.php';

This is for PHPMailer 5.

 

Which version do you actually have?

On 3/12/2019 at 6:13 AM, gw1500se said:

I know it was a namespace statement but it had nothing to do with the original problem. It still looks like a path issue for the PHPMailer code.

You need to load the autoloader before you do anything else.  It's not optional.  The autoloader figures out where your component libraries are and resolves them for you.  It should be the first thing you do in your script.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.