I've never done html e-mail before, I've just set up php mail using http://www.postmarkapp.com
I was wondering how I would go about sending php mail as html?
Does anybody have any previews of a php page sending html e-mail I can look at to get the jist of how it works?
Currently I'm just putting text into a variable and sending it as a message, how is it done for html?
Regards
Do you still want to use Postmark to send emails?
In Postmark, you set the TextBody property to the text version of your message, and the HtmlBody property to the html version of it. It is good practice to always include both. Depending on whether your user's email client supports HTML or not, the appropriate message form is rendered. Read more on this here.
Edit: Added an example. I generally like splitting my string into separate lines so that I can indent it nicely like a real HTML file. Of course, if you used templates, that would make it so much better.
$htmlBody = "
<html>
<body>
Thank you for using our app!<br />
- Super Awesome App Team
</body>
</html>
";
http://phpmailer.worxware.com/
This is the best class I've found to send mail with PHP. It allows HTML formatting with alternate plain text part, as well as attachments. It also seems to filter out spam quite well when used for online forms.
Put the html code inside the variable where you need as you are doing while creating web pages and set the mail header to text/html.
$headers.= 'MIME-Version: 1.0' . "\r\n";
$headers.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers.= "From: eaxfd#gmail.com \r\n";
$headers.= "Reply-To: eaxfd#gmail.com \r\n";
mail($to,$subject,$message,$headers);
You just have to include html headers in your call to the mail() function:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1';
mail($to, $subject, $message, $headers);
Related
i want to use click able link in email, but it is not reflecting in email sending through php mail function, below is my code
$url = "<html><a href='www.google.com'>Link</a></html>";
$message = "Hi test url ".$url." ";
$$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Admin <test#ex.com> \r\n";
#mail('test1#ex.com',$subject,$message,$headers);
Content which i'm getting from email:
Hi test url <html><a href='www.google.com'>Link</a></html> ##NEW##
You don't seem to be sending HTML mail correctly. Usually I will recommend using a third-party PHP library, like SwiftMailer, to simplify the process.
Edit: If you still wish to use php's mail() function, you will have to specify the headers correctly. This article could help.
To use the HTML in phpmailler:
$mail->isHTML(true)
You review the documantation
phpMailler
I am using php mail method to send HTML mails to people from my domain. I am using the following code to send mail
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type:text/html; charset=iso-8859-1' . "\r\n";
mail($id, $subject, $message,$headers);
However, the problem is that the code doesnt work when the size of message increases. It works fine for smaller messages.
Is there any way to overcome that ??
I resolved my issue. It was due to less number of breaking tags br in the html code. I introduced some br tags here and there and it flew :)
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Php mail: how to send html?
formatting email in php?
I'm using PHP to send emails, like this:
$to = $emailAddress;
$subject = "qwerty";
$message = "foo" . PHP_EOL . "bar";
$headers = 'From: "asdf" <email#email.com>';
mail($to, $subject, $message, $headers);
When I attempt to put HTML code within the message, it just sends the actual HTML as plaintext. This leads me to ask - how can I format emails as I can format webpages?
Make sure you're setting your headers for text/html, like so:
// 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";
See Example #4 in the PHP Manual for the full example.
To add style to your email, you first need to add a header as mentioned by jmort253, like this:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Once you have added this to you header. You can do styling in the actual message, ie:
$message = "<html><body><h1>Welcome</h1><p>Thanks for the email</p></body></html>";
Make sure that you do inline CSS or put it in the header, don't put it in an external CSS file. As for images, you'll need to include full paths to your server, so that the client mail application can find the image on the web.
Hope this helps.
I recommend not reinventing the wheel. Instead use phpmailer class or something similar. Will solve many other upcoming problems as well :)
I am currently processing a form with text and making an email out of the text.
I am facing two problems:
1) If I just take the text as is, I get \n\r that appear in the email whenever the person uses line breaks.
2) If I use nl2br() function on the input text, I get strings in my email.
Is there a correct or "best practice" way handling such situations?
Thanks,
Alex
You should add appropriate headers to your email
// To send HTML mail
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
before using nl2br()
// Then mail it
mail($to, $subject, nl2br($message), $headers);
When I want to send email, my codes look like these:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: me#localhost' . "\r\n";
// Email Variables
$toUser = 'you#something';
$subject = "Testing";
$body = '<html><head><title></title></head><body>
This is my picture <img src="http://domain.com/me.jpg">';
if(mail($toUser,$subject,$body,$headers)){
echo "Email sent!!";
}
Here are my questions:
Must I include the <html>, <head>, <title>... tags?
Can I use stylesheet? For example, <div style="..."> ?
I tested send the email to several accounts, if I include the <img> tag (for include image), it goes to junk mails, but if just plain text it goes to inbox. Anyone has any idea why it is? And how to solve it?
You should send your e-mail as multipart with a text/plain portion too.
That'll let people with text-only mail clients read it, and also count in your favour with any spam detection systems.
No, some mail clients (especially webmail) will strip out anything before <body>
You can use inline styles <div style=""> - using an external .css file is not recommended as it may not be loaded.
If you only have a bit of text and an image it looks a lot like spam. If you had more text it may get treated differently.
The problem with css in mail is that (again) is not standard in all mail services
here will find a complete descripction
http://www.campaignmonitor.com/css/