View Full Version : Sending HTML Form w/ Attachments

12-19-2007, 09:34 PM
Hey there!

I have a form that I want to have an attachment box, much like an email. However, the form is in HTML and despite my efforts, I can't get the HTML and attachment to send at the same time.

All right, when I wrote this before, I said I didn't think PHPMailer would do the trick. But I managed to get an HTML email to send in it, with an attachment, but the attachment is always just a couple of bytes and not the real thing. The attachment should be loaded from a field where a user browses to a file on their computer. (Like C:\Blah.jpg)

It gets the name of the file correctly, but that's about it.

Do these files have to be uploaded somewhere? Or does it have to do with the encoding? I have it set to "application/octet-stream"

Any advice? I'm an intermediate beginner to PHP.

Thanks :thumbsup:

My code:

$mail = new PHPMailer();
$mail->SMTPAuth = false;
$mail->From = $qq_emailv; //variable of user-inputed email
$mail->FromName = $from; //variable of user's name
$mail->Subject = "Welcome!";
$mail->Body = "<b>This is a</b> <i>test</i> <small>of the PHP</small><h1>Mailer</h1> {$attach}";
$attach = stripslashes($attach); // $attach is declared earlier from the POST array

12-21-2007, 01:59 AM
Bump...I added more info! Any thoughts? Thanks guys...!

12-21-2007, 04:38 PM
With PHPMailer you need to allow them to upload the files to the webserver and then you can attach it to the email before sending. They can't send directly from their hard drive.

12-21-2007, 07:15 PM
I'm such a dork ;) I got an attachment to send WITH an HTML. Thank you for the tip, arnyinc.

Is it possible to send more than 2 attachments, however, in PHP Mailer?

12-22-2007, 12:25 AM
K, ignore that last post - for some reason, the attachments decided not to send again! And I didn't even fiddle with the script!

Here is my script. I apologize if it seems very unsecure. I am planning to add much more security (limiting file sizes and types) once I have the attachment basics down.

If anyone could look over this and tell me what's going on... my attachments again aren't sending. They worked for a little bit, but that's it. I can see the file is uploading though into the correct directory.


if (isset($_POST['submit']) && $_POST['submit'] == "Submit")
$qq_name = false;
$qq_emailv = false;

//Check If Empty Form
if ( empty( $_POST['qq_name'] ) ) {
echo '<br><font color="red">ERROR: Please enter a First Name</font>'; }
else {
$qq_name = true; }

if ( empty( $_POST['qq_email'] ) ) {
echo '<br><font color="red">ERROR: Please enter a Email</font>'; }
else {
//Valid Email
if(!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $_POST['qq_email'])) {
echo '<br><font color="red">ERROR: Please enter a valid Email</font>'; }
else {
$qq_emailv = $_POST['qq_email']; }

// If - 2
if ($qq_name && $qq_emailv) {

$from = $_POST['qq_name'];

///Uploading Attachments Script///
$target_path = "./uploads/";

/* Add original filename to our target path.
Result is 'uploads/filename.extension' */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

////////////////Send Mail//////////////////

$mail = new PHPMailer();
$mail->SMTPAuth = false;
$mail->From = $qq_emailv;
$mail->FromName = $from;
$mail->Subject = "MLI, INC - QUICK QUOTE FORM";
$mail->Body = ' (The HTML form is here and submits correctly. I'm removing it now because it's GIGANTIC!) ';
$attach = $_FILES['uploadedfile']['name'];

// Send to Mail
if(!$mail->Send()) {
echo 'Failed to send mail. Please contact the website administrator.';
} else {
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo 'Mail sent with attachment:' . $target_path . '.';


SeeIT Solutions
12-22-2007, 12:34 AM
It looks like you are moving the file to the temp location on your server after you send the mail.

That might be why you can see the file after but it doesnt send.

move the line

move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);

out of the if statement and place it above this:

$attach = $_FILES['uploadedfile']['name'];

You might also want to place checks to see if there is a file uploaded, at the moment it will try to send attachments when there are none.

12-22-2007, 03:15 AM
Are you saving the attachments for a reason? You shouldn't need to even move them unless you intend to save them.