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 [email protected] 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 "[email protected]";
//$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: [email protected]' . "\r\n" .'Reply-To: [email protected]' . "\r\n" . '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: [email protected]' . "rn" .'Reply-To: [email protected]' . "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 "[email protected]";
//$email = get_option('admin_email');
//$email = is_email($email);
//return $email;
}
function smartsms_wp_mail_content_type() { return "text/html"; }