How to send HTML/CSS emails? - php

Most email clients have problems reading CSS in HTML emails (including Gmail and Hotmail). I often use this service to convert my HTML/CSS to proper email format so that everything looks normal on the user's end. Basically what it does is convert all CSS to inline styles:
http://premailer.dialect.ca/
Do any of you have any other methods for sending CSS in your HTML emails? I automatically generate the emails, and due to some restrictions, I can't modify the the inline styles.

As for direct format, I've always done inline CSS styling, however I use SwiftMailer (http://swiftmailer.org/) for PHP5 to handle email functionality and it has helped immensely.
You can send multipart messages with different formats so if the email client doesn't like the HTML version, you can always default to the text version so you know at least something is getting through clean.
In your "views" folder, you can set apart routes for different email formats (I use smarty too, hence the .tpl extension). Here's what a typical SwiftMailer::sendTemplate() function would look like when you're setting up the templates:
$email_templates = array('text/html' => 'email/html/' . $template . '.en.html.tpl',
'text/plain' => 'email/text/' . $template . '.en.txt.tpl');
foreach ($email_templates as $type => $file) {
if ($email->template_exists($file)) {
$message->attach(new Swift_Message_Part($email->fetch($file), $type));
} elseif ($type == 'text/plain') {
throw new Exception('Could not send email -- no text version was found');
}
}
You get the idea. SwiftMailer has a bunch of other good stuff, including returning "undeliverable" addresses, logging delivery errors, and throttling large email batches. I'd suggest you check it out.

You need to add a header that says the content is HTML. When you use the mail() function, one of the headers should be: Content Type: html/text (That might not be the 'exact' header).
Let me find you an example: (From the php.net/mail page)
// 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);

To add to the above example, (in case you don't know PHP very well), you just have to build the "email" using the variables: to, subject, message, and headers
Let me know if you want to know how to create a form to fill and run this PHP script, otherwise, you can simply enter everything in this file manually, save as a PHP file, throw it up on a server that supports PHP, and navigate to the file in your browser.
Here's the code:
// Setup recipients
$to = 'johndoe#google.com' . ',' // comma separates multiple addresses
$to .= 'janedoe#msn.com';
// subject
$subject = 'PHP Email Script - Test Email';
// message (to use single quotes in the 'message' variable, supercede them with a back slash like this--> \'
$message = '
<html>
<head>
<title>PHP Email Script</title>
</head>
<body>
<p style="background: #ccc; width: 100%;">Test Email Script</p>
</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";
// Send the email
mail($to, $subject, $message, $headers);

the easiest way is to write a html page with embedded css and push it through automated styling machine

Related

Sending html received from form to email securely with php

I have a button to send a div as html to email on a website.
I would like to avoid rebuilding the html using just the variables associated but I think about the risk of doing this as someone could send anything to my php script.
Also is it enough to remove <script> tags ?
This is how I get and send the email:
$subject = 'Some title';
$message = $html_mail;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'To: '. $to . "\r\n";
$headers .= 'From: My name <'.$admin_mail.'>' . "\r\n";
$response = mail($to, $subject, $message, $headers);
Almost all the email clients such as gmail, yahoo, outlook, thunderbird, mail etc do not run javascript at all. Javascript and other scripts are disabled by default. So <script> tags are not at all security threat. However, abusive language or images that users might embed in HTML emails can be a serious issue in your case if it is not handled appropriately. If you think that your users are sophisticated and it is not the case then there are no worries.

Print only link name not whole href in php mail body

When sending an email I want to show the hyperlink value not the whole href.
This is my code
if ($model->update(array('status')))
{
$body.="Your account has now been verified. Please, login and spiff up your profile and take a look at our ,";
$body.="<a href='https://contactcenters.com/page/58/?Advertisewithus'>sponsorship program</a>";
$body.="to start building your business!";
$email=$model->email;
mail($email,$subject,$body,$headers);
//----------------------------------------------------------//
$this->redirect($model->user_id);
}
I want to send sponsored program not the whole href in the mail body. But whenever I send an email using this it prints <a href='https://contactcenters.com/page/58/?Advertisewithus'>sponsorship program</a>"; instead of Sposorship Program
I have tried to add double quotes inside single quotes but it didn't work.
You need to add to headers:
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
if you haven't done it yet.
Also you have to make sure your e-mail client is properly set. For example in Mozilla Thunderbird you may set it to display only TXT and then it may perform some changes to the content. Otherwise you may send email correctly but you will result you don't expect
Based on http://php.net/manual/en/function.mail.php
You need to inform mail engine, that you are sending something more complicated then just text
// 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);
You are sending the mail as plain text, so html would not be recognized. Add these to the $headers:
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
See http://www.php.net/manual/en/function.mail.php

PHP mail can not send when there is a url a tag in the body

When I added "a href tag " in the mail body the mail is not sent.
If I remove this 'a href and www' tag, the mail sends and all other content display as per my requirement.
I don't know where is the exact problem, I'm using GoDaddy hosting with PHP 5.3 version.
If anyone has a better solutions please share with me .
<?php
// multiple recipients
$to = 'ali.dzinemedia#gmail.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '<a href=www.google.com>Click here</a>';
// To send HTML `enter code here`mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
// Additional headers
$headers .= 'To: Mary <ali.dzinemedia#gmail.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <ali.dzinemedia#gmail.com>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
echo "To : ".$to;
// Mail it
mail($to, $subject, $message, $headers);
Use this as the header:
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset: utf8\r\n";
that's if you want to use HTML in the body, but you have to create well formatted HTML, you know with all of its tags: html, head, body, and close them all.
<html>
<head></head>
<body>Content here and this is a link</body>
</html>
I had the same problem once, turned out my spam filter blocked the mail when a link was in it and let it trough when I removed the link.
Took me some time to notice that

sending only a text email instead of html

I have this code at the top of my email php file which sends out emails
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from <$from>" . "\r\n";
$headers .= 'Cc: matt.e#posnation.com' . "\r\n";
$headers .= 'Bcc: posnation#gmail.com' . "\r\n";
I need to change this to just a plain text email ...what do i change. I was thinking the second line text/html to text/text but i wasnt sure
You want to change the "text/html" to "text/plain".
You can use text/plain
But if you enter html content the tags and all will be shown on the client though ...
But i'm sure there's an alternate way to support both text and html ...
Are you using the built in mail function or an external mail app like phpMailer?
You don't need to change anything.
Technically it should be "text/plain", but if you want, you can just as easily send an "HTML" email with no HTML tags and it will work just fine.

PHP mail function is sending emails in Gmail's SPAM folder, How to get rid of it?

i am using this code :
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
it sends email to my gmail's SPAM folder .
Suggest any solution .
i dont want to use any PEAR MAIL type way to send email or dont want to require and include any file.
This functions works without including/requiring any additional php file.
You should use proper header like this from PHP manual
$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);
The Gmail spam filter has lots of parts to it which determine the spamminess of mail. You could start by seeing the SPF record for your domain. Avoid using sales language, HTML and colours where possible in your email.
There's also an old but fun video from Google about how their spam filtering works.
It's all in the headers. I don't know how GMail works internally, but I've found on my projects that setting Reply-To can Content-Type headers seems to fix this.
See PHP mail() docs for an example of doing this.
(Note: I've got a PHP questionnaire/response system that sets these headers and the e-mails come through correctly to GMail, Hotmail and Yahoo! Mail).

Categories