PHP Mail Headers does not work?

1.6k views Asked by At

Whenever I have the headers added to mail(), the recipient does not receive any email.

This works ok:

// The message
$message = "Line 1\r\nLine 2\r\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");

// Send
mail('[email protected]', 'My Subject 2', $message);

This is not ok:

// The message
$message = "Line 1\r\nLine 2\r\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");

// Send with headers
mail('[email protected]', 'My Subject', $message, 'From: Test <[email protected]>'); 

Any ideas why?

EDIT:

It seems that it is caused by the @yahoo.co.uk email addresses. It is ok with @gmail.com!

Why!??? Is it something to with my production server??

EDIT 2:

Same thing happens even though I use PHPMailer:

// Include Composer autoloader.
require_once __DIR__ . '/vendor/autoload.php';

$mail = new PHPMailer;

$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
$mail->addAddress('[email protected]');               // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

But it works ok with @gmail.com:

$mail->setFrom('[email protected]', 'Mailer');
1

There are 1 answers

2
cnizzardini On

More specifically to answer your question, see http://php.net/manual/en/function.mail.php Example 2 titled "Example #2 Sending mail with extra headers."

You need to add return/new lines for valid headers.