PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,341 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,620 people online right now. Registration is fast and FREE... Join Now!




Sending Multiple Attachments with mail()

 

Sending Multiple Attachments with mail()

denewey

18 May, 2009 - 07:12 PM
Post #1

New D.I.C Head
*

Joined: 18 May, 2009
Posts: 6

I've got a piece of code that creates a function to send an e-mail with an attachment which is a html file. I have tried to also include another attachment which is an image that is embedded in the html file. The code for the individual attachment works fine. When I add the second attachement, the first attachment goes but not the 2nd. Below is the code:

CODE

function mail_attached($to, $from, $subject, $message, $filename, $headers = '')
    {
        $unique_sep = md5(uniqid(time()));
        $headers .= "From: $from\n";
        $headers .= "MIME-Version: 1.0\nContent-Type: multipart/mixed;boundary=\"$unique_sep\";\n";
        $headers .= "charset=\"iso-8859-1\"\nContent-Transfer-Encoding:7bit\n\n";
        $headers .= "If you are reading this, then your e-mail client does not support MIME.\n";        
        $headers .= "--$unique_sep\n";
        $headers .= "Content-Type: text/html; name=\"" . $filename[0]['filename'] . "\"\n";
        $headers .= "Content-Transfer-Encoding: 8bit\n";
        $headers .= "Content-Disposition: attachment; filename=\"" . $filename[0]['filename'] . "\"\n\n";
        echo "The file name1 is $filename[1]['filename']<br>";//DEBUG
        $filedata = implode("", file($filename[0]['file']));
        $headers .= $filedata . "\n\n";
        $headers .= "--$unique_sep--\n";
        $headers .= "Content-Type: image/jpg; name=\"" . $filename[1]['filename'] . "\"\n";
        $headers .= "Content-Transfer-Encoding: base64\n";
        $headers .= "Content-Disposition: attachment; filename=\"" . $filename[1]['filename'] . "\"\n\n";
        echo "The file name2 is $filename[1]['filename']<br>";//DEBUG
        $filedata = implode("", file($filename[1]['file']));
        
        $headers .= chunk_split(base64_encode($filedata));
        $headers .= "--$unique_sep--\n";
        mail($to, $subject, $message, $headers);
    }

    $date = time();
    $handle = fopen("./forms/form12b25-" . $date . ".htm", "w+");
    if($handle)
    {    
        fwrite($handle, $HTML);
        fclose($handle);
        $f_array_1 = array('file'=>"./forms/form12b25-" . $date . ".htm",'mimetype'=>"text/plain",'filename'=>"form12b25-" . $date . ".htm");
        $f_array_2 = array('file'=>"ballot-checked.jpg",'mimetype'=>"image/jpg",'filename'=>"ballot-checked.jpg");
        $file_array = array($f_array_1, $f_array_2);
        
        mail_attached("denewey@gmail.com", "12b25@edgarfilingagent.com", "form12b25 Submission", "form12b25 Submission Generated", $file_array);
                


Any idea what I've done wrong?

User is offlineProfile CardPM
+Quote Post


no2pencil

RE: Sending Multiple Attachments With Mail()

18 May, 2009 - 07:14 PM
Post #2

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,203



Thanked: 289 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
QUOTE(denewey @ 18 May, 2009 - 09:12 PM) *

Any idea what I've done wrong?

Is the error that both files are not attaching, or that the image is not showing up when the html is viewed?

In the event that it's the 2nd question, an html file cannot load images (or css) from email attachments. The email client isn't a webserver.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 05:34PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month