user892134
user892134

Reputation: 3224

wp_mail not sending when adding headers

Emails are being sent successfully but I want to send email to include a footer html template, so i've added the following lines:

$headers .= 'MIME-Version: 1.0' . "\r\n";    
$headers .= "Content-type: text/html; charset=UTF-8 \r\n"; 

Now emails aren't being sent. How do i solve this?

$emails = array("[email protected]");
        $emails = array_unique($emails);
        $subject = $_POST['subject'];
        $message = $_POST['message'];

        $message.="<div class='footer'>
        <ul style='padding: 0;margin: 0;'>
            <li><img style='width:300px' src='https://www.test.co.uk/wp-content/themes/test/images/structural/head-logo.jpg' /></li>
            <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>Tel:</span> +44 203 051 1214</li>
            <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>Fax:</span> +44 207 657 3322</li>
            <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>E-mail:</span> [email protected] </li>
            <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>Web:</span> www.test.co.uk</li>
        </ul>
    </div>";
                $headers = 'From: test <[email protected]>' . "\r\n"; 
                $headers .= 'MIME-Version: 1.0' . "\r\n";    
                $headers .= "Content-type: text/html; charset=UTF-8 \r\n";

                $success = wp_mail( $emails, $subject, $message, $headers );  //send emails

Upvotes: 0

Views: 1752

Answers (1)

christian Nguyen
christian Nguyen

Reputation: 1600

Firstly, I has seen this code. There is no thing wrong with that header excerpt you don't need this line:

$headers .= 'MIME-Version: 1.0' . "\r\n";

wp_mail will automatic added it for you.

Secondly, Try this code:

    $emails = "[email protected]";
    //$emails .= ', ' . '[email protected]';

    $subject = !empty($_POST['subject']) ? sanitize_text_field($_POST['subject']) : 'test subject';
    $message = !empty($_POST['message']) ? $_POST['message'] : '';

    $message.="<div class='footer'>
            <ul style='padding: 0;margin: 0;'>
                <li><img style='width:300px' src='https://www.test.co.uk/wp-content/themes/test/images/structural/head-logo.jpg' /></li>
                <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>Tel:</span> +44 203 051 1214</li>
                <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>Fax:</span> +44 207 657 3322</li>
                <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>E-mail:</span> [email protected] </li>
                <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>Web:</span> www.test.co.uk</li>
            </ul>
        </div>";

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
    $headers .= 'From: test <[email protected]>' . "\r\n";

    $success = mail($emails, $subject, $message, $headers);

I think there is some mixed here.

If still not work, maybe you should install this plugin here with your gmail information and try your code again. Hope that help!

Upvotes: 1

Related Questions