0

I have set up SMTL like this,

add_action( "phpmailer_init", "send_smtp_email" );
function send_smtp_email( $phpmailer ) {
ini_set("sendmail_from","[email protected]");
ini_set("sendmail_path","[email protected]");
    // Define that we are sending with SMTP
    $phpmailer->isSMTP();
    // The hostname of the mail server
    $phpmailer->Host = "localhost";
    // Use SMTP authentication (true|false)
    $phpmailer->SMTPAuth = false;
    // SMTP port number - likely to be 25, 465 or 587
    $phpmailer->Port = "25";
    // Encryption system to use - ssl or tls
    $phpmailer->SMTPSecure = "tls";

    $phpmailer->From = "[email protected]";
    $phpmailer->FromName = "XYZ";
    }

and added below code for my contact form to send email.

$headers = array('MIME-Version: 1.0'.'From: '.$_POST['namee'].'<'.$_POST['email'].'>'.'Reply-To: '.$_POST['email']);
$comment = $comment . "\r\n" . "\r\nIP Address: " . $id . "\r\nUser Agent: " . $browser . "\r\nReferrer: " . $referrer;
wp_mail($to,$website,$comment,$headers);

But, It is neither setting the headers properly nor does it send the email with the From address as the one taken from the contact form.

I'm getting emails but the From address in those emails is that of the host server.

I want that the email address taken from the contact form should get set as the From address in the email.

I know that there are hooks to replace the admin email and name, but, that replaces the site admin email address and name and become static. I want that the email address user enters in contact form should be the From address in the email being sent.

How can I do this? Does anybody know about it?

3
  • Are you sure you should be using $_POST['namee'] and not $_POST['name']?
    – TomC
    May 26, 2016 at 15:33
  • 1
    WP reserves $_POST['name'] and setting it causes trouble. May 26, 2016 at 16:49
  • As Andy said $_Post['name'] is reserved by WP and hence I used namee May 27, 2016 at 0:22

1 Answer 1

0

You don't need to pass the from name and email to PHPMailer in your code as it will take it from $headers so try the following:

add_action( "phpmailer_init", "send_smtp_email" );
function send_smtp_email( $phpmailer ) {
// ini_set("sendmail_from","[email protected]");
// ini_set("sendmail_path","[email protected]");
    // Define that we are sending with SMTP
    $phpmailer->isSMTP();
    // The hostname of the mail server
    $phpmailer->Host = "localhost";
    // Use SMTP authentication (true|false)
    $phpmailer->SMTPAuth = false;
    // SMTP port number - likely to be 25, 465 or 587
    $phpmailer->Port = "25";
    // Encryption system to use - ssl or tls
    $phpmailer->SMTPSecure = "tls";

    // $phpmailer->From = "[email protected]";
    // $phpmailer->FromName = "XYZ";
    }

Also, double check the form field name for: $_POST['namee']

9
  • I tried what you are suggesting, but in that case I am not receiving any emails. May 27, 2016 at 0:16
  • I checked again and now without $phpmailer->From = "[email protected]"; /and $phpmailer->FromName = "XYZ"; I'm not receiving any emails. Is there any way I can pass form input dynamically to these 2 options? May 27, 2016 at 1:22
  • Hmm. Try this as your $headers: $headers = 'From: ' . $_POST['namee'] . '\r\n'; $headers .= 'Reply-To: ' . $_POST['email']) . '\r\n'; $headers .= 'MIME-Version: 1.0' . "\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; or try with plain text instead of HTML mail.
    – TomC
    May 27, 2016 at 6:51
  • Nope... That gives the same result! May 28, 2016 at 9:58
  • What are your email headers from the email you receive?
    – TomC
    May 28, 2016 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.