Sending mail with css is not applying in zend - php

I am using zend.Following is my piece of code in my Action
.......
// create view object
$html = new Zend_View();
$html->setScriptPath(APPLICATION_PATH . '/layouts/scripts/');
// assign values
$html->assign('OrderList', $this->view->OrderList);
$html->assign('title', 'Package Slip');
$html->assign('current_date', date("F d, Y"));
// render view
$bodyText = $html->render('test.phtml');
$mail = new Zend_Mail('utf-8');
$mail->setBodyHtml($bodyText)
->setFrom('noreply#metalxplus.com', 'admin')
->addTo('dineshkumar.m#openwavecomp.in')
->setSubject('Test');
$mail->send();
........
When I echo this $bodyText variable before sending mail, I got complete page with css. But when i send it to mail, css is not applying. What i have to do here? Kindly advice

Many email clients utilise old version of html and therefore do not respect css. Gmail for instance will not respect any css you add to your message.
To view output, I suggest using either litmus or email on acid which displays how your message will look across a range of email clients.

First of all, view the source of the e-mail to see if the e-mail even contains the CSS you expect. If you're using relative paths; this might be the source of the issue. When you visit a page on, say, //example.com/hello, a path such as "/css/style.css" will be requested as "//example.com/css/style.css".
An e-mail client has no such luxury; it doesn't have the domainname. You'll have to provide the full path to the CSS, ie "http://example.com/css/style.css".

For this problem what I did is to apply in-line styles and it works fine.

Related

Where do I format my html email?

So I have designed a website that Ill be hosting very soon. Everything works including forms. However, When a user sends me emails through the form, the email formatting is very ugly.I want to design my emails, something like this:
https://cdn.tutsplus.com/webdesign/uploads/2013/06/21.png
So when I send an email to my client, it should somewhat look attractive like above.
My question is, where do I code the design of the HTML email? Do I do that in my index.html, where my website content is there? But that doesn't make sense?
Do I create a new HTML file and do the code there? and how do i do it?
Thanks in advance
From a plain ugly mail moving to the "right formatted" template mails is a big jump.
You need to tell what you are using to send the mail. Are you using any frameworks or is it just PHP?
To send mails in HTML format, use the below as part of the headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Are you already sending mails in HTML Format and just need help on formatting the HTML? Just remember that most of the email clients and email providers don't support external css files. Gmail even removes the CSS Styles at the head of the HTML. Your safest place for CSS styles will be inline using "Style" tag.
For the fancy formatting, I think the safe way to go is to use Tables (and nested tables if need be) most of the email clients and email providers remove the Div tags also.
If you are using PHPMailer then you can save the HTML code into a variable and assign it to the Body like given below:
$mail->Body = "<!-- your html code goes here -->;
As was mentioned by Els Den Lep, there is no way to guarantee that your mail will appear in the format you want in all the email providers / client. However, if you use a combination of Tables, inline CSS you can be fairly sure of a robust display (very close to what you want).
An inelegant hack will be to convert your HTML as an image and use utilities like GIMP or Photoshop to convert it into an HTML using the slice option. This can be especially useful if you are having a large image where you need to give links to specific areas of the image.
Supposing that the file that handles the sending of your email is called sendmail.php, you can do the styling in your sendmail.php as inline css like so:
$message = 'Contact form.<br />';
$message .= 'Subject:<br />';
$message .= "<h1 style=\"color:blue;margin-left:30px;\">This is a heading.</h1><br />";

HTML in php mail function

I have a contact form on my website so users can send me an email but I have run into a problem.
I want to use an HTML link inside the email and I also want the content the user is sending to me to be formated how they would like it.... let me explain.
If a user sends this:
Hello World!
Isnt it a great Day?
without using headers to enable html, then it says formated like that when it reaches me.
If I use headers (MIME) to enable html, to also include a link in the email (which I would like to do), then it reaches me as:
Hello World!Isnt it a great Day?
How can I include html, and also keep the email formatted properly?
Thanks
And hopefully all this makes sense :S
Use nl2br on your message - http://php.net/manual/en/function.nl2br.php
It will replace all newlines with HTML <br>
$message = nl2br($message, false);
The second parameter *is_xhtml* is optional if you want to send an HTML email instead of XHTML

How to show Embedded image in an HTML email as normal images (PHP)

I am creating an email client and going through many problems. Now the latest problem which I am facing is to show embedded images on the HTML email body.
My email body code looks similar to this:
<img alt="image001" src="cid:image001.gif#01CCB988.809DD560" id="Picture_x0020_1" />
Few related posts are found, but they are not useful. Also looking for other possible similar problems while displaying mail contents.
I am using PHP IMAP with POP3 to fetch mails. Currently using gmail as mail server. Sending mails using SMTP (PhpMailer).
Thanks in advance.
In HTML, images are standalones documents with a specific URI (used by src="" attribute).
In your MIME boby, this src attribute refer to a relative MIME part.
So, you'll need to write all MIME parts contents in a distinct file (i.e. /tmp/[md5_MIME_PART_ID])
Then use a CGI script to rewrite the email body and forward specific mime request :
readmail.php
<?
if($_GET['mime']) {
readfile("/tmp/{$_GET['mime']}");
die;
}
$contents = preg_replace("/sid:/", "readmail.php?mime=$1", $contents);
echo $contents;
Of course, a proper database system will be more efficient here, but that's a start
Did you use:
$mail->AddEmbeddedImage(filename, cid, name);
//By using this function with this example's value above, results in this code:
$mail->AddEmbeddedImage('my-photo.jpg', 'my-photo', 'my-photo.jpg ');
If yes, what is going wrong? Why are you not happy.
Please note that by default, most email clients do not show embedded images, this is goes for Gmail and Outlook. You can not circumvent that. Its a security / bandwith saving feature. The user would first have to press 'Show images' before they appear.

how to send image in newsletter?

can anyone tell me how can i send images as main body part in newsletter. how can i add any image from backend such that when i send a newsletter to the subscribers the image i want to show goes as main body part of the mail.
actually what i am saying is i have a form in which i can enter text and that text goes well in newsletter. now i want to add images in the form as well so that i don't need to write anything and only image will go in the mail as main body part of mail.
Thank you so much.
you guys have been very supportive to me.
If your newsletter is in HTML format, simply link to the live images that are on a server.
If you don't have your own server, simply host the images in any sort of free images hosting i.e. (imageshack.us) and on the body of your emails add:
<img src="http://imageshack.us/myimage.png">
And when the user opens the email, the images will load from the server.
As long as the email is multi-part (as previously suggested), any users that "can't read" HTML, will get the text version, which can have "hard-links" to the images on your live server.
Hope this helps you
First, you need to send your newsletter as HTML. Then, you can insert the image in your newsletter as
<img src="image_url">
The image_url can be remote or embedded. The remote image makes email smaller but most mail clients will ask user's approval. The embedded image will be displayed without asking user but it will be part of Email.
To use remote image, just host the image somewhere and put the URL as image_url.
Embedded image needs to be encoded as MIME parts. It's not trivial to do this. You need to use a package like PhpMailer,
http://sourceforge.net/projects/phpmailer/
Here is an example,
<?php
require("class.phpmailer-lite.php");
$mail = new PHPMailerLite();
$mail->From="you#example.com";
$mail->FromName="Your Name";
$mail->AddAddress("list#example.com");
$mail->Subject = "Your fancy newsletter";
$mail->IsHTML(true);
$mail->AddEmbeddedImage('image.png', 'image_id', 'test.png', 'base64', 'image/png');
$mail->Body = <<<EOT
<h1>My Newsletter</h1>
<p>This picture is embedded in newsletter: <img src="cid:image_id" /></p>
EOT;
$mail->AltBody="Text only, sorry no image";
if(!$mail->Send())
{
echo "Error sending: " . $mail->ErrorInfo;
}
else
{
echo "Mail is sent";
}
?>
You should create a multipart mime message, containing the image and maybe some HTML.
I wouldn't recommend attaching images to an email....it's one of many flags to email service providers that you're sending spam. Best to do as listed in Marcos' solution and link from another server via absolute URL. Remember also to avoid the obvious email no-nos that will get you black flagged. It only takes one questionable email to raise a flag with a spam filter that will aggressively report you to the RBL's. And trust me, it's not fun to get off those lists!
My company sends tens of thousands of emails on behalf of our clients every day. We tried embedding at one point for a test, and found that more than 50% of our emails were either undeliverable or lost in the "great abyss" of spam filtering. By linking, we're at well over 85% deliverablility, depending of course on the quality of the emails provided.

Whats the correct way to embed images PHPMailer with an HTML email?

I want to send an Newsletter with PHPMAiler. The newsletter does work, but I'm wondering if there is a better option to do this.
What I have is.
HTML Page
Images
Now my code looks as follows
$mail = new PHPMailer();
//Adding the body
$body = file_get_contents('template/index.htm');
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view this message, please use an HTML compatible email viewer!";
$mail->SetFrom('xxxxxxx', 'xxxxxxxxxx');
$address = ".......#gmail.com";
$mail->AddAddress($address, "xxxxxxx");
$mail->AddEmbeddedImage("template/images/bullet_point.gif","1");
$mail->AddEmbeddedImage("template/images/template_1_01.gif","2");
$mail->AddEmbeddedImage("template/images/template_1_03.gif","3");
$mail->MsgHTML($body);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
I use file_get_contents to get the html page, and AddEmbeddedImage to embed images, now is there a way to pass only the HTML page to PHPMailer and that PHP Mailer will embed these images automatically?
I don't think what you are trying to do is possible (automatically adding required images) with PHPMailer "from scratch".
Maybe you could parse the HTML to get the list of images it links to ? There are at least 2 ways of doing that :
using rexexes ; generally not the best way to do things if you're trying to extract data from HTML -- and you are ^^
another (probably) better way would be using DOMDocument::loadHTML on the HTML content loaded from your file (the output of file_get_contents), and work with the methods of DOMDocument (and, why not, DOMXPath ?)
To get the images, I suppose you have <img> tags in your e-mail, with src attributes ; the goal is to find the values of those ones ;-)
Once you have the list of images path, you iterate over them, and call $mail->AddEmbeddedImage on each one of them.
I suppose that would work just fine (haven't tried though, but I don't see why it wouldn't).
As a side note, the other way would be to keep the images on your server for a couple of days/weeks/months, and not include them in the mail ; it would make the mails smaller, which means :
less bandwith spent on your side (if your are sending lots of mails, it can represent quite an amout of bandwith)
less bandwith spent on each one of your clients' side -- which is nice for them too ^^
less space used on their hard disks ;-)
And to make sure the images don't get erased / replaced on the server before a couple of weeks/month, you can create a new directory for each newsletter, containing it's images -- and once in a while, delete the directories older than X days.

Categories