PHP mail() function not sending email - php

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);

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.

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);

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!

Related

PHP headers code does not display address in email, instead show server default address

I tried many alternatives to this code but it does not show my display name.
$to = $email;
$subject = $subject;
$message = $msg;
$headers = 'From: info#test.com \r\n';
$headers .= "Reply-To: info#test.com \r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
try adding in the following convention.
$headers .= 'From: Mysitename <mysitename.com>' . "\r\n";
http://php.net/manual/en/function.mail.php
Ok I manage to make it work by re-arranging the headers, thanks for your input Ori.
$to = $email;
$subject = $subject;
$message = $msg;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From: Info <info#testsite.com>' . "\r\n";

Emails from variable not working

I am trying to send emails but when it comes in my inbox the from email which I have mentioned is not showing up though it coming from the mail server. Why is this happening so?
//This one is working fine
if($email) {
$from = "EditMate#editmate.co";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From:" . $from;
echo mail($email, $subject, $message_admin, $headers);
}
//In this one it is not showing the from email
if($receipt) {
$from = "newproject#editmate.co";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From:" . $from;
echo mail($receipt, $subject, $message_admin, $headers);
}
Try adding from address in the $additional_parameters section of your mail() function like:
mail($receipt, $subject, $message_admin, $headers,"-f newproject#editmate.co");

How to check if mail was send or not using php?

How to check if mail was send or not using php ?
I use this code for send email using php. It's work good.
But i want to check email send success or not, How can i do that ?
<?PHP
$to = "test#mail.com";
$subject = "test subject";
$message = "test message";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: SENDER <noreply#sender.com>' . "\r\n";
$headers .= 'Return-Path: return#sender.com' . "\r\n";
mail($to, $subject, $message, $headers, '-freturn#sender.com');
?>

Bit strange behavior of php mail function

By mistake, I added from-address as to-address in mail-function.
It sends mail to both to-address and from-address why? Is it documented anywhere?
$from = 'from_user#gmail.in';
$to = 'to_user#gmail.com';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: ' . $to . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$message = json_encode(compact('to', 'from', 'headers'));
// NOTE THE FROM INSTEAD OF TO
mail($from, $subject, $message, $headers);
Further to the comments and to highlight your request for reference. Here is a snippit from the php manual for reference. Notice the first line of additional headers:
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
So you are sending an email both through the mail($to... (which happens to be the $from in your case) but you are also sending the $to in the $headers declaration.

PHP mail displaying content type header inside the body

I'm making a form, when it submits it sends you a HTML email with a confirmation that you submitted it correctly.
When I receive the email; the content type header is inside the body of the email and the HTML code is displaying as raw text.
Screenshot:
Here is my code:
$to = 'myemail#gmail.com';
$subject = "HTML email";
$body = "<html><body><h1>Hello world!</h1></body></html>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <webmaster#website.com/>' . "\r\n";
mail($to, $subject, $body, $headers);
Could anyone please tell me what exactly is going wrong?
Thank you
Change your below three lines
From
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <webmaster#website.com/>' . "\r\n";
To
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type:text/html;charset=iso-8859-1" . PHP_EOL;
$headers .= 'From: <webmaster#website.com/>' . PHP_EOL;
I think after that this will work properly

Categories