1

I Have this simple following code :

<?php
//if "email" variable is filled out, send email
if (isset($_POST['email']))  {
    //Email information
    $to = get_option( 'admin_email' );
    $headers = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    //send email
    wp_mail($to, $subject, $message, $headers);

    //Email response
    echo "Thank you for contacting us!";
}

    //if "email" variable is not filled out, display the form
    else  {
?>
<form method="post">
    Email: <input name="email" type="text" /><br />
    Subject: <input name="subject" type="text" /><br />
    Message:<br />
    <textarea name="message" rows="15" cols="40"></textarea><br />
    <input type="submit" value="Submit" />
 </form>

<?php
  }
?>

My question is how to retrieve the sender email instead of the generated wp_mail() from $header wordpress@$sitename ?

Thanks in advance for any help.

  • You get the email from $_POST['email']. Did you want this to show up as the "From" email address? – czerspalace Feb 3 '17 at 20:59
  • @czerspalace I don't get the $_POST['email'] and this is driving me crazy... I'm getting the generated wp_mail() from header, like so : From : WordPress : wordpress@mysiteurl.domainname I want to get the sender email as expected... – LebCit Feb 3 '17 at 21:02
  • You are not setting the From address anywhere.. Review this question to see how to set wordpress.stackexchange.com/questions/160017/… – czerspalace Feb 3 '17 at 21:31
  • Thanks for the link, I've seen it before but still unable to make it work. – LebCit Feb 3 '17 at 22:42
  • Did you change the $headers to match what is in the link? If so, please edit question and post your updated code – czerspalace Feb 4 '17 at 0:28
0

You are not setting the from address.

You can do the following:

<?php
//if "email" variable is filled out, send email
if (isset($_POST['email']))  {
    //Email information
    $to = get_option( 'admin_email' );
    $headers = array('From:'.$_POST['email']);
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    //send email
    wp_mail($to, $subject, $message, $headers);

    //Email response
    echo "Thank you for contacting us!";
}

    //if "email" variable is not filled out, display the form
    else  {
?>
<form method="post">
    Email: <input name="email" type="text" /><br />
    Subject: <input name="subject" type="text" /><br />
    Message:<br />
    <textarea name="message" rows="15" cols="40"></textarea><br />
    <input type="submit" value="Submit" />
 </form>

As you can see, the $headers uses an array that contains the text 'From:' to set the from header.

Hope that helps.

  • Hello @Steve North, thanks a lot for your help, but I have already try it and it's not working. The message is not sent at all... WEIRD... – LebCit Feb 4 '17 at 0:56
  • 1
    Sorry, got it a little wrong.. It should be: $headers = array('From: Me Myself <'.$_POST['email'].'>'); See here: developer.wordpress.org/reference/functions/wp_mail – Steve North Feb 4 '17 at 1:01
  • I've just tried your suggestion, not sending the message at all... I've memorized the wp_mail() functions and looked at the core so many times... Thanks again for your help – LebCit Feb 4 '17 at 1:12
  • 1
    Try var_dump(wp_mail()) and see what gets returned. – Steve North Feb 4 '17 at 1:32
  • 1
    Your sending is returning false. Does it return true without $headers? If so, your host may only allow to headers. See: wordpress.stackexchange.com/questions/216482/… – Steve North Feb 4 '17 at 1:42
0

I've learned an expensive (time) lesson.
The next time I'll want to set a contact form, I'll first check the host servers specific configuration regarding Email settings !
Thanks to Steve North, I understood that my host doesn't allow direct custom $headers for security reason...
But, I think that, every decent host has solutions for their customers.
So, if your are on Name Cheap (that's my case), you can refer to this article How to configure Contact Form hosted with us

Hope that this will help others. SYA :)

  • And chance you could add this as an edit to your original answer and mark mine as correct? – Steve North Oct 27 '17 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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