Julo0sS
Julo0sS

Reputation: 2102

PHP mail() function ... empty message sent

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);

Upvotes: 2

Views: 1937

Answers (2)

Sela Yair
Sela Yair

Reputation: 284

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.

Upvotes: 1

Spencer May
Spencer May

Reputation: 4505

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>';

Upvotes: 0

Related Questions