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: [email protected]' . "\r\n" ;
$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';
$headers .= 'From: Website <[email protected]>';
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 <[email protected]>';
mail($to, 'User Registration', $message, $headers);
18
  • Is this code running on Linux or Windows? If Windows what PHP version you running on? Commented Sep 17, 2013 at 16:05
  • Also you don't show us how you're adding the "Subject:", can you add that. Commented Sep 17, 2013 at 16:06
  • You've got a typo error, charset, not "chareset" Commented Sep 17, 2013 at 16:06
  • @crm Try using this $to = $_POST['register-email']; instead of $to = $this->post_data['register-email']; Commented Sep 17, 2013 at 16:12
  • Thanks, but its still not playing ball Commented Sep 17, 2013 at 16:12

3 Answers 3

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 <[email protected]>' . "\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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I updated my question with the new code that still doesnt work.
5

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

$subject = 'subject';
$message = 'message';
$to = '[email protected]';
$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);

1 Comment

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.
-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!

4 Comments

Headers should be separated by \r\n no matter what environment, see docs here: secure.php.net/manual/en/function.mail.php.
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!
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.
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 the subject too :) it's been like that for a while now!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.