1

Please help me, I am having problems with sending an html formatted mail message using php mail() I think the problem lies in the header. I have included two headers with a slight difference only in single quotes or double quotes:

header 1:

$headers = 'From: [email protected]\r\n Reply-To: [email protected]';
$headers .= '\r\nContent-Type: multipart/alternative; boundary="'.$random_hash.'"'; 

When I use single quotes like above, all my html code is printed in the mail as simple text without th proper html formatting. Also my header is shown all messed up with everything after \r\n missing.

header 2:

$headers = "From: [email protected]\r\nReply-To: [email protected]";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"".$random_hash."\"";

Using this one, I get a perfect header but now my mail is sent empty with an empty attachment. I don't where that comes from as I don't attach anything to my mail.

Please suggest what to do

5 Answers 5

5

If you use single quotes on your PHP strings escape characters like \r\n will stop working.

I'm not sure how to help with your attachment without more context.

Sign up to request clarification or add additional context in comments.

Comments

4

I don't like php mail. I recommend use XpertMailer: http://www.xpertmailer.com/ do an excellent work.

1 Comment

+1 nice library, haven't seen that one yet. Looks pretty good!
3
  • Use a library
  • Don't reinvent the wheel if you can't make it round.
  • Use a library!

1 Comment

I can't stand the phrase Don't reinvent the wheel, since it requires the assumption that the wheel is inherently perfect (and since when are any of the frameworks/libraries perfect)... But you're extension I think pushes the correct point home... +1
0

This is what I used before switching to phpmailer. As mentioned use a library.

// Make email headers
$separator = '--==Multipart_Boundary_'.md5(time());
$eol = PHP_EOL;

$filepath = "filename.pdf";
// open pdf file
$handle = fopen($filepath, "r");
// read pdf file
$f_contents=fread($handle,filesize($filepath));
// encode read pdf file
$attachment = chunk_split(base64_encode($f_contents));
// close pdf file
fclose($handle);
$message = "Text goes here";

// main header (multipart mandatory)
$headers = "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "X-Priority: 1 (Highest)".$eol;
$headers .= "X-MSMail-Priority: High".$eol;
$headers .= "Importance: High".$eol;

// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/plain; charset=utf-8".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol;

// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/pdf; name=".$filename.$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment; filename=".$filename.$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";

Comments

0

If you're not actually defining two versions (plaintext/HTML) with multipart boundaries then you should change the Content-type: multipart/alternative to the proper content-type for your mail body.

Additionally, libraries like PHPMailer, et cetera are generally preferred over PHP's native mail() function because they offer a great deal more flexibility while not requiring you to manually construct complex headers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.