So I am trying to send a fairly simple HTML email using PHP. I have spent the last three days trying to find a good solution and think I found one, but when I test it, it doesn't send properly. I borrowed this code from one of the tutorials I was referencing. The test code is as follows:
<?php
//define the receiver of the email
$to = 'myemail#gmail.com';
//define the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
The problem is that, although it sends the email just fine, it either sends it as plain text, or a blank message in Gmail. Any ideas why?
I should of course tell you to use a library for this, such as swift, but I think this is a good exercise so I'll tell you what's wrong with it :)
It's because your line endings are wrong. Unless otherwise specified, line endings in email messages are CRLF (\r\n), whereas your ob_start() block is likely to only have LF (\n) as the line separator.
This causes GMail to misinterpret the email message and shows nothing instead. In my case it shows an empty download file ;)
Related
here's (all) the code:
<?php
/*
WHITE SPACE MATTERS IN THIS DOCUMENT
*/
include("_php/ChromePhp.php");
// define receiver of email
$to = "person#place.com";
// define subject of email
$subject = "<--== KABAM! HTML Email from WR! ==-->";
// define message to send. use /n for linebreaks
//$message = 'This is the message. Read it. Love it.';
// create a boundary string. it must be unique!
$uniqID = md5(date('r', time()));
// define the headers. separated by \r\n
$headers = "From: some dude" . "\r\n";
$headers .= "Reply-To: nobody" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$uniqID."\""."\r\n";
// read the attachment into a string, encode it and then
// split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('./_dox/pdftmp/emailTESTER.zip')));
// define the body of the message
ob_start(); // turn on output buffering
?>
--PHP-mixed-<?php print $uniqID; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php print $uniqID; ?>
--PHP-alt-<?php print $uniqID; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
This is the TEXT email.
Nothing but pure text. not really fun...
--PHP-alt-<?php print $uniqID; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h1>This is the HTML test email.</h1>
<h3>Read it. Love it.</h3>
<p>this is all HTML. without any CSS, mind you...</p>
<?php include("_php/formEmail.php"); ?>
--PHP-alt-<?php print $uniqID; ?>--
--PHP-mixed-<?php print $uniqID; ?>
Content-Type: application/zip; name="emailTESTER.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php print $attachment; ?>
--PHP-mixed-<?php print $uniqID; ?>--
<?php
// copy current buffer contents into $message then delete
// the contents of the output buffer
$message = ob_get_clean();
// send the email
$mail_sent = #mail($to, $subject, $message, $headers);
// display a message depending on mail_sent status
print $mail_sent ? "Mail Sent: ".$uniqID : "Mail Failed: ".$uniqID;
?>
and this is what pops out in the email client: (doesn't render...)
This is the TEXT email.
Nothing but pure text. not really fun...
--PHP-alt-a0d18dbf6c6ec8fb30c47adc84234c75Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h1>This is the HTML test email.</h1>
<h3>Read it. Love it.</h3>
<p>this is all HTML. without any CSS, mind you...</p>
--PHP-alt-a0d18dbf6c6ec8fb30c47adc84234c75--
the attachment, instead of being "emailTESTER.zip" is simply "Part 2" and no extension. if i add '.zip', it becomes the correct archive (albeit misnamed) with the correct contents...
i triple checked the boundary lines, i believe they are correctly set. the only thing that i could think of would be something in the Content-Type declarations...but if it is, i'm blank as to what it might be... i've read all the prior posts on "PHP email HTML blah blah" and while they lent insight, none of them touched on my odd pair of hiccups. narf
so. what did i miss? why is it not working correctly/completely?
TIA.
WR!
Don't reinvent the wheel. Use an existing mail class. PHPMailer is excellent.
I have an issue with some emails that are being sent from my PHP application.
The emails are being received blank and they are all getting sent to a specific address. These emails are also CC'ed to an address i have control over and they are all received correctly. All the emails are sent in HTML.
I do not have control over the remote email address that these emails are sent to, but i am 100% sure they go through some kind of M$ exchange spam filter. Could it be the filter that is causing the data to go missing?
Any other thoughts on this would be great!
You say all emails are sent in HTML. You may need to send plain text to go with it - to cater for those email clients that dont support HTML. http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php.
This is a copy-paste from the above link:
<?php
//define the receiver of the email
$to = 'youraddress#example.com';
//define the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
WITHOUT using PHPMailer, Swiftmailer, PEAR, Zend_mail, or any other libraries at all, I want to send an email with an image attachment inline.
The important part here is attaching it inline: I already am able to do everything else.
Inline meaning that it is able to be used by the HTML in the email in an image tag.
I really don't want to use PHPMailer or anything like that--I am not the only one who has tried to figure out how to do this on stackoverflow, and so far all the questions I've seen get nothing but arguments about why they should be using PEAR or Zend_mail or something. I don't want to do that, and I don't want to argue about it.
is this what you are looking for??
<?php
//define the receiver of the email
$to = 'youraddress#example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
I don't know if this is what you want, but is easy to send an email with mail() function in PHP, in HTML format:
http://php.net/manual/en/function.mail.php
In the examples, there's one with HTML
i want to be able to send mails using php. i have no experience in this. i got some scripts like http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php. I want to send a html mail which needs to be formed using data from a mysql database. I have email addresses stored in a database and want to design a script to get addresses from it and call mailing function to form accordingly html message and send it
Please help me, give me any method to send html mails formed using database data and which can be called in the form of a function so tht i can automate sending
I HAVE ADDED THE CODE I AM USING PLEASE TELL ME THE ERROR IN THIS...WEN I RUN THIS SCRIPT AN EMPTY MAIL WITH AN EMPTY ATTACHMNT IS SENT TO MY ID
<?php
//define the receiver of the email
$to = 'mymail#gmail.com';
//define the subject of the email
$subject = 'hi there';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: blah <contact#mydomain.com>\r\nReply-To: contact#mydomain.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
echo $random_hash;
echo '<br>Content-Type: text/plain; charset="iso-8859-1"
<br>Content-Transfer-Encoding: 7bit';
echo '<br>Hello World!!!
This is simple text email message.';
echo $random_hash;
echo '<br>Content-Type: text/html; charset="iso-8859-1"
<br>Content-Transfer-Encoding: 7bit ';
echo '
<br><span><iframe src="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fpages%2F89Deals%2F162906580388522&width=200&colorscheme=light&connections=6&stream=false&header=true&height=287" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:200px; height:287px;" allowTransparency="true"></iframe></span>gfhjfjhfjhjjgkgk<br>
</span>
</td>
</table>
</body> ';
echo $random_hash;
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();;
//echo $headers." ";
echo $message;
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
The script already is PHP, so there is no need to "convert it to PHP".
To understand the code you had problems with, you need to understand the output buffer. ob_start() tells PHP that everything that will be output by echo should not really be output but rather be put into a hidden buffer whose content thereafter is retrieved using ob_get_clean(). That becomes the contents of the mail.
So everything you echo between ob_start() and ob_get_clean() will be the contents of your mail.
Ironically, for an IT contractor, I find myself building a website for a recruitment consultancy.
They would like a form which allows users to send them a CV but, from a security point of view would like the CV's sent to an e-mail address rather than stored on a web server. Does anyone know if such a thing is possible with jquery forms plugin or some other type of script?
No luck so far with google etc.
Thanks in Advance.
This page explain everything from file upload to sending email with attachment
<?php
//define the receiver of the email
$to = 'youraddress#example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Let the upload proceed as normal into a temp folder on the web server, then email that file where it needs to go and then delete it.
Otherwise, it's just tell the user to email in their CV. I can't think of any way of redirecting the file stream into a mail message directly.