最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to retrieve the sender email with wp_mail()?

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Feb 4, 2017 at 13:43 LebCit asked Feb 3, 2017 at 20:55 LebCitLebCit 2481 silver badge11 bronze badges 5
  • You get the email from $_POST['email']. Did you want this to show up as the "From" email address? – czerspalace Commented Feb 3, 2017 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 : [email protected] I want to get the sender email as expected... – LebCit Commented Feb 3, 2017 at 21:02
  • You are not setting the From address anywhere.. Review this question to see how to set wordpress.stackexchange/questions/160017/… – czerspalace Commented Feb 3, 2017 at 21:31
  • Thanks for the link, I've seen it before but still unable to make it work. – LebCit Commented Feb 3, 2017 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 Commented Feb 4, 2017 at 0:28
Add a comment  | 

2 Answers 2

Reset to default 1

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 :)

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: Me Myself <'.$_POST['email'].'>'); //updated
    $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.

发布评论

评论列表(0)

  1. 暂无评论