Processing a HTML email template file and php - php

I created a html email welcome.tpl what php mail method do I have to use in order to send out that file as the body of the message? prior to this I've been using and including the html and text in variables
$text_content.= "\r\n";
$text_content.= "--------------------------------\r\n";
$html_content.= "</body></html>";
$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';
$headers = "MIME-Version: 1.0\r\n";
$headers.= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\r\n";
$headers.= "Content-Transfer-Encoding: 7bit\r\n";
$body = "--$mime_boundary\n";
$body.= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n";
$body.= "Content-Transfer-Encoding: 7bit\n\n";
$body.= $text_content;
$body.= "\n\n";
$body.= "--$mime_boundary\n";
$body.= "Content-Type: text/html; charset=\"UTF-8\"\n";
$body.= "Content-Transfer-Encoding: 7bit\n\n";
$body.= $html_content;
$body.= "\n\n";
$body.= "--$mime_boundary--\n";
$headers.= 'From: So <support#sos.com>' . "\r\n";
$headers.= "X-Sender-IP: $_SERVER[SERVER_ADDR]\r\n";
$headers.= 'Date: '.date('n/d/Y g:i A')."\r\n";
$headers.= 'Reply-To: So <support#sos.com>' . "\r\n";
mail($en['email'], $subject, $body, $headers);
should i be using something like $body = file_get_contents(); and is mail(); the best method?

I would suggest the following:
You are using a .tpl extension on your template, so I'm assuming you are running Smarty as a template engine?
If not, then you can simply use file_get_contents();
$template = file_get_contents('template.tpl');
$template = str_replace('{name}', 'Sean Nieuwoudt', $template);
$template = str_replace('{email}', 'me#me.com', $template);
...
etc
The simply use the mail() function to send off the email.
The alternative and somewhat more reliable way would be to use something like Postmarkapp to send emails. It guarantee's delivery where as mail() might end up in the receivers spam folder (especially if running on a shared hosting environment).
With postmark, you can do something like this:
Mail_Postmark::compose()
->addTo('jane#smith.com', 'Jane Smith')
->subject('Subject')
->messagePlain($template)
->send();
Take a look at some of the freely available PHP-Postmark classes http://developer.postmarkapp.com/developer-libs.html#php-5

Use the following code :
// 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";
and you can also include that
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
and use
$body = file_get_contents();
and send the mail by mail function:
mail($en['email'], $subject, $body, $headers);

Related

Want to put button instead of link in mail

I want to convert the link to the button in PHP mail.php file.
$message = sprintf($this->language->get('mail_message'), $store_name, $link);
I will appreciate if someone helps me to convert the link to the button.
To achieve this, you have to send html email instead of plain/text email. It is pretty simple, leave the images on the server and send the PHP + CSS to them...
$to = 'xyz#example.com';
$subject = 'Subject';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: abc#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$message = '<p>Anchor Text</p>';
mail($to, $subject, $message, $headers);

PHP mail function Carbon Copies

I am having trouble with php's mail function.
This is the code:
public function send_email($emailInfo)
{
// email fields: to, from, subject, and so on
$to = $emailInfo['toEmail'];
$from = $emailInfo['fromEmail'];
$subject = $emailInfo['subject'];
$message = $emailInfo['message'];
//$message = $this->base_directory.'/application/views/emailtemplates/ticketresponse'($emailInfo['viewVars'], true);
$headers = 'From: '.$emailInfo['fromEmail'].' <'.$emailInfo['fromEmail'].'>';
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
if (is_array($emailInfo['cc']))
{
$headers .= 'Cc: ';
foreach ($emailInfo['cc'] as $cc)
{
$headers .= $cc. ",";
}
$headers = substr($headers, 0, -1);
$headers .= "\r\n";
}
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// send
#mail($to, $subject, $message, $headers, '-f'.$emailInfo['fromEmail']);
}
the function does indeed send emails, and the array of cc recipients is indeed populated, however these recipients do not receive any emails.
Any additional headers supplied must be seperated by \r\n ie, CRLF. You are doing this for the cc header, but not for the others. This might have an impact.
$headers .= "\nMIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= " boundary=\"{$mime_boundary}\"\r\n";
I strongly recommend avoiding PHP's built-in mail() function. It's really not fit for purpose for anything beyond the most basic panic email to the site administrator.
For anything more than that (including simple stuff like adding CC addresses), I recommend using a decent mailer class, such as PHPMailer. It will make your code much simpler and more reliable.
Hope that helps.

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 email using php deliver message as html code

iam sending html message contains table
when i recive message on gmail,hotmail,yahoo account it works fine but when receiving on other client
ie Microsoft outlook it resolve it as html code !!
here is my email headers
'MIME-Version: 1.0' . "\r\n" .
'Content-Type: text/html;charset= UTF-8' . "\r\n" .
'From: me <me#client.com>'
I Always use this function ,and it helps
function sendHTMLemail($HTML,$from,$to,$subject,$fromname)
{
$headers = "From: ".$fromname." <".$from.">\r\n";
$headers.= "Reply-To: ".$fromname." <".$from.">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$boundary = uniqid("HTMLEMAIL");
// First we be nice and send a non-html version of our email
$headers .= "Content-Type: multipart/alternative;".
"boundary = $boundary\r\n\r\n";
$headers .= "This is a MIME encoded message.\r\n\r\n";
$headers .= "--$boundary\r\n".
"Content-Type: text/plain; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode(strip_tags($HTML)));
// Now we attach the HTML version
$headers .= "--$boundary\r\n".
"Content-Type: text/html; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($HTML));
// And then send the email ....
mail($to,$subject,"",$headers);
}
I know this isn't an answer for your question, but I suggest using a mailing library, that will allow you to send mails much more easily, and supports features such as attachments, authentication, etc.
I recommend SwiftMailer which works great, is simple and well documented.

HTML email not rendering when sent

my html email welcome.tpl file is formatted corrected I tested it with PutsMail and it the code below process and sends out the email but the HTML is not rendered. All i'm getting is the actually html source code in the email.
$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-Type: multipart/alternative; boundary=" .$mime_boundary. "\r\n";
$headers.= "Content-Transfer-Encoding: 7bit\r\n";
$headers.= "From: <support#system.com>" . "\r\n";
$headers.= "X-Sender-IP: $_SERVER[SERVER_ADDR]" . "\r\n";
$headers.= "Date: ".date('n/d/Y g:i A') . "\r\n";
$headers.= "Reply-To: my" . "\r\n";
$subject='New Reg';
$body.= "{$mime_boundary}\n";
$body.= "Content-Type: text/html; charset=iso-8859-2\n";
$body.= "Content-Transfer-Encoding: 7bit\n\n";
$body.= $html_content;
$body.= "\n\n";
$body.= "--{$mime_boundary}\n";
$html_content = file_get_contents('emails/welcome.tpl');
$body = str_replace("{Username}",$en['user'],$html_content);
mail($en['email'], $subject, $body, $headers);
Use and check:
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
Hope it helps
All boundaries need "--" prepended, also the last (closing) mime boundary needs "--" appended, so
$body.= "[--]{$mime_boundary}\n";
should by changed to
$body.= "--{$mime_boundary}--\n";
on last occurrence and to
$body.= "--{$mime_boundary}\n";
on all but last occurrences.

Categories