Jump to content

PHP Multiple Attachement Mail Delivery


Recommended Posts

$from = "test@localhost.com";
$to = "test@localhost.com";
$subject =$_POST['subject'];
$message = $_POST['body'];

// include the from email in the headers
$headers = "From: $from";

// boundary
$time = md5(time());
$boundary = "==Multipart_Boundary_x{$time}x";

// headers used for send attachment with email
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$boundary}\"";

// multipart boundary
$message = "--{$boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";

// Fily Type Validation
$allowedExtensions = array("jpeg","jpg");

// attach the attachments to the message
$files = $_FILES['imgupload'];

for($i=0; $i < count($files['name']); $i++)
if ($files['name'][$i] != '')
$file_name = $files['name'][$i];
$file_path = pathinfo($file_name);
$file_ext = $file_path['extension'];
if (in_array($file_ext, $allowedExtensions))
$tmp_name = $files['tmp_name'][$i];
$content = chunk_split(base64_encode(file_get_contents($tmp_name)));
$message .=
"--{$boundary}\n" .
"Content-Type: {\"application/octet-stream\"};\n" .
" name=\"{$file_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$file_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";

} else { $errormgs= 'alert("File Type is not accepted, please use an image.")'; $formerrors = 'true'; }
} // else { $errormgs= 'alert("You Forgot to Attach the Photo")'; $formerrors = 'true'; }

// sending mail
if (!$formerrors)
$sendmail = mail($to, $subject, $message, $headers);
} else
print '<script type="text/javascript">';
print $errormgs;
print '</script>';




Hello Everybody, I hope somebody can help me with the script that I have written. It allows me to create as many input field in my form and deliver the attachment to the dedicated email without changing the PHP code for every additional attachment input field.

The only problem I have is an alert for the attachment fields. If there is no file selected I want the popup with the message demanding the image to be added. For some reason it doesn't work. If I add the else statement with the message the form keeps giving me that alert no matter if I add the Image or don't. What am I doing wrong?

Thank you all for help and sorry if I am expressing myself baddly.

Link to comment
Share on other sites

You could really benefit from using a well developed email sending class. Swift Mailer, at swiftmailer.org is a really nice one. It will take all of the work out of your task, and it does a lot of things you probably don't even know you need to do to send email correctly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.