File attachment and Phpmailer class

I would like to send email with file attached using Html form and phpmailer class. While I am able to send the email properly, i am not able to attach the file with the message. Could anyone who has used phpmailer show me how a file input from a html form can send the message with the file as attachment.

Below is my mailform.php:

<form action=“index.php?action=sendmail” method=“post” enctype=“multipart/form-data” name=“theform”>
<input type=“hidden” name=“op” value=“send_email” />
<input type=“hidden” name=“MAX_FILE_SIZE” value=“30000” />
<table width=“80%” border=“0” align=“center” cellpadding=“2” cellspacing=“2”>
<td width=“13%”>From:</td>
<td colspan=“2”><input name=“fromemail” type=“text” id=“fromemail” size=“45” value=“<?php echo stripslashes($from) ?>” ></td>
<td colspan=“2”><input name=“toemail” type=“text” id=“toemail” value=“<?php echo $to ?>” ></td>
<td colspan=“2”><input name=“subject_field” type=“text” id=“subject_field” size=“45” value=“<?php echo stripslashes($subject) ?>” ></td>
<td colspan=“2”><textarea name=“textmsg” cols=“80” rows=“10” value=“<?php echo stripslashes($mailbody) ?>”></textarea></td>
<td>File Attachment </td>
<td colspan=“2”><input name=“file_attach” type=“file” id=“file_attach”></td>
<td> </td>
<td width=“17%”><input name=“Sendmail” type=“submit” id=“Sendmail” value=“Send Mail”> </td>
<td width=“70%”><input name=“Cancel” type=“button” id=“Cancel” value=“Cancel”></td>


Now how must I use the phpmailer class to send attachments?

Thanks for the help.

Split it into two operations. First handle the upload (documented thoroughly here:, then attach the uploaded file to the message. Attaching a file is covered very clearly in the phpmailer docs:

I have read through the links provided, but I seem to be missing something. Could you pls tell me what is wrong in the following code:

function sendemail() {
// instantiate the class
$mailer = new FreakMailer();
$mailer->From = $_POST[“fromemail”];
$mailer->To = $_POST[“toemail”];
$mailer->Subject = $_POST[“subject_field”];
$mailer->Body = $_POST[“textmsg”];
$mailer->WordWrap = 50;

//for file attachments

$uploaddir = ‘uploads/’;
$uploadfile = $uploaddir . basename($_FILES[‘file_attach’][‘name’]);
move_uploaded_file($_FILES[‘file_attach’][‘tmp_name’], $uploadfile);
$firstfile = $_POST[“file_attach”];
$mailer -> Addattachment($firstfile, testpdf, “base64”, “application/pdf”);

echo ‘There was a problem sending this mail!’;
echo ‘Mail sent!’;


I’m not very good at php, so would appreciate a detail explaination.

Thanks for all the help.


 //for file attachments

$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['file_attach']['name']);
move_uploaded_file($_FILES['file_attach']['tmp_name'], $uploadfile);
$mailer -> Addattachment($uploadfile, testpdf, "base64", "application/pdf");

Sorry to be replying after a long time. Have been busy lately. Thanks for the suggestion, however, the change doesn’t seem to work.The email with the body and subject is sent but not the attachment. Why is that so? Does anyone have a working example of the phpmailer class using attachment? Would appreciate all the help.


This works for me:
I’m using SMTP so if you don’t want that, just delete those top 2 lines :slight_smile:

// instatiate mailer class
$mail = new PHPMailer();
// mail settings
$mail->Host = 'localhost';
$mail->From = '';
$mail->FromName = "AutoMailer";
$mail->Subject = 'Subject';
$mail->Body = 'Body';
$mail->AddAttachment('path_to_uploaded_file', 'filename.whatever');

Thanks for the reply. However, I was looking for an example where the user attaches a file with an email form (as shown in my 1st post) and the email with the file attached is sent to the receiver. My previous script works fine without the file attached. I am not sure why the file doesn’t upload to the upload folder. Any ideas?