I am writing a custom plugin that sends an email to users from admin/front end section. I am using wp_email() to send emails and emails are being sent fine. I am testing this on a plain WP installation with only my plugin installed and hostgator as my hosting server. Whenever the email is sent, the email is sent from xyz@gator39.hostgator.com and not from the email address of WP admin account. I have also tried setting custom hooks as mentioned here and setting custom headers, but none of them is working. I am not sure what mistake I am doing. Can you please guide me on solving this issue. Code provided below
EDIT
I have also tried testing it from another dedicated windows server and getting same error.
// new name
function smartsms_mail_from_name() {
return "WebMaster";
//$name = get_option('blogname');
//$name = esc_attr($name);
//return $name;
}
// new email-adress
function smartsms_mail_from() {
return "webmaster@example.com";
//$email = get_option('admin_email');
//$email = is_email($email);
//return $email;
}
add_filter( 'wp_mail_from', 'smartsms_mail_from' );
add_filter( 'wp_mail_from_name', 'smartsms_mail_from_name' );
//$headers = 'From: '. get_option('blogname') .' <' . get_option('admin_email') . '>';
$headers = 'From: webmaster@example.com' . "rn" .'Reply-To: webmaster@example.com' . "rn" . 'X-Mailer: PHP/' . phpversion();
$mail = wp_mail($to, $subject, $message, $headers);
After Brady’s answer below, edited the code as below..but no email was
sent. However, I get the message as “Message was sent successfully” 🙁
if($to!="")
{
//$headers = 'From: '. get_option('blogname') .' <' . get_option('admin_email') . '>';
//$headers = 'From: webmaster@example.com' . "rn" .'Reply-To: webmaster@example.com' . "rn" . 'X-Mailer: PHP/' . phpversion();
add_filter('wp_mail_from', 'smartsms_mail_from'); // add filter to modify the mail from
add_filter('wp_mail_from_name', 'smartsms_mail_from_name'); // add filter to modify the mail from name
add_filter('wp_mail_content_type', 'smartsms_wp_mail_content_type'); // add filter to modify the mail content type
$mail = wp_mail($to, $subject, $message); // send mail
remove_filter('wp_mail_from', 'smartsms_mail_from'); // remove applied filter
remove_filter('wp_mail_from_name', 'smartsms_mail_from_name'); // remove applied filter
remove_filter('wp_mail_content_type', 'smartsms_wp_mail_content_type'); // remove applied filter
//$mail = wp_mail($to, $subject, $message, $headers);
if($mail)
{
echo 'Your message has been sent!';
}
else echo 'There was a problem sending your message. Please try again.';
}
// new name
function smartsms_mail_from_name() {
return "WebMaster";
//$name = get_option('blogname');
//$name = esc_attr($name);
//return $name;
}
// new email-adress
function smartsms_mail_from() {
return "webmaster@example.com";
//$email = get_option('admin_email');
//$email = is_email($email);
//return $email;
}
function smartsms_wp_mail_content_type() { return "text/html"; }
The correct way is to apply a filter to wp_mail_from and wp_mail_from_name. You would think that using the $headers would work and normally it would but there are many plugins that filter and then don’t take off their filter when they have sent their email which then leaves those details on the email for the next send. Below is a snippet of my plugin that uses these filters. Adjust code to your liking. Take note how I remove the filters once I’ve sent the mail via
wp_mail()
: