I have PHP and Postfix set up at my Ubuntu server. I need to send HTML email from PHP script. The email is sent just fine, but it is displayed as plain text with HTML tags included. Additionally, some headers are also displayed in the email itself.
My guess is, that it has something to do with the headers. I've spent nearly a day searching for a possible solution and haven't found one.
Here's the PHP code:
$headers='';
$headers.="MIME-Version: 1.0 \r\n";
$headers.="Content-type: text/html; charset=\"UTF-8\" \r\n";
$headers.="From: ".FROM_EMAIL."\r\n";
mail($email, $vars['title'], $content, $headers);
EDIT:
$headers='';
$headers.='MIME-Version: 1.0'."\r\n";
$headers.='Content-type: text/html; charset=iso-8859-1'."\r\n";
$headers.='From: Kinesioteip.ee<'.FROM_EMAIL.'>'."\r\n";
$headers.='To: '.$email."\r\n";
mail($email, $vars['title'], $content, $headers);
Still no luck...
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To:' . "\r\n";
$headers .= 'From: Admin<youremail#email.com>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
can you try changing these lines
$headers.="MIME-Version: 1.0 \r\n";
$headers.="Content-type: text/html; charset=\"UTF-8\" \r\n";
to
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
and see if they work
i think your fourth header is wrong because FROM_EMAIL variable not having '$'
try
$headers.="From: ".$FROM_EMAIL."\r\n";
$vars['title'] change in to $var
mail($email, $vars, $content, $headers);
Related
On sending a mail I've set my header to:
$headers = 'From: Tómas<tomas#email.com>'. "\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\n";
for mail($to, $subject, $message, $headers);
The Tómas is working in the content correctly but not in the from section of the email. It shows up like Tómas. Any idea how to modify the header to make this work?
Many thanks.
Tó = Tó
change the following line
$headers = 'From: Tómas<tomas#email.com>'. "\r\n";
with
$headers = 'From: Tomas<tomas#email.com>' . "\r\n";
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!
I am trying to figure out why the bcc part of this PHP mail function is not working in the code below:
function _send_user_email($to, $subject, $message) {
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=iso-8859-1";
$headers[] = "From: Customer Service <support#mydomain.com>";
$headers[] = "Bcc: <support#mydomain.com>";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, implode("\r\n", $headers));
}
I wouldn't think that there should be any problem specifying a bcc email address that is the same as the From address, but I'm not sure.
When I test this function, the recipient receives the message, but the BCC copy does not come through. Any idea why? Thanks.
Seriously, don't use the mail() function -- you're just letting yourself into a world of hurt.
If you want to do anything beyond absolutely the most basic email, I strongly recommend using a decent mail class, such as phpMailer.
It will make things much easier. No more messing around building the headers yourself, or trying to get the mime types working. Sending to multiple addresses, CC and BCC addresses becomes simple, and adding attachments goes from virtually impossible with mail() to dead simple.
Hope that helps.
try this one in your script you have to change "" to '' and also remove <> then it will be work here i edited your script
function _send_user_email($to, $subject, $message) {
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=iso-8859-1";
$headers[] = "From: Customer Service <support#mydomain.com>";
$headers[] = 'Bcc: support#mydomain.com';
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, implode("\r\n", $headers));
}
here my mail function example
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: mydemo.com<$your_email>\r\n" .
$headers .= 'Bcc: mydemo#mydemo.com' . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
Now If You Want To Use Html In Message
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
For Exampe Message
$message .='<table><tr><td></td></tr></table>';
Try to use Cci like this example:
$headers = array(
'From' => $from,
'To' => $to,
'Cci' => $bcc,
'Subject' => $subject
);
it working fine, i have tested the script found that email drop in Junk box.
try to add name of email holder :
"Bcc: Support <support#mydomain.com>";
My script send the email but it doesn't render the html tags. I'm not sure why not.
$email = $row['email'];
$mail_body = '<html>
<body>
<p>hello,'.$name.'</p>
<p>this is a testing email </p>
<hr />
<p>by server</p>
</body>
</html>';
$subject = "Better website";
$to = "$email";
$headers = "From: mailscript#hotmail.com\r\n";
$headers .= "Content-Type: text/html\r\n";
$mail_result = mail($to, $subject, $mail_body, $headers);
Try setting the mime type as well as shown in the manual for mail()
// 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";
as Jrod already answered you need to add the header
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
but I found that this one is screwing mine up as of late and had to comment it out:
$headers = 'MIME-Version: 1.0' . "\r\n";
I know this is simple but my brain is fried from trying to solve a different problem!
I'm using php's mail function to email the user. Below is my code. See the a href link, how do I get this to display as an actual link within the php?
$email=someone#example.com;
$content= "Dear Whoever,
NB: Please click here to read and download the terms and conditions.";
mail( "$email", "Welcome", $content, "From: support#example.com");
As noted above, your code has error. It should be:
email='someone#example.com';
$content= "Dear Whoever,
NB: Please click here to read and download the terms and conditions.";
mail( $email, "Welcome", $content, "From: support#example.com");
You would need to set the mime type to HTML in the header, and use it as a parameter in the mail() function.
From the manual
// 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);
Though I usually use SwiftMailer and it has other neat features.