I’ve been struggling to find any simple to follow guides or tutorials on how to do this so I thought I would ask here. I am sending out an email and all I want to do is attach 2 pdf files to it. I have found a script that allows me to send a single email with a single attachment.
<?php
$from = "me@domain.com";
$to = $_POST['email'];
$subject = "Uploaded file";
$mail_body = "Message body";
$fileatt = $_FILES['file']['tmp_name'];
$fileatt_type = $_FILES['file']['type'];
$fileatt_name = $_FILES['file']['name'];
$headers = "From: $from";
if (is_uploaded_file($fileatt))
{
$file= fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
$semi_rand = md5(time());
$mime_boundary = "Multipart_Boundary_x{$semi_rand}x";
$headers .= "\
MIME-Version: 1.0\
" . "Content-Type: multipart/mixed;\
" . " boundary=\\"{$mime_boundary}\\"";
$mail_body = "This is a multi-part message in MIME format.\
\
" . "--{$mime_boundary}\
" . "Content-Type: text/html; charset=\\"iso-8859-1\\"\
" .
"Content-Transfer-Encoding: 7bit\
\
" . $mail_body . "\
\
";
$data = chunk_split( base64_encode($data));
$mail_body .= "--{$mime_boundary}\
" . "Content-Type: {$fileatt_type};\
" . " name=\\"{$fileatt_name}\\"\
" . "Content-Disposition: attachment;\
" .
" filename=\\"{$fileatt_name}\\"\
" . "Content-Transfer-Encoding: base64\
\
" . $data . "\
\
" . "--{$mime_boundary}--\
";
$ok = @mail($to, $subject, $mail_body, $headers);
if($ok)
echo "Email sent :)";
else
echo "Failed !!!";
fclose($file);
}
?>
This works fine but how can I adapt this to send 2 attachments? Here’s the code for the form that gets the user info;
<form action="process.php" method="post" enctype="multipart/form-data">
To:<input type="text" name="email" /><br />
File: <input type="file" name="file" id="file" /> <br />
<input type="submit" name="submit" value="Sent" />
</form>
If anyone can help or point me in the right direction, then I would be very grateful.