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 |2 Answers
Reset to default 1I'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.
$_POST['email']
. Did you want this to show up as the "From" email address? – czerspalace Commented Feb 3, 2017 at 20:59$_POST['email']
and this is driving me crazy... I'm getting the generatedwp_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$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