PHP mail html format not working - php

Using php, and with this code I receive the email as a plain text, did I miss something? as I need to send formatted email which could contain links for example.
$to = "receiver#test.com";
$subject = "Password Recovery";
$body = '
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From: info#test.net\r\n"."X-Mailer: php";
if (mail($to, $subject, $body, $headers))
echo "Password recovery instructions been sent to your email<br>";

You've re-set your headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From: info#test.net\r\n"."X-Mailer: php";
You're missing a dot in that last line, which is over-writing the previous two:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: info#test.net\r\n"."X-Mailer: php";

Look at this example, this is sufficient to send mail in php:
<?php
//change this to your email.
$to = "abc#gmail.com";
$from = "Example#example.com";
$subject = "Hello! This is HTML email";
//begin of HTML message
$message ="
<html>
<body>
<p style=\"text-align:center;height:100px;background-color:#abc;border:1px solid #456;border-radius:3px;padding:10px;\">
<b>I am receiving HTML email</b>
<br/><br/><br/><a style=\"text-decoration:none;color:#246;\" href=\"www.example.com\">example</a>
</p>
<br/><br/>Now you Can send HTML Email
</body>
</html>";
//end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
//options to send to cc+bcc
//$headers .= "Cc: [email]maa#p-i-s.cXom[/email]";
//$headers .= "Bcc: [email]email#maaking.cXom[/email]";
// now lets send the email.
mail($to, $subject, $message, $headers);
echo "Message has been sent....!";
?>

I found the same problem with a specific mail server, in my case the solution was to set "\n" instead of "\r\n" as the end of line for the headers.

Even though your problem seems to be caused by re-assigning to $headers, I've ran into a similar problem and discovered the cause to be the double line-ending "/r/n". Some mail apps such as Bluemail, start the mail body (effectively ending the headers) after reading a double line ending.
In my case, it was solved by changing this:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: info#test.net\r\n"."X-Mailer: php";
to this:
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers = "From: info#test.net\n"."X-Mailer: php";

Related

Php mail function unable to send HTML

I am trying to send an email with a link, the problem im facing is that outlook does not reconize the HTML which is being put in the email.
$onderwerp = "test";
$bericht = <<<EOT
<html>
<head>
<title>Email_test</title>
</head>
<body>
* the link
</body>
</html>
EOT;
$headers = 'From: ' . $verstuurd_van;
mail($naar_1, $onderwerp, $bericht, $headers);
Thanks in forehand for the help.
In order to be able to send a HTML email you need to add a few more headers:
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
So the entire email becomes something like:
$to = 'example#example.com';
$subject = 'Test';
$message = "
<html>
<body>
<p>
Hallo <b>Example</b>,
</p>
</body>
</html>";
$headers = "From: example#example.nl\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers);
Also take a look at: https://css-tricks.com/sending-nice-html-email-with-php/
just mention the headers in your email script like .
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
so your script will
$onderwerp = "test";
$bericht = <<<EOT
<html>
<head>
<title>Email_test</title>
</head>
<body>
* the link
</body>
</html>
EOT;
$headers = 'From: ' . $verstuurd_van;
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($naar_1, $onderwerp, $bericht, $headers);
you can take reference from html email with php or w3school email html

Sending Mail through PHP

Here i have a problem when iam sending a mail.It is delevering properly but when iam trying designing the message body with HTML tags it sending the same html tags to my email inbox.How can i overcome this please tell me.
Here the PHP code:
$to = "$email";
$subject = "Test mail";
$message = '<!Doctype html><html><head><title>Confirm Link</title></head><body><div style="color:blue">Thank u for registering with us </div>
<div> Hi'.$name.' Please click the below link to activate your accont</div><div>Active your account now</div> </body></html>';
$from = "abc#gmail.com";
$headers = "From:" . $from;
Here my deliverd email inbox message:
here designing not working it display same as my program code.Please help me.
Confirm LinkThank u for registering with us
HiSrinivas Please click the below link to activate your accontActive your account now
// 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";
Note:
If intending to send HTML or otherwise Complex mails, it is recommended to use the PEAR package ยป PEAR::Mail_Mime.
Reference
Send an HTML email:
<?php
$to = "somebody#example.com, somebodyelse#example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>
The problem may be that you are not setting the mail headers. Try this:
// 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";
mail($to, $subject, $message, $headers);

How do I turn a PHP variable string to output HTML?

I am trying to create an autoresponder email and the content is a PHP variable. I want it to output HTML code and right now it's not.
//Example:
$respondmessage = " Hello $fullname,
We are confirming your Appointment today!
Please click here to confirm!
";
This outputs:
Hello Your Name,
We are confirming your Appointment today!
Please click here to confirm!
In the email. Is there a way to make the email accept HTML code?
You can following line as an header in your code :
$headers = "Content-type: text/html\r\n";
This will help you to create email into HTML
Assuming you're using the built in mail function in PHP:
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// 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";
// Mail it
mail($to, $subject, $message, $headers);
?>
This example is from: http://us2.php.net/manual/en/function.mail.php
The important part is the $headers variable.
By the way, as andrewsi mentioned - you need to escape your quotes:
$respondmessage = "Hello $fullname,
We are confirming your Appointment today!
Please click here to confirm!";
You have to include this header to your email script
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
From http://php.net/manual/en/function.mail.php example# 4
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// 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);
?>

send html email and receive it as text

I have this php code to send an HTML email :
function sendEmail($to,$from,$subject,$body) {
$To = $to;
$Subject = $subject;
$Message = $body;
$from = "Do Not Reply";
$Headers = "From: sender#taskmanager.domain.ca"." \r\n" .
"Reply-To: ".$from." \r\n" .
'MIME-Version: 1.0' . "\r\n".
'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($To, $Subject, $Message, $Headers);
}
function sendEmailUser($fromUserObj, $userInfo){
$html="
<html>
<body>
<p>
Dear ".$userInfo["firstName"] ." ".$userInfo["lastName"]." ,</p>
<p>
Welcome to IT .</p>
<p>
Account Information:</p>
<p>
Username:".$userInfo["username"]."</p>
<p>
Password: ".$userInfo["password"]."</p>
<p>
You can login <a href = 'http://domain.com'>here</a> .</p></body>
</html>";
$subject = "subject";
sendEmail($userInfo["email"], $subject, $html);
}
I receive the email normal text, with not html interpreted.
<html> <body> <p> Dear test lastname ,</p> <p> Welcome to IT.</p> ... </html>
what's wrong with my code ??
please any help I'll appreciate it.
try decalring your header variable like this
$Headers = "From: sender#taskmanager.domain.ca"." \r\n";
$Headers .="Reply-To: ".$from." \r\n" ;
$Headers .= 'MIME-Version: 1.0' . "\r\n";
$Headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
function sendEmail parameters -> ($to,$from,$subject,$body) (4 parameters)
you are calling -> sendEmail($userInfo["email"], $subject, $html); (3 parameters)
it should be:
sendEmail($userInfo["email"], $email_from, $subject, $html);

HTML formatted email does not display correctly

I'm still struggling with this mail script - I'm now getting all the marked up html through rather than seeing it as rendered html if that makes sense?
<?php
$mailheader .= "MIME-Version: 1.0\r\n";
$mailHeader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$formcontent .="<table border='1'>";
foreach ($_POST as $field=>$value)
{
$formcontent.="<tr>";
$formcontent .= "<td>$field:</td> <td>$value</td>";
$formcontent.="</tr>";
}
$formcontent .= '<tr><td>User-Agent: </td><td>'.$_SERVER['HTTP_USER_AGENT'].'</td>';
$formcontent .="</table>";
$recipient = "info#*******.com";
$subject = "Event feedback form";
$mailheader = "From: web.form#*******-events.co.uk\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailHeader .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Failure!");
header("location:http://www.******-events.co.uk");
?>
Follow php's documentation:
You'll need html tags
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// 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);
?>
I'd rather use a PHP class as PHPMailer for HTML emails. And btw I will add the full HTML document tags (html, head, body, etc...) for the mail body.
I'd missed these:
$formcontent = '<html><body>';
Thanks for everyone's time and input though
Jim

Categories