PHP/HTML Code Renders as Text in Email: Any Insight? - php

I've tried many header changes to no avail.
Could it be the email server? Or am I missing something crucial?
I would like to see more than the code rendered as text in the resultant email.
Thanks!
<?php session_start(); ?>
<?php
$body = '';
$body .= '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
</head>
<body>
<h1>Here is a Copy of Your Order</h1>';
$body .= '<table>
<tr>
<th>Product</th>
<th>Cost</th>
<th>Units</th>
<th>Subtotal</th>
</tr>';
$total = 0;
foreach($_SESSION['cart'] as $item) {
$body .= "<tr>
<td>{$item['item']}</td>
<td>\${$item['unitprice']}</td>
<td>{$item['quantity']}</td>
<td>$".($item['unitprice'] * $item['quantity'])."</td>
</tr>";
$total += ($item['unitprice'] * $item['quantity']);
}
$body .= '</table>';
$body .= "<p>Grand total: \$$total</p>";
$body .='</body></html>';
}
?>
<?php
echo $body;
$to = 'xxxx#gmail.com';
$subject = 'Fill This Order';
$headers = 'From: xxxx#gmail.com' . PHP_EOL;
$headers .= 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
mail($to, $subject, $message, $body, $headers);
?>

here is a tutorial about how to send a mail with html code in it: Sending Nice HTML Email with PHP
work around it, what they got works.
as you can easly see there, the headers sent is:
$to = 'bob#example.com';
$subject = 'Website Change Reqest';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
they diffrence you can see is mostly: \r\n in every end of line.
this could be your problem because without it it can be something like:
$headers .= 'MIME-Version: 1.0' . PHP_EOL . 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
and in headers... this could be: 'MIME-Version: 1.0Content-type: text/html; charset=iso-8859-1
and not work..
i think you should take a look in the link i added and modify your code by it.
EDIT:
i think i found it! you sent mail($to, $subject, $message, $body, $headers);
while PHP mail function uses:
mail(to,subject,message,headers,parameters)
you can see here how to use the mail function: The PHP mail() Function
the thing i think heppend is you sent $message which does not exsits and the headers are not sent as they should.

Have you tried swapping out PHP_EOL for "\r\n"?

Related

Sending email with a link inside the body [duplicate]

How can I send an HTML-formatted email with pictures using PHP?
I want to have a page with some settings and HTML output which is sent via email to an address. What should I do?
The main problem is to attach files. How can I do that?
It is pretty simple. Leave the images on the server and send the PHP + CSS to them...
$to = 'bob#example.com';
$subject = 'Website Change Request';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$message = '<p><strong>This is strong text</strong> while this is not.</p>';
mail($to, $subject, $message, $headers);
It is this line that tells the mailer and the recipient that the email contains (hopefully) well-formed HTML that it will need to interpret:
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
Here is the link I got the information from... (link)
You will need security though...
You need to code your HTML content using the absolute path for images. By absolute path, I mean you have to upload the images to a server and in the src attribute of images you have to give the direct path, like this <img src="http://yourdomain.com/images/example.jpg">.
Below is the PHP code for your reference: It’s taken from mail:
<?php
// Multiple recipients
$to = 'aidan#example.com' . ', '; // Note the comma
$to .= 'wez#example.com';
// Subject
$subject = 'Birthday Reminders for August';
// Message
$message = '
<p>Here are the birthdays upcoming in August!</p>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\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";
// Mail it
mail($to, $subject, $message, $headers);
?>
I have this code and it will run perfectly for my site:
public function forgotpassword($pass, $name, $to)
{
$body = "<table width=100% border=0><tr><td>";
$body .= "<img width=200 src='";
$body .= $this->imageUrl();
$body .= "'></img></td><td style=position:absolute;left:350;top:60;><h2><font color = #346699>PMS Pvt Ltd.</font><h2></td></tr>";
$body .= '<tr><td colspan=2><br/><br/><br/><strong>Dear '.$name.',</strong></td></tr>';
$body .= '<tr><td colspan=2><br/><font size=3>As per Your request we send Your Password.</font><br/><br/>Password is : <b>'.$pass.'</b></td></tr>';
$body .= '<tr><td colspan=2><br/>If you have any questions, please feel free to contact us at:<br/>support#pms.com</td></tr>';
$body .= '<tr><td colspan=2><br/><br/>Best regards,<br>The PMS Team.</td></tr></table>';
$subject = "Forgot Password";
$this->sendmail($body, $to, $subject);
}
Mail function
function sendmail($body, $to, $subject)
{
//require_once 'init.php';
$from = 'testing#gmail.com';
$headersfrom = '';
$headersfrom .= 'MIME-Version: 1.0' . "\r\n";
$headersfrom .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headersfrom .= 'From: ' . $from . ' ' . "\r\n";
mail($to, $subject, $body, $headersfrom);
}
The image URL function is used for if you want to change the image. You have to it change in only one function. I have many mail functions, like for forgot password or create user. Therefore I am using the image URL function. You can directly set the path.
function imageUrl()
{
return "http://" . $_SERVER['SERVER_NAME'] . substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/") + 1) . "images/capacity.jpg";
}
Sending an HTML email is not much different from sending normal emails using PHP. What is necessary to add is the content type along the header parameter of the PHP mail() function. Here is an example.
<?php
$to = "toEmail#domain.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>A table as email</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Fname</td>
<td>Sname</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=UTF-8" . "\r\b";
$headers .= 'From: name' . "\r\n";
mail($to, $subject, $message, $headers);
?>
You can also check here for more detailed explanations by W3Schools.
You can easily send the email with HTML content via PHP. Use the following script.
<?php
$to = 'user#example.com';
$subject = "Send HTML Email Using PHP";
$htmlContent = '
<html>
<body>
<h1>Send HTML Email Using PHP</h1>
<p>This is a HTMl email using PHP by CodexWorld</p>
</body>
</html>';
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: CodexWorld<info#codexworld.com>' . "\r\n";
$headers .= 'Cc: welcome#example.com' . "\r\n";
$headers .= 'Bcc: welcome2#example.com' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>
Source code and live demo can be found from here - Send Beautiful HTML Email using PHP
The simplest way is probably to just use Zend Framework or any of the other frameworks like CakePHP or Symfony.
You can do it with the standard mail function too, but you'll need a bit more knowledge on how to attach pictures.
Alternatively, just host the images on a server instead of attaching them. Sending HTML mail is documented in the mail() function documentation.
Use PHPMailer.
To send HTML mail, you have to set $mail->isHTML() only, and you can set your body with HTML tags.
Here is a well written tutorial:
How to send mail using PHP
The trick is to know the content id of the image MIME part when building the HTML body part.
It boils down to making the img tag—<img src="cid:entercontentidhere" />
Kronolith.php
Look at the function buildMimeMessage for a working example.

HTML not being rendered in email

$message = 'What are the other benefits to saying "nah" that you can think of? - ' . $_POST['other-benefits'] . '<br>' .
'What are you pledging to do? - ' . $_POST['what-pledge'] . '</p>';
I am trying to do a break after each line in php but with embedded HTML. When I send an email(function not included) it doesn't render with breaks. It literally just types out the as a string. Is there a way to correct this?
Given the lack of information given about the problem, I can only assume that the issue is that the headers of the email have not been set to send HTML emails.
You'll have to do something along the lines of:
// Setup email content.
$to = "example#email.com";
$subject = "Example HTML email";
$message = 'What are the other benefits to saying "nah" that you can think of? - ' . $_POST['other-benefits'] . '<br>' .
'What are you pledging to do? - ' . $_POST['what-pledge'] . '</p>';
// Setup headers.
$headers = "From: from#email.com\r\n";
$headers .= "Reply-To: replyto#email.com\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
// This next one is where the magic happens.
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// Send!
mail($to, $subject, $message, $headers);
Refer to: https://css-tricks.com/sending-nice-html-email-with-php/
In order to send an email containing html, pay special attention to the following headers:
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
Example Code:
$otherbenefits = $_POST['other-benefits'];
$whatpledge = $_POST['what-pledge'];
$email = <<< LOL
<html>
<body>
<p> What are the other benefits to saying "nah" that you can think of? - $otherbenefits <br> What are you pledging to do? - $whatpledge </p>
</body>
</html>
LOL;
$emailaddress = "personsemail#website.com";
$subject = "Test Email";
$headers = "From: noreply#server.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($emailaddress, $subject, $email, $headers);
For more information, please visit PHP.net regarding the mail() function and how to send email as HTML.
http://php.net/manual/en/function.mail.php

Issue PHP mail header

I have the following issue when using php mail, I'll expose the two ways I do it and wich result I get and expect:
Case 1:
$to = $EmailsPropiosPresupuestos;
$subject = $txtAsuntoCliente;
$message = $emailCliente;
$headers .= "From: noreply\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
The email i sent and the html decoded in the correct way, but my "From" header looks like: "noreply#124512.net" (Strange root).
Case 2:
$to = $EmailsPropiosPresupuestos;
$subject = $txtAsuntoCliente;
$message = $emailCliente;
$headers .= "From: noreply#example.com\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
Now my "From" header is fine, "noreply#example.com", but the mail doesn't decode the html, so I can't view the message in the correct way.
Try to add the MIME version to your header:
$headers .= "MIME-Version: 1.0\n";
For the no-reply I suggest you to add it using the Reply-To:
$headers .= "Reply-To: noreply#example.com\n";
This will allow you to keep a better from email address.
And In my code I use \n on each header line and finish with \r\n.
from: http://de3.php.net/manual/en/function.mail.php
Example #4 Sending HTML email
It is also possible to send HTML email with mail().
<?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);
?>

php mail() not sending styles

I am trying to send style information along with an email via PHP mail() function. Unfortunately, even though my mail client is set to accept styled emails it still just renders as plain html text.
$to = $email;
$subject = "Subject Details";
$message='<html><body><table width="600" height="840" border="0" cellpadding="5" cellspacing="5">';
$message.='<tr><td height="110"><img src="https://user.co.uk/images/logo.jpg" alt="SignDox" width="300" height="100" /></td></tr>';
$message.='<tr><td height="29" bgcolor="#5C1561"> </td></tr>';
$message.='<tr><td height="537" valign="top">';
$message.="Message Goes Here\n\n";
$message.="Username: ".$new_agent_id."\n";
$message.="Password: ".$pass1."\n\n";
$message.="To sign in to your user panel follow this link: \n";
$message.="Inside your admin section you will be able to change your username and password.\n";
$message.='</td></tr>';
$message.='<tr><td height="140" bgcolor="#5C1561"> </td></tr></table></body></html>';
$from = "no-reply#sender.co.uk";
$headers = "From:" . $from;
mail($to,$subject,$message,$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";
Note:
1) If intending to send HTML or otherwise Complex mails, it is recommended to use the PEAR package » PEAR::Mail_Mime.
2) It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.
Please read more this: PHP Manual
You need to define the correct headers in order to send HTML formatted emails. This can be done very simply using:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
However, I would urge you to use the PHPMailer Class. This class will handle all headers and anything else you may need. You can easily add attachments, embed images, send via SMTP etc...
It is a fantastic class, and not to mention the amount you will learn with objects and classes, expecially if you are a newbie to PHP :-)
See here for the PHPMailer Class
Set the content type, and charset as desired. You should also terminate your from header with \r\n.
$headers = "From:" . $from . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
try something like
<?php
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From:" . $from;
/*
$headers[] = "Bcc: JJ Chong <bcc#domain2.com>";
$headers[] = "Subject: {$subject}"; */ optional
mail($to, $subject, $message, implode("\r\n", $headers));
function send_mail($from,$fromName,$to,$object,$bodyText,$bodyHtml){
$site = "mywebsite.ca";
$from = $fromName." <".$from.">";
$limite = "_----------=_parties_".md5(uniqid (rand()));
$header = "Reply-to: ".$from."\n";
$header .= "From: ".$from."\n";
$header .= "X-Sender: <".$site.">\n";
$header .= "X-Mailer: PHP\n";
$header .= "X-auth-smtp-user: ".$from." \n";
$header .= "X-abuse-contact: ".$from." \n";
$header .= "Date: ".date("D, j M Y G:i:s O")."\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/alternative; boundary=\"".$limite."\"";
$message = "";
$message .= "--".$limite."\n";
$message .= "Content-Type: text/plain\n";
$message .= "charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 8bit\n\n";
$message .= $bodyText;
$message .= "\n\n--".$limite."\n";
$message .= "Content-Type: text/html; ";
$message .= "charset=\"iso-8859-1\"; ";
$message .= "Content-Transfer-Encoding: 8bit;\n\n";
$message .= $bodyHtml;
$message .= "\n--".$limite."--";
if(mail($to,$object,$message,$header)){
//echo all
}
else{
//echo mssage not submit
}
}
hope this function can help you. try to use this function to as your send_mail, then at the buttom try to
write a condition to see if your message is submitted.You just needed to give inputs in it
//your header should be like this
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

Send HTML in email via PHP

How can I send an HTML-formatted email with pictures using PHP?
I want to have a page with some settings and HTML output which is sent via email to an address. What should I do?
The main problem is to attach files. How can I do that?
It is pretty simple. Leave the images on the server and send the PHP + CSS to them...
$to = 'bob#example.com';
$subject = 'Website Change Request';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$message = '<p><strong>This is strong text</strong> while this is not.</p>';
mail($to, $subject, $message, $headers);
It is this line that tells the mailer and the recipient that the email contains (hopefully) well-formed HTML that it will need to interpret:
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
Here is the link I got the information from... (link)
You will need security though...
You need to code your HTML content using the absolute path for images. By absolute path, I mean you have to upload the images to a server and in the src attribute of images you have to give the direct path, like this <img src="http://yourdomain.com/images/example.jpg">.
Below is the PHP code for your reference: It’s taken from mail:
<?php
// Multiple recipients
$to = 'aidan#example.com' . ', '; // Note the comma
$to .= 'wez#example.com';
// Subject
$subject = 'Birthday Reminders for August';
// Message
$message = '
<p>Here are the birthdays upcoming in August!</p>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\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";
// Mail it
mail($to, $subject, $message, $headers);
?>
I have this code and it will run perfectly for my site:
public function forgotpassword($pass, $name, $to)
{
$body = "<table width=100% border=0><tr><td>";
$body .= "<img width=200 src='";
$body .= $this->imageUrl();
$body .= "'></img></td><td style=position:absolute;left:350;top:60;><h2><font color = #346699>PMS Pvt Ltd.</font><h2></td></tr>";
$body .= '<tr><td colspan=2><br/><br/><br/><strong>Dear '.$name.',</strong></td></tr>';
$body .= '<tr><td colspan=2><br/><font size=3>As per Your request we send Your Password.</font><br/><br/>Password is : <b>'.$pass.'</b></td></tr>';
$body .= '<tr><td colspan=2><br/>If you have any questions, please feel free to contact us at:<br/>support#pms.com</td></tr>';
$body .= '<tr><td colspan=2><br/><br/>Best regards,<br>The PMS Team.</td></tr></table>';
$subject = "Forgot Password";
$this->sendmail($body, $to, $subject);
}
Mail function
function sendmail($body, $to, $subject)
{
//require_once 'init.php';
$from = 'testing#gmail.com';
$headersfrom = '';
$headersfrom .= 'MIME-Version: 1.0' . "\r\n";
$headersfrom .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headersfrom .= 'From: ' . $from . ' ' . "\r\n";
mail($to, $subject, $body, $headersfrom);
}
The image URL function is used for if you want to change the image. You have to it change in only one function. I have many mail functions, like for forgot password or create user. Therefore I am using the image URL function. You can directly set the path.
function imageUrl()
{
return "http://" . $_SERVER['SERVER_NAME'] . substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/") + 1) . "images/capacity.jpg";
}
Sending an HTML email is not much different from sending normal emails using PHP. What is necessary to add is the content type along the header parameter of the PHP mail() function. Here is an example.
<?php
$to = "toEmail#domain.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>A table as email</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Fname</td>
<td>Sname</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=UTF-8" . "\r\b";
$headers .= 'From: name' . "\r\n";
mail($to, $subject, $message, $headers);
?>
You can also check here for more detailed explanations by W3Schools.
You can easily send the email with HTML content via PHP. Use the following script.
<?php
$to = 'user#example.com';
$subject = "Send HTML Email Using PHP";
$htmlContent = '
<html>
<body>
<h1>Send HTML Email Using PHP</h1>
<p>This is a HTMl email using PHP by CodexWorld</p>
</body>
</html>';
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: CodexWorld<info#codexworld.com>' . "\r\n";
$headers .= 'Cc: welcome#example.com' . "\r\n";
$headers .= 'Bcc: welcome2#example.com' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>
Source code and live demo can be found from here - Send Beautiful HTML Email using PHP
The simplest way is probably to just use Zend Framework or any of the other frameworks like CakePHP or Symfony.
You can do it with the standard mail function too, but you'll need a bit more knowledge on how to attach pictures.
Alternatively, just host the images on a server instead of attaching them. Sending HTML mail is documented in the mail() function documentation.
Use PHPMailer.
To send HTML mail, you have to set $mail->isHTML() only, and you can set your body with HTML tags.
Here is a well written tutorial:
How to send mail using PHP
The trick is to know the content id of the image MIME part when building the HTML body part.
It boils down to making the img tag—<img src="cid:entercontentidhere" />
Kronolith.php
Look at the function buildMimeMessage for a working example.

Categories