send html email using php deliver message as html code - php

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.

Related

Unable to email using php when add context type

When I use headers like this, I cant receive the email that I send to myself
$headers = "From: 123#example.com \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=ISO-8859-1\r\n";
$headers .= "'X-Mailer: PHP/' . phpversion();";
but when I use this header just like this:
$headers = "From: 123#example.com \r\n";
I can receive the email that I send to myself.
Can anyone tell me whats wrong with it?

Send attachment with Sendmail

I want to send an attachment with PHPmailer, but when I add the 'addattachment' string, the mail is being send in plain text. This is my code:
$headers .= "AddAttachment('downloads/file.pdf','file.pdf')\n";
$headers .= "Reply-To: Contoso <noreply#contoso.com>\n";
$headers .= "From: Contoso <info#contoso.com>\n";
$headers .= "Organization: Contoso \n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=UTF-8\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-Mailer: PHP". phpversion() ."\n" ;
mail($to, $subject, $body, $headers,$param);
So the script is working without the 'AddAttachment' rule. How can I add an attachment succesfully?
Here is a tutorial how to send an attachment.
http://webcheatsheet.com/php/send_email_text_html_attachment.php#attachment
Its not enough to set only the header. You have to add the attachment as base64 encoded tring to your message.
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
Perhaps its much easier to use a lib with do the work something like PHPMailer or Swift Mailer what i prefer.
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

Processing a HTML email template file and 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);

Problem dynamically sending text message with PHP

I am trying to dynamically send text messages using a PHP script. PHP code:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$textbody=<<<_MESSAGE_
Some text
_MESSAGE_;
mail('myphonenumber#SMSgateway','subject',$textbody,$headers);
I did receive a text message, but it is a "photo message" or rather multimedia instead of text and I am unable to open the message. I have tried playing around with the encoding and $textbody="this text"; instead of *MESSAGE*.
a) How can I send a normal text message (not multimedia)?
b) Why can't I open it?
c) Is there a way for people to respond to the texts I send with text? When I sent myself a text from hotmail I was able to reply and I got the answer in my inbox. When I tried to put $header.= 'From: me <me#somedomain.com>' . "\r\n"; the email wouldn't send
(reason: 553 sorry, your mail was
administratively denied. (#5.7.1))
Thanks!
$sendTo = "test#test.com";
$subject = trim($_POST['subj']);
$headers = "From: ".$_POST['name']."<" . trim($_POST["email"]) .">\r\n";
$headers .= "Reply-To: " . trim($_POST["email"]) . "\r\n";
$headers .= "Return-path: " . trim($_POST["email"]);
$headers .= "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$message = strip_tags($_POST["messg"]);
if (#mail($sendTo, $subject, $message, $headers))
{ echo "sent successfully"; }
else
{ echo "Error ... Plz try again later" ; }
This code which I'm using to send emails
I worked before on SMS project so if you have any question about how to link with Getaway feel free to contact me

How do I send emails with Arabic content via PHP's mail function?

I'm having a challenge with sending emails with arabic content using PHP's mail function. Let's say I have this simple arabic string:
بريد
I've tried several ways to utilize the headers, but the emails content all still end up with something like: X*X1X(X1Y X/. However, the email subject is correctly encoded if I use arabic characters (thanks to the base64_encode, see function below)
Here's one of the email functions I've tried
function sendSimpleMail($to,$from,$subject,$message) {
$headers = 'MIME-Version: 1.0' ."\r\n";
$headers .= 'To: '.$to ."\r\n";
$headers .= 'From: '.$from . "\r\n";
$headers .= 'Content-type: text/plain; charset=UTF-8; format=flowed' . "\r\n";
$headers .= 'Content-Transfer-Encoding: 8bit'."\r\n";
mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=',$message, $headers);
}
Any suggestions on alternative ways to achieve this goal?
Unfortunately, 8bit encoding is not reliable in e-mail. Many mail transport agents will remove the top bit of every byte in the mail body. بريد is "\xD8\xA8\xD8\xB1\xD9\x8A\xD8\xAF" in UTF-8 bytes; remove the top bit from those bytes and you get ASCII "X(X1Y\nX/".
The way to get non-ASCII characters into a mail body is to set Content-Transfer-Encoding to either base64 or quoted-printable, and the encode the body with base64_encode or quoted_printable_encode, respectively.
(quoted-printable is better if the mail is largely ASCII as it retains readability in the encoded form and is more efficient for ASCII. If the whole mail is Arabic, base64 would probably be the better choice.)
$boundary = uniqid(rand(), true);
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary = $boundary\n";
$headers .= "This is a MIME encoded message.\n\n";
$headers .= "--$boundary\n" .
"Content-Type: text/plain; charset=UTF-8 \n" .
"Content-Transfer-Encoding: base64\n\n";
$headers .= chunk_split(base64_encode($plaintext));
$headers .= "--$boundary\n" .
"Content-Type: text/html; charset=ISO-8859-1\n" .
"Content-Transfer-Encoding: base64\n\n";
$headers .= chunk_split(base64_encode($msg));
$headers .= "--$boundary--\n" .
mail($address, $subject, '', $headers);
This one works for me.
Try this
$headers .= 'From: =?UTF-8?B?'.base64_encode($from). "\r\n";
Your code works for me as-is.
Are you sure that $message contains a valid UTF-8 string?

Categories