PHP mail() attachment problems
Asked Answered
R

4

16

Can someone help me figure out why this keeps returning false?

$to = "[email protected]";
$from = "Website <[email protected]>";
$subject = "Test Attachment Email";

$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "document.pdf";

//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;

// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;

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

// send message
if (mail($to, $subject, "", $headers)) {
    echo "mail send ... OK";
} else {
    echo "mail send ... ERROR";
}
Rosinarosinante answered 8/6, 2011 at 6:39 Comment(3)
I've seen this "put everything in $headers" a couple of days ago. Where do you guys copy paste it from?Bituminous
This manual header manipulation is a dirty solution. Personally i'm a fan of Zend_Mail, though i'd like to know of any other "smart" methods.Outstanding
@MattH., the Zend developers doesn't have to do this "dirty solution" too?Rafaelarafaelia
B
29
$to = "[email protected]";
$from = "Website <[email protected]>";
$subject = "Test Attachment Email";

$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "document.pdf";

//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// send message
if (mail($to, $subject, $body, $headers)) {
    echo "mail send ... OK";
} else {
    echo "mail send ... ERROR";
}

What I've done:

  • Separated the body from the headers
  • removed extra EOLs put before separators
Bituminous answered 8/6, 2011 at 6:48 Comment(7)
pretty clear, despite of other proposed solutions, this worked!Meditation
Didn't work for me. I just change the $to and $filename variables. The e-mail is sent, the body message and the attachment it's there, but the file came broken. I tried with a ".zip" and ".txt".Rafaelarafaelia
@Alin Purcaru: Above code is not working for me, its just sending a file with filename, contents are missing in that file.Mediacy
I tried this code every thing is working find but attached file become empty when I download it from emailAerify
@MoazAteeq please define $pdfdoc variable absolute path. This is working fine for me.Feu
Thanks a lot. this is working fine. I have another problem. Can you please help me - my file path is $filename = "http://www.example.com/csv/document.csv"; This is working fine. but in gmail I want to show Test File instead of http://www.example.com/csv/document.csv Is it possible?Feu
Thanks.. mail is sent with attached pdf file but it is not open. There was a problem previewing this document. this message is open in gmail. I used absolute path for file. The path is $_SERVER['DOCUMENT_ROOT']."/ticket/ticket.pdf" is it wrong?Jamajamaal
C
3

I was also facing issue and found a neat answer. You can use this function for multiple attachment.

function mail_attachment($files, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message, $cc, $bcc) 
{
    $uid = md5(uniqid(time()));
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "cc : < $cc > \r\n" ;  // comma saparated emails
    $header .= "Bcc :  < $bcc >\r\n"; // comma saparated emails
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";

    foreach ($files as $filename) 
    { 
        $file = $path.$filename; // path should be document root path.
        $name = basename($file);
        $file_size = filesize($file);
        $handle = fopen($file, "r");
        $content = fread($handle, $file_size);
        fclose($handle);
        $content = chunk_split(base64_encode($content));

        $header .= "--".$uid."\r\n";
        $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
        $header .= $content."\r\n\r\n";
    }
    $header .= "--".$uid."--";
    return mail($mailto, $subject, "", $header);
}

Hope it helps.

Chaddy answered 15/3, 2013 at 10:59 Comment(1)
Looks a lot like Brian's solution from #9520088Cumulation
T
0

These are the headers I use in my script

$base = basename($_FILES['upload']['name']);
$file = fopen($randname_path,'rb');
$size = filesize($randname_path);
$data = fread($file,$size);
fclose($file);
$data = chunk_split(base64_encode($data));

//boundary
$div = "==Multipart_Boundary_x".md5(time())."x";
//headers
$head = "From: $from\n".
        "MIME-Version: 1.0\n".
        "Content-Type: multipart/mixed;\n".
        " boundary=\"$div\"";
//message
$mess = "--$div\n".
        "Content-Type: text/plain; charset=\"iso-8859-1\"\n".
        "Content-Transfer-Encoding: 7bit\n\n".
        "$message\n\n".
        "--$div\n".
        "Content-Type: application/octet-stream; name=\"$base\"\n".
        "Content-Description: $base\n".
        "Content-Disposition: attachment;\n".
        " filename=\"$base\"; size=$size;\n".
        "Content-Transfer-Encoding: base64\n\n".
        "$data\n\n".
        "--$div\n";
$return = "-f$from";

http://asdlog.com/Create_form_to_send_email_with_attachment

Treatise answered 10/8, 2012 at 22:7 Comment(0)
R
0

Like i commented in the @GovindTotla's answer, his solution has worked for me. For a matter of completeness, below is the output code of the $header with two attachments. Consider it a reverse engineered answer:

From: [email protected]
Reply-To: [email protected]
Cc: [email protected]
Bcc: [email protected]
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="2527518bb813d2f2847a2adba8b8b47f"

This is a multi-part message in MIME format.
--2527518bb813d2f2847a2adba8b8b47f
Content-type:text/html; charset=iso-8859-1
Content-Transfer-Encoding: 7bit

Here comes the message.

--2527518bb813d2f2847a2adba8b8b47f
Content-Type: application/octet-stream; name="test.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test.txt"

U29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBlbHNlLiBTb21ldGhpbmcgZWxzZS4gU29tZXRoaW5n
IGVsc2UuIFNvbWV0aGluZyBlbHNlLiBTb21ldGhpbmcgZWxzZS4gU29tZXRoaW5nIGVsc2UuIFNv
bWV0aGluZyBlbHNlLiBTb21ldGhpbmcgZWxzZS4gU29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBl
bHNlLiBTb21ldGhpbmcgZWxzZS4gU29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBlbHNlLiBTb21l
dGhpbmcgZWxzZS4gU29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBlbHNlLiBTb21ldGhpbmcgZWxz
ZS4gU29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBlbHNlLgo=


--2527518bb813d2f2847a2adba8b8b47f
Content-Type: application/octet-stream; name="test2.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test2.txt"

VGhpcyBpcyB0aGUgY29udGVudCBvZiB0aGUgc2Vjb25kIGF0dGFjaG1lbnQuIFRoaXMgaXMgdGhl
IGNvbnRlbnQgb2YgdGhlIHNlY29uZCBhdHRhY2htZW50LiBUaGlzIGlzIHRoZSBjb250ZW50IG9m
IHRoZSBzZWNvbmQgYXR0YWNobWVudC4gVGhpcyBpcyB0aGUgY29udGVudCBvZiB0aGUgc2Vjb25k
IGF0dGFjaG1lbnQuIFRoaXMgaXMgdGhlIGNvbnRlbnQgb2YgdGhlIHNlY29uZCBhdHRhY2htZW50
LiBUaGlzIGlzIHRoZSBjb250ZW50IG9mIHRoZSBzZWNvbmQgYXR0YWNobWVudC4gVGhpcyBpcyB0
aGUgY29udGVudCBvZiB0aGUgc2Vjb25kIGF0dGFjaG1lbnQuCg==


--2527518bb813d2f2847a2adba8b8b47f--
Rafaelarafaelia answered 14/10, 2013 at 23:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.