PHP sendmail() formatting not preserved - php

I am creating a web application for a client that has the ability to send out emails. I am using TinyMCE for my text editor, which works quite nicely. I am using sendmail() with PHP Swiftmailer to handle the actual sending of the email. Swiftmailer works nicely, as well.
The only problem that I am running into is that when I receive the email (in Gmail), the formatting is not rendered correctly. I am receiving the following in the body of my email:
<p>Oh Hello! This is a test <strong>message</strong>. Here is a link: Google.</p>\r\n<p> </p>\r\n<p> </p>\r\n<p> </p>\r\n<p>Line breaks!<br /> <br /> Shift breaks!</p>\r\n<p> </p>\r\n<p>Bye!</p>
The links render, and that's about it. What am I missing?
Thanks!

Check out the second and third code blocks in the documentation for how to set the HTML Content-Type...
http://swiftmailer.org/docs/header-parameterized

You need to send your e-mail in an actual HTML format. You are instead sending the plain text of the HTML. The links render because Google is nice and automatically links what it believes to be a valid URL.
I strongly recommend the Pear mail class. There are convenient functions for setting HTML and plain text message bodies.

What I would do in your case is something like this:
<?php
$headers = "From: Me <me#myemail.com>\r\n";
$headers .= "X-Mailer: PHP/".phpversion()."\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; //States it is HTML Content
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
$subject = "This is my subject";
$message = <<<MESS
<p>Oh Hello! This is a test <strong>message</strong>. Here is a link: Google.</p>
<p> </p>
<p> </p>
<p> </p>
<p>Line breaks!<br /> <br /> Shift breaks!</p>
<p> </p>
<p>Bye!</p>
MESS;
if(mail("", $subject, $message, $headers) == True){
echo "Message Sent";
} else {
echo "Message NOT Sent"
}
?>
NOTE: GMAIL is REALLY picky about how it displays emails. Best to create a gmail account and test for yourself how it looks. Hotmail is similar

Related

HTML to send a pre-written email

I have put a website up on the internet, and I want to have a function where a user can enter their email address into a form and when submitted, they will receive a pre-set email.
There are other questions, but none of them seemed to help me.
I have basic code here:
<form action="send.php" method="post">
<input type="text" name="mail">
<input type="submit" value="send">
</form>
Here is the send.php
<?php
$mail = $_POST['mail'];
?>
<html>
<button>SEND</button>
</html>
Please excuse the fact that you have to press send twice, I will change that.
What I want to do, is send the user (email is represented by $mail) a pre written document via email, not as an attachment, but a the composition of the email.
I am unsure as to weather i can just use a word document, or there is complex code behind it.
Since i am only familiar with basic visual elements, please could you explain what parts of the code do what, as i may want to tweak it.
Thanks!
As Fred said, the best way to approach this is via PHP's mail function. When send.php runs, you can include PHP code like
mail($mail, "My Email Subject", "<html><b>Email Message</b></html>", $headers);
Where your header variable will have to contain all the necessary information to tell your client's mail software that the string message contains HTML that should be parsed. An example header setup is as follows, from CSS Trick's lovely example for this problem:
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
Noting that you can replace the strip_tags call with whatever set emails you want. Then, you can replace my <html><b>Email Message</b></html> with whatever code you want to make a nicely formatted email.
Using PHP's mail() function also makes it so you do not need to click submit twice.

HTML Tags are not rendering in the Gmail - Using PHP language

I have used used the PHP Language for Sending the email and HTML tags are not rendering in the gmail account sometimes.I have used Content Type as - charset="iso-8859-1" I have attached the image.
And also am receiving the Message ID, which should not be come in the mail.
I don't recommend/use php built in mail() function to send and receive emails. Use php open source libraries like PHPMailer and SwiftMailer. I have been using PHPMailer after facing many issues when using mail() alone. Very easy to implement and has lots of features. You don't have to spend your valuable time for email sending related development.
http://phpmailer.worxware.com/
http://swiftmailer.org/
If the HTML isn't rendering, that usually means the headers weren't set properly.
Try this:
<?php
//change this to your email.
$to = "m#m.com";
$from = "m#m.com";
$subject = "This is HTML email";
$message = "
<b>htmlemail....</b> <br>
<a href='http://www.google.com'>Google</a>";
// To send the HTML mail we need to set the Content-type header.
$headers = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=iso-8859-1rn";
$headers .= "From: $from\r\n";
mail($to, $subject, $message, $headers);
echo "Message sent..";
?>
From http://www.webhostingtalk.com/showthread.php?t=416467

php mail() function not sending mail when I use text/html

So when I execute this without modifying the header information to send html type emails it sends however when I have it send with it being an html email it never gets sent.
here is the code:
<?php
$to = "someone#gmail.com";
$subject = "Order Confimation - mywebsite.com";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: <contact#mywebsite.com>";
$message = "
<!DOCTYPE html>
<html lang='en-us'>
<head>
<meta charset='utf-8'>
<title>Order Confirmation</title>
<style type='text/css'>
//style information
</style>
</head>
<body>
<div class='box'>
<h1 class='right'>Thanks!</h1>
//Blah blah blah
</div>
</body>
</html>
";
mail($to, $subject, $message, $headers);
if (mail($to, $subject, $message, $headers)) {
echo "<p class='hide'>E-mail Sent</p>";
} else {
echo "<p class='hide'>Problem</p>";
}
?>
And it returns E-mail sent however the email never goes to the inbox
Any suggestions?
Also pear package is enabled.
There's nothing wrong with your code as written above (with the exception that you call mail() twice - once outside the error check loop, once inside the 'if' - so you send 2 emails)
A quick test delivered fine to a Google Apps email address, so there's nothing about the code that specifically should be causing problems.
Spam filtering is a cumulative game. There can be multiple small things wrong none of which mark you as spam, but cumulatively tip your score over the limit.
In this case, you're sending a solely HTML email without a text/plain component. This is a negative mark against your score which seems to be acting as the straw to break the camel's back. If you're sending from a shared host with poor reputation you may get a few more points against you, and your PHP mail settings may be pushing an invalid return-path or other origin-based error which also count against.
You could try sending a multipart with both text/plain and text/html per http://krijnhoetmer.nl/stuff/php/html-plain-text-mail/
If that doesn't work, then if you post the full headers of the successful plaintext email then I might be able to see if you have other indicators.
Gmail will accept most mail, but just blackhole it after acceptance, so if you're using that to test you won't get much feedback.
Email's a tricky game, and if you're needing to get delivered for your app's success, you may want to consider using a third party service:
http://SendGrid.com
http://PostageApp.com
http://PostmarkApp.com
These services are designed around sending event-driven email from apps and handling all the mess on the ISP delivery end.
Full Disclosure: I am the Deliverability guy at PostageApp
I noticed you don't have
$headers .= 'To: ';
Could that have something to do with it?
I think you need to include the SMTP settings for outgoing mail if you are using mail function in php. So technically your mail will be reported as sent but would not have gone through the mail provider.
(I know you need to do that in java when using the mail function.)
mail function returns true, so you should say :
// mail() function return 0 if all right, so compare this with 0
if (mail($to, $subject, $message, $headers)) {
echo "E-mail Sent";
} else {
echo "Problem";
}
Not :
if (mail($to, $subject, $message, $headers)==0)

How to test if email is send (or received)?

below is a part of a website where orders are sent to a small a computer that prints the email out directly on a receiptprinter.
The problem is that sometimes (1 in 50 times) the email is never printet, even though you are send to the kvittering.php page.
Any ideas of how I can make a test, that only forwards you to the kvittering.php if the email is 100% sent?
Then I can eliminate the website as a source of error and focus on the printer.
Any advice is welcome.
<?php
$headers = "From: www.testsite.dk \r\n";
$headers.= "Content-Type: text/plain; charset=iso-8859-1 ";
$headers .= "Content-Transfer-Encoding: 8bit ";
$body = "$name_field\nTel. $phone_field\n$email_field \nVil gerne bestille følgende:$menucard_to\nMed følgende ændringer\n $message";
header("Location: $redirect_field");
mail($to, $subject, $body, $headers);
?>
<script>
<!--
window.location= "kvittering.php"
//-->
</script>
orders are sent to a small a computer that prints the email out directly on a recieptprinter.
I'd strongly suggest you to choose one of hundreds more convenient and secure protocols, such as FTP, SSH XML-RPC, HTTP or any other.
If you can't you have to:
study the particular mailserver serviong this "small a computer"
obtain one of hundreds PHP SMTP scripts interacting with SMTP manually and use it instead of mail().
study it's return values and check them with your sending code
Probably, you can try embedding 1px image in e-mail and point its path to a PHP script which should trace opening an email (which means successful delivery) and deliver 1px blank PNG. You can find information on such READTAG on the net.
Hope it will help you.
Use PHPMailer class at first and connect to SMTP server some how. Maybe you can get an access to same SMTP where the printer is connected. And move header() function after mail() function.
EDIT: Here is the article about redirects.
The most basic way to check if the e-mail has been sent is just:
<?php
$headers = "From: www.testsite.dk\r\n";
$headers.= "Content-Type: text/plain; charset=iso-8859-1\r\n";
$headers.= "Content-Transfer-Encoding: 8bit";
$body = "$name_field\nTel. $phone_field\n$email_field \nVil gerne bestille følgende:$menucard_to\nMed følgende ændringer\n $message";
if(mail($to,$subject,$body,$headers))
{
header("Location: $redirect_field");
echo'
<script>
<!--
window.location= "kvittering.php"
//-->
</script>';
}
else
{
echo'Error...';
}
?>

What to include in email headers, using php mail()?

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/

Categories