I am trying to build a script (PHP email attachment) that works for all mailing Systems (Gmail, Yahoo, Outlook etc) The codes below work perfectly for Hotmail, but Gmail and others dont come through. What should i do to the codes below to fix this issue. Thanks!

5 Years
Discussion Span
Last Post by programmer12

The code thing is not working. Sorry... I will have to post it with inline

    $fname = $_POST['firstname'];
    $lname = $_POST['lastname'];
   $email = $_POST['email'];
    $month = date('F');//Month

     //define the receiver of the email
$to = $_POST['email'];
//define the subject of the email
$subject = 'Subject Line Here';
//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: From Goes Here\r\nReply-To: webmaster@hotmail.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
$fileatt = "newsletter/Apr2012.pdf"; // Path to the file
    $fileatt_type = "application/pdf"; // File Type
    $fileatt_name = "Apr2012.pdf"; // Filename that will be used for the file as the attachment
    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    $attachment = chunk_split(base64_encode($data));
//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

Plain text test here

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit


--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/pdf; name="Apr2012.pdf" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>
--PHP-mixed-<?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 );

Edited by diafol: fixed code tags

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.