2

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('contact@xxx.net', '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('contact@xxx.net', 'My Subject', $message, 'From: Test <test123@yahoo.co.uk>'); 

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('test123@yahoo.co.uk', 'Mailer');
$mail->addAddress('contact@xxx.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$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('test123@gmail.com', 'Mailer');
Run
  • 54,938
  • 169
  • 450
  • 748
  • I know this is not an answer to your exact question, but have you considered using an existing library such as PHPMailer which takes care of this for you? – cnizzardini Dec 14 '16 at 18:42
  • https://github.com/PHPMailer/PHPMailer – cnizzardini Dec 14 '16 at 18:44
  • Are you getting errors from PHP or within your OS logs such as syslog? That may tell you why. – cnizzardini Dec 14 '16 at 18:55
  • I don't get any error from PHP. I can't see any error logs on my server - it is a shared hosting. – Run Dec 14 '16 at 19:00
  • Are the message marked as spam in yahoo? The problem can be the diff in domain from smtp-sender to from-header. – rogeriolino Dec 14 '16 at 19:05
  • @rogeriolino `Are the message marked as spam in yahoo?` nope. i haven't set any spam filter in my email account in my server. – Run Dec 14 '16 at 20:04
  • Try to configure the smtp transport in PHPMailer, using the yahoo credential. – rogeriolino Dec 14 '16 at 20:52
  • @rogeriolino i don't understand the smtp part. why using the yahoo credential? – Run Dec 15 '16 at 05:01
  • 1
    By default PHPMailer use `mail` function. The test is setup yahoo smtp (https://github.com/PHPMailer/PHPMailer) to check if your host server (default PHP mail configuration) is blocked by yahoo or not. – rogeriolino Dec 15 '16 at 15:00

1 Answers1

3

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.

cnizzardini
  • 1,196
  • 1
  • 13
  • 29
  • "Multiple extra headers should be separated with a CRLF". You shouldn't need to with a single header line. – Devon Bessemer Dec 14 '16 at 18:47
  • I have tried with that example #2. It is still the same. I found a strange reason that causes the problem - see my edit above. – Run Dec 14 '16 at 18:55