2
$subject = get_the_title(); 
$sender_name = get_bloginfo('name');
$blog_url = get_bloginfo('url');

$to      = '[email protected]';
$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: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\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;
2

1 Answer 1

4

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(
    '[email protected]',
    '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(
    '[email protected]',
    '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() ),
    )
);
6
  • still else working here in code see image-result Apr 30, 2013 at 16:37
  • @RameezSOOMRO Please make sure your server is set up properly.
    – kaiser
    Apr 30, 2013 at 18:28
  • mail were sent if i dont' add html headers, but how i can check server setup properly because i am using shared hosting... May 2, 2013 at 11:45
  • Sorry, but this question is outside the scope of this site.
    – kaiser
    May 2, 2013 at 14:51
  • can you please send me url of related solution, but I am also using contact form 7 plugin its work fine in same site with html headers... May 2, 2013 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.