2

I am attempting to send an email using the mail() PHP function. I had it working until I attempted to give it a subject of "User registration", then the mail is not sent!

Heres the code (have simplified it greatly)

$to = $this->post_data['register-email'];
$message = 'Hello etc';
$headers = 'From: noreply@example.com' . "\r\n" ;
$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';
$headers .= 'From: Website <admin@example.com>';
mail($to, 'User Registration', $message, $headers);

I also attempted to use a variable containing the string of text but that didnt work.

Why is it not sending the mail when I add the subject exception?

Thanks

EDIT: updated code thats still not working

$to = $this->post_data['register-email'];
$message = 'Hello etc';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: Website <admin@example.com>';
mail($to, 'User Registration', $message, $headers);
crmepham
  • 4,676
  • 19
  • 80
  • 155

3 Answers3

9

On your 4th line you're using ' that handles everything inside of it as a string so change

$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';

To:

$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

and as mentioned in the comments change chareset to charset

Edit:

if your sending a txt/html mail you have according to documentation to set mime in the headers too so try this

    $to = $this->post_data['register-email'];
    $message = 'Hello etc';

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= 'From: Website <admin@example.com>' . "\r\n";
    mail($to, 'User Registration', $message, $headers);

If it still doesn't work you could try to debugg your code, simply add

error_reporting(E_ALL);
ini_set('display_errors', '1');

on top of the page, and take it from there, and if you still can't solve it by yourself post it here and I'll do my best to help ya out.

Community
  • 1
  • 1
Breezer
  • 10,410
  • 6
  • 29
  • 50
5

I'm using this code on most of my projects:

$subject = 'subject';
$message = 'message';
$to = 'user@gmail.com';
$type = 'plain'; // or HTML
$charset = 'utf-8';

$mail     = 'no-reply@'.str_replace('www.', '', $_SERVER['SERVER_NAME']);
$uniqid   = md5(uniqid(time()));
$headers  = 'From: '.$mail."\n";
$headers .= 'Reply-to: '.$mail."\n";
$headers .= 'Return-Path: '.$mail."\n";
$headers .= 'Message-ID: <'.$uniqid.'@'.$_SERVER['SERVER_NAME'].">\n";
$headers .= 'MIME-Version: 1.0'."\n";
$headers .= 'Date: '.gmdate('D, d M Y H:i:s', time())."\n";
$headers .= 'X-Priority: 3'."\n";
$headers .= 'X-MSMail-Priority: Normal'."\n";
$headers .= 'Content-Type: multipart/mixed;boundary="----------'.$uniqid.'"'."\n";
$headers .= '------------'.$uniqid."\n";
$headers .= 'Content-type: text/'.$type.';charset='.$charset.''."\n";
$headers .= 'Content-transfer-encoding: 7bit';

mail($to, $subject, $message, $headers);
HosseyNJF
  • 491
  • 1
  • 6
  • 18
Yevgen
  • 1,239
  • 3
  • 15
  • 30
  • I got this error: "Multiple or malformed newlines found". Which was corrected after removing one of \n (there are two) from Content-Type headers line. – Yeti Mar 28 '17 at 09:46
-1

I would suggest to use PHP_EOL instead of \r\n or \n as the line break would then be determined by your environment...

$headers  = 'MIME-Version: 1.0' . PHP_EOL;

etc.. hoping this might finally solve your problem!

MediaVince
  • 479
  • 1
  • 8
  • 13
  • Headers should be separated by \r\n no matter what environment, see docs here: https://secure.php.net/manual/en/function.mail.php. – contrebis Sep 01 '15 at 12:03
  • It ultimately depends on your environement as on Linux PHP_EOL = \n and on Windows as \r\n (the proper CRLF per RFC) "2.3.7 Lines ... In addition, the appearance of "bare" "CR" or "LF" characters in text (i.e., either without the other) has a long history of causing problems in mail implementations and applications that use the mail system as a tool. " What's to say that the mail client does not convert properly those instances.. AFAIK I've never had trouble on linux with email and never had double lines because of \r\n as is neither! – MediaVince Nov 17 '15 at 12:00
  • But PHP_EOL has nothing to do with the spec for an email. In my experience, assuming that something *might possibly* convert something is a recipe for a bugs, if not now, in the future. – contrebis Nov 17 '15 at 15:47
  • You're right in that a bug is easily introduced by lack a of understanding but in this case, i tend to think that following the RFC to the bit is a little exagerated! In the end the most valuable response to the initial post is to actually use a library such as SwitfMailer::MailTransport (btw it uses a condition if not on linux and not using SMTP to actually replace \r\n with PHP_EOL in headers, body and in [this github recent commit](https://github.com/swiftmailer/swiftmailer/commit/cff72ef856d9aa3f6d7d221eea94ed939539d74b) the subject too :) it's been like that for a while now! – MediaVince Nov 17 '15 at 16:23