$subject = get_the_title();
$sender_name = get_bloginfo('name');
$blog_url = get_bloginfo('url');
$to = 'myemail@mydomainname.com';
$subject="the subject";
$message="hello";
$headers="MIME-Version: 1.0" . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$sender_name.' <no-reply@'.$blog_url.'>' . "\r\n";
$headersssssssssssss="From: webmaster@example.com" . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
When I use the $headersssssssssssss
variable as a parameter in the mail()
function, it works and sends an E-Mail.
But when I use $headers
as a parameter, it does not.
Note: I have already tried using wp_mail
instead, with the same results.
if( mail($to, $subject, $message, $headersssssssssssss) )
{
echo '<script>alert("mail sent success!");</script>';
} else {
echo '<script>alert("mail where not sent");</script>';
}
exit;
1 Answer
There’s the wp_mail()
function in WordPress. The headers have to be added as array without trailing \n\r
or similar.
Example
wp_mail(
'test@example.com',
'Hello World!',
'Just saying...',
array(
'MIME-Version: 1.0',
'Content-type: text/html; charset=iso-8859-1',
sprintf(
'From: %s <no-reply@%s>',
get_bloginfo('name'),
site_url()
),
sprintf( 'X-Mailer: PHP/%s', phpversion() ),
)
);
To change the content type you could as well use a filter:
<?php
/* Plugin Name: WP Mail Content Type text/html */
function wpse_97789_mail_contenttype( $content_type )
{
remove_filter( current_filter(), __FUNCTION__ );
return 'text/html';
}
// Then, whereever you need it, just add the filter before calling the function
// It removes itself after firing once
add_filter( 'wp_mail_content_type', 'wpse_97789_mail_contenttype' );
wp_mail(
'test@example.com',
'Hello World!',
'Just saying...',
array(
'MIME-Version: 1.0',
sprintf(
'From: %s <no-reply@%s>',
get_bloginfo('name'),
site_url()
),
sprintf( 'X-Mailer: PHP/%s', phpversion() ),
)
);