Sending HTML Email Using PHP - php

I am trying to send html email using php. it is working fine on gmail, yahoo etc. but on my cpanel (Horde Email Client) the text is comming as an attachment and it is not showing any body.
These are the headers i am using
$headers = "From: " ."XYZ<noRelpy#xxx.com>" . "\r\n";
$headers .= "Reply-To: ". "noRelpy#xxx.com" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=us-ascii" . "\r\n";
I have also tried changing the charset to iso-8859-1 but still no sucess

Sending emails is hard, and fraught with serious security problems (mostly related to spam). It takes years to write code that sends email reliably.
Instead of trying to do it yourself, this is a place where a third party library makes sense.
We have been using PHPMailer for years: http://code.google.com/a/apache-extras.org/p/phpmailer/

It is difficult to write custom code that sends HTML e-mail, especially ones with embedded images or attachments. I have written a class to do this, but once you do, you realise there are a bunch of other features you need.
Using a third-party library really is a better choice here if your core product is not a mailing library. PHPMailer is good, another good option is
swiftmailer
Good luck!

Related

Is there an e-mail template file like *.oft, which can be opened in all e-mail programs?

Currently I'm writing a web interface which is full of data. I would like to export this data as an e-mail template, so you can edit the e-mail afterwards.
Is there a format such as *.oft, which can be read by all email programs?
I know that there is such a function in HTML (<a href="mailto:[...]">). Since the e-mail text is very long and I want to attach files, this doesn't seems to be a good solution.
Can someone help me?
There's no such format as you're looking for. The internet engineers who design email protocols and format concern themselves with what goes out over the network, but not with what's internal to the machine. Email is highly interoperable when sending and receiving messages as a result, but there's no analogous standards body for internal API's. Sometimes there are de facto standards that arise from a imitating the behavior of popular piece of software, but that hasn't happened for the email feature you're looking for.
Maybe you mean .EML files? I remember that this file can be opened with nearly all e-mail clients such as Outlook, Thunderbird etc.
As far as I can tell you can go one of two ways:
1 - Use a prebuilt solution such as the one by these guys at postageapp.com their API looks pretty simple and does everything you want to achieve.
2 - More time consuming, but you can create your own templete using PHP. I would go for something along these lines. This is a very simple way of doing it. You could write your own CSS in the head and go nuts. Just remember that tables are king when it comes to HTML email :) Once the template has been written, you could just pass through the $content to fill it....
sender.php
<?php
$email = "Hello, \n";
$email.= "Very <strong>impressed</strong> with your email! \n\n";
$email.= "Faithfully\n";
$email.= "Mr Example\n";
$to = "me#me.me"; // <-- Set this as yours for testing...
$from = "someone#nice.me";
$subject = "An email";
$headers = "From: Someone nice <" . $from . ">\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= nl2br($email); // <---- if your email contains newline characters - it will convert them to <br />
$message .= '<br />www.example.com';
$message .= "</body></html>";
mail($to, $subject, $message, $headers);
// ADD LOTS OF PROTECTION IF NOT HARD CODING :)
?>
To extend this - if you for example use Gmail, then you could use Google's SMTP to send it - so that the email wouldn't say 'Sent on behalf of'.
Just create a email using HTML without CSS. Any data or binary can be passed through PHP and then emailed to the recipient.
To get you started, Create a file using fopen();. PHP has other functions that would allow you to edit, open, search&replace and even convert a file to a format of your choice. This is equivalent to a .oft file if not better, because all emails are be sent in text or a html equivalent regardless of the email client.
If you'd would like some more info on how to create this, let me know.

how to send embed images in email using php and html

i know this question is asked several time and it is basic question but i searched last three days but i am still unable to send images in email.please help how can i embed images in my email that is which headers should i include in headers
i write the following code.
$to = "example#gmail.com";
$subject = "new account created";
$body = "New account has been created
<img border=’0' src=’http://www.example.com/myimages/track.php?MID=1111&SID=2222' width=’1' height=’1'>
" ;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: admin#gmail.com";
$neww=mail($to,$subject,$body,$headers);
The reason you can't see your images in email is most email readers stript them out since they can be used for verifying valid email addresses which is a common tactic used by spammers. Your code screams tracking which only proves the point.
There is no way around this as the image filtering is done by the email clients and cannot be controlled by any code you write.
Check out this thread.
I posted a solution whereby you could use Mozilla Thunderbird to fetch the html image code for you which you can place in your script. The image will be embedded and would depend on no links or external content.
Look for my answer with the big screenshot of a lizard.
But, even despite that, users can still block images of all kinds. But emebedded images might be considered more acceptable than linked content - it depends on the client as to how images are handled.

PHP email style

So on a website I am working on there is an automated email sent when you register. When it sends it shows the css.
$body '<span style="color:#444444;">Header</span>';
I also tried this code that I found online that had style=\"asdas\" then used a command to remove the slashes but that didn't work either. Is there a simple php code that will just simply embed the html in the email?
First, make sure you put the $body = as #JamWaffles suggests.
Next, are you setting your headers correctly?
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
You can find a pretty detailed tutorial here: http://css-tricks.com/sending-nice-html-email-with-php/
It shows importance of headers, HTML vs txt email and user desires. It should be a good starting point for you.
You can also add a tld check in your code and assume they would have an html capable email client/web interface(gmail/yahoo/live etc) or you can just ask them to provide you with this information and then send them the emails in the format they have asked you to.

PHP output buffering not working

I have a php file that I am trying to send the output in a email using the php mail function (PHPMailer is not an option as the server I am working restricts their SMTP server). The code for the mail function is
$to = "xxx#example.com";
$subject = "Outdoor Grill Service Request";
ob_start();
require 'grill-form.php';
$body = ob_get_clean();
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers = "From: xxx#example.com\n";
mail($to,$subject,$body,$headers);
echo "Mail sent to $to";
grill-form.php is a php file that contains a html file that has tables that are populated with php variable from a form. This worked perfectly using PHPMailer but once I migrated over to standard php mail is got "screwy".
The issue I am running into is when the email sends I am getting raw HTML code and not the output of the grill-form.php (a styled table with values). I have little knowledge with the php mail function so I might be missing something stupid.
Was wondering what I am doing wrong. Thank you in advance for you help you people are the best.
Assuming that's your actual code, you're overwriting the $headers variable with the third declaration, not appending to what you have. The Content-type header is never making it in.
PHPMailer does support SMTP, by the way--what exactly do you mean by "restricts their SMTP server"? (Here's an example: http://phpmailer.worxware.com/index.php?pg=examplebsmtp)

What do I need for a compliant email header

I am trying to send an email from a site I am building, but it ends up in the yahoo spam folder. It is the email that sends credentials. What can I do to legitimize it?
$header = "From: site <sales#site.com>\r\n";
$header .= "To: $name <$email>\r\n";
$header .= "Subject: $subject\r\n";
$header .= "Reply-To: site <sales#site.com>" . "\r\n";
$header .= "MIME-VERSION: 1.0\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$phpversion = phpversion();
$header .= "X-Mailer: PHP v$phpversion\r\n";
mail($email,$subject,$body,$header);
Don't use HTML in your email.
Send it via a legitimate mail server with a static IP and reverse-DNS (PTR) that points to the machine's real host name (and matches a forward lookup).
Include a Message-ID (or ensure that the local mailer adds one for you).
Run your email through SpamAssassin and see which bad-scoring rules it matches. Avoid matching them.
Use DomainKeys Identified Mail to digitally sign your messages.
I just successfully tried the following from my Yahoo! Web Hosting account:
$email = "me#site.com";
$subject = "Simple test";
$body = "Simple test";
$header = "From: site \r\n";
$header .= "To: $name \r\n";
$header .= "Subject: $subject\r\n";
$header .= "Reply-To: site " . "\r\n";
$header .= "MIME-VERSION: 1.0\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$phpversion = phpversion();
$header .= "X-Mailer: PHP v$phpversion\r\n";
mail($email,$subject,$body,$header);
However, you have some duplication in your header you should only need to do the following:
$email = "me#site.com";
$subject = "Simple test";
$body = "Simple test";
$header = "From: site \r\n";
$header .= "MIME-VERSION: 1.0\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$phpversion = phpversion();
$header .= "X-Mailer: PHP v$phpversion\r\n";
mail($email,$subject,$body,$header);
In addition to Ted Percival's suggestions, you could try using PHPMailer to create the emails for you rather than manually building the headers. I've used this class extensively and not had any trouble with email being rejected as spam by Yahoo, or anyone else.
There is also the possibility that 'sendmail' (which is underneath the PHP mail() function) needs extra parameters. If you have a problem with return headers (such as Return-Path) not being set with what you set them to be, you may need to use the fifth mail() parameter. Example:
mail('recipient#domain.com', 'Subject', $mail_body, $headers, " -f sender#domain.com");
There is some further evidence that true vanilla sendmail may have problem with this! Hopefully you have 'postfix' as PHP's underlying mail() support on your target server.
In addition to Ted Percival's suggestions, make sure that the IP address the email is coming from is a legitimate source for email according to the SPF record of site.com. If site.com doesn't have an SPF record, adding one (which allows the IP address in question, of course) may help get the emails past spam filters.
And if absolutely do need to use HTML in your email, make sure that you also include a plain text version as well; you'd use the content type of "multipart/alternative" instead of "text/html".
Ted's suggestions are good, as are Tim's, but the only way I've ever been able to reliably get email through to Yahoo/Hotmail/etc is to use the PEAR email classes. Try those & (assuming your server is OK) I can pretty much guarantee it'll work.
Ted and Tim have excellent suggestions. As does Shabbyrobe. We use PHPMailer and don't have any problems with spam filters.
One thing to note is that many spam filters will count NOT having a text version against you if you are using a MIME format. You could add all of the headers and the text version yourself, or just let PHPMailer or the PEAR mail library take care of that for you. Having a text version may or may not help, but it is good practice and user friendly.
I realize that your code sample is just that - a sample, but it is worth saying: Do not ever just drop user provided data into your mail headers. Make sure you validate that it is data you expect. It is trivial to turn a php mail script into an open relay, and nobody wants that.
Check rfc 822 and rfc 2045 for email format. I find python's Email class really easy to work with. I assume php's PEAR does the same (according to earlier mails). Also the header and the body are separated by a "\r\n\r\n", not sure if your code automatically inserts that, but you can try appending that to the header.
I dont think that DK/SPF might be necessary (since there are lots of webservers out there without DK/SPF support). There can be alot of factors that might be causing it to get blocked(atleast 10K different criterions and methods.. p0f,greylisting,greylisting, blacklisting etc etc). Make sure that your email is properly formatted(this makes a BIG difference). Look into libraries that generate the complete header for you.. that way you have least chances of making any mistake.
Adding a SPF record is very easy. You should try.
This one is for dreamhost plus googlemail
You should also ad you webserver ip address (in my case, the line before googlemail)
The last line tells the server to do a soft reject (mark as spam but don't delete) I'm using it instead of "-" (delete) because google documentation says so :-)
It's a TXT record
v=spf1
ip4:64.111.100.0/24 ip4:66.33.201.0/24 ip4:66.33.216.0/24
ip4:208.97.132.0/24 ip4:208.97.187.0/24 ip4:208.113.200.0/24 ip4:208.113.244.0/24
ip4:208.97.132.74 ip4:67.205.36.71
include:aspmx.googlemail.com
mx ~all
Hope it helps

Categories