2

I try to send an email from my server to recipient, no problem, the email is sent. I have TEXT HTML in this email, and a PDF attachment...

The problem is: I receive the mail, I receive the PDF, but there is NO text!

Here is my code if you can help :)

$filepath = '../../path/file.pdf';
if (!preg_match("#^[a-z0-9._-]+@(hotmail|live|msn).[a-z]{2,4}$#", $email))
{
    $br = "\r\n";
}
else
{
    $br = "\n";
}
$message_html= "Content-Type: text/html; charset=\"ISO-8859-1\"".$br;
$message_html.= "Content-Transfer-Encoding: 8bit".$br;
$message_html.= '<!DOCTYPE html><html><head>
      <title>mytitle</title>
    </head>
    <body>
      <div>
      my content!
      </div

    </body>
</html>';

$boundary = "-----=".md5(rand());
$boundary_alt = "-----=".md5(rand());

$subject = "mysubject";

$attached_file = file_get_contents($filepath);
$attached_file = chunk_split(base64_encode($attached_file));
$pos=strrpos($filepath,"/");
if($pos!==false)$file_name=substr($filepath,$pos+1);
else $file_name=$filepath;
$pos=strrpos($filepath,".");
if($pos!==false)$file_type="/".substr($filepath,$pos+1);
else $file_type="";

$attached = "\n\n". "--" .$boundary . "\nContent-Type: application".$file_type."; name=\"$file_name\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"tickets.pdf\"\r\n\n".$attached_file . "--" . $boundary . "--";

//=====HEADER
$header = "To: ".$email." ".$br;
$header.= "From: frrrrrom".$br;
$header.= "Cc: cccccccc".$br;
$header.= "Reply-to: qdfqsf".$br;
$header.= "MIME-Version: 1.0".$br;
$header.= "Content-Type: multipart/mixed;".$br." boundary=\"$boundary\"".$br;
//==========

$body = $message_html.$attached;

mail($email,$subject,$body,$header);
3
  • 1
    There are many subtleties to sending email. Instead of constructing everything yourself I recommend using a free library, for example Zend Framework 2: stackoverflow.com/questions/17066078/… Commented Jun 25, 2014 at 14:26
  • thx for the reply but... well... even if it's not perfect... i'd like to do it myself... ^^ maybe it's boundary causing issue... anyone has an idea? Commented Jun 25, 2014 at 14:42
  • Check whether you can receive plain text. And in $message_html variable use single quotes (') instead of double quotes (") and try Commented Jun 25, 2014 at 14:43

2 Answers 2

1

What I would if I had to debug it, is to remove the attached PDF and check if it sends the text and then try to add it back.

Also you can try printing to your screen with var_dump the $body and the others vars and share it with us and then it'll be easier to spot the problem. As probably it's with what you send which doesn't match the format of email sending.

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

2 Comments

This is THE idea! :D i tried, and found that if i DONT attach the PDF file, the mail content (html) IS visible... BUT if i attach the PDF, there is no text displayed... where does this trick come from? how to solve this issue? is it a "content-type" mistake i did?
The problem was a missing "--".$boundary before the text... code wasnt clear enough to delimitate html or attachment... so it was one or another, not both! THANKS ! :)
0

I'm pretty sure that when you are sending an email in php, the text has to be set inline. Also you didn't have your closing div finished (you had </div), this may have caused you not to be able to see the text, but it was there.

$message_html.= '<!DOCTYPE html><html><head><title>mytitle</title></head><body>';
$message_html.= '<div>my content!</div>'; // The unfinished div
$message_html.= '</body></html>';

4 Comments

It's unlikely that a single missing > would cause nothing to be displayed, browsers are pretty good at fixing bad HTML jsfiddle.net/n4YVU
@JuanMendes if you have ever messed with automated html emails, you would know that html in emails is very different than in page html. It has a mind of its own.
I must admit, I don't have a lot of experience with HTML emails
Thanks for reply, but it is not an unclosed <div> problem... even with the good bracket, the mail is empty... but something interesting found, check the comment i'll leave to previous answer ;)

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.