PHP page sending mail..want multiple attachments

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
kbrown3074
Forum Contributor
Posts: 119
Joined: Thu Jul 20, 2006 1:36 pm

PHP page sending mail..want multiple attachments

Post by kbrown3074 »

I currently have a php page that sends an email with a single attachment. Is there anything special I need to do to send multiple attachments? I was thinking I can just loop through the attachment code but was wondering if there was a better way that I havent tried yet.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you using Swift?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah we'll need to see any code to know what you mean by "the attachment code", unless you are using Swift mailer, in which case you just call $message->attach() multiple times.
User avatar
kbrown3074
Forum Contributor
Posts: 119
Joined: Thu Jul 20, 2006 1:36 pm

Post by kbrown3074 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am not currently running swift.  Is that a single package that I can install on the server?  Below is the code that I am running for a single file..works so far with one file.

Code: Select all

function filemail($filename,$subject,$mailtxt,$mailto)
{
   $fileatt = $filename; // Path to the file
   $fileatt_type = "application/octet-stream"; // File Type
   $fileatt_name = $filename; // Filename that will be used for the file as the attachment

   $email_from = "name.com"; // Who the email is from
   $email_subject = $subject; // The Subject of the email
   $email_txt = $mailtxt; // Message that the email has in it

   $email_to = $mailto; // Who the email is too

   $headers = "From: ".$email_from;

   $file = fopen($fileatt,'rb');
   $data = fread($file,filesize($fileatt));
   fclose($file);

   $semi_rand = md5(time());
   $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

   $headers .= "\nMIME-Version: 1.0\n" .
            "Content-Type: multipart/mixed;\n" .
            " boundary=\"{$mime_boundary}\"";

   $email_message = "This is a multi-part message in MIME format.\n\n" .
                "--{$mime_boundary}\n" .
                "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
               "Content-Transfer-Encoding: 7bit\n\n" .
   $email_txt . "\n\n";
   $data = chunk_split(base64_encode($data));

   $email_message .= "--{$mime_boundary}\n" .
                  "Content-Type: {$fileatt_type};\n" .
                  " name=\"{$fileatt_name}\"\n" .
                  "Content-Transfer-Encoding: base64\n\n" .
                 $data . "\n\n" .

                  "--{$mime_boundary}--\n";

   $ok = @mail($email_to, $email_subject, $email_message, $headers);
}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
kbrown3074
Forum Contributor
Posts: 119
Joined: Thu Jul 20, 2006 1:36 pm

Post by kbrown3074 »

d11wtq wrote:Yeah we'll need to see any code to know what you mean by "the attachment code", unless you are using Swift mailer, in which case you just call $message->attach() multiple times.
Just wanna say "Mad Props" on the swift mailer!! It was EXACTLY what I needed...thanks!!
Post Reply