Submit file attachments from a form using phpMailer and PHP

I have a form on example.com/contact-us.php that looks like this (simplified):

 <form method="post" action="process.php" enctype="multipart/form-data"> <input type="file" name="uploaded_file" id="uploaded_file" /> <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> </form> 

In my process.php file, I have the following code using PHPMailer() to send email:

 require("phpmailer.php"); $mail = new PHPMailer(); $mail->From = me@example.com; $mail->FromName = My name; $mail->AddAddress(me@example.com,"John Doe"); $mail->WordWrap = 50; $mail->IsHTML(true); $mail->Subject = "Contact Form Submitted"; $mail->Body = "This is the body of the message."; 

Email sends the body correctly, but without the attachment uploaded_file .

MY QUESTION

I need an uploaded_file from a form to attach to an email and submit. I don't care about saving the file after the process.php script sends it by email.

I understand that I need to add AddAttachment(); somewhere (I assume in the Body line) for the attachment to be sent. But...

  • What do I put at the top of the process.php file to pull out the uploaded_file ? How is something using $_FILES['uploaded_file'] to pull a file from contact-us.php?
  • What is included in AddAttachment(); for the attached file and sent along with the email and where should this code go?

Please help and tell the code! Thank!

+48
php phpmailer file-upload email-attachments
Aug 01 '12 at 17:07
source share
5 answers

Try:

 if (isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) { $mail->AddAttachment($_FILES['uploaded_file']['tmp_name'], $_FILES['uploaded_file']['name']); } 

A basic example can also be found here .

Function definition for AddAttachment :

 public function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') 
+79
Aug 01 2018-12-12T00:
source share

File cannot be attached from client PC (download)

In the HTML form, I did not add the following line, so there was no attachment:

ENCTYPE = "Multipart / Form Data"

After adding a line in the form (as shown below), the application will work fine.

 <form id="form1" name="form1" method="post" action="form_phpm_mailer.php" enctype="multipart/form-data"> 
+5
Dec 15
source share

You would use $_FILES['uploaded_file']['tmp_name'] , this is the way PHP stores the downloaded file (this is a temporary file automatically deleted by PHP when the script ends, if you did not move it / copy it to another location).

Assuming that the client and server download settings are correct, you have nothing to do to “download” the download. It will be just magically available in this tmp_name path.

Please note that you need to confirm that the download is indeed successful, for example

 if ($_FILES['uploaded_file']['error'] === UPLOAD_ERR_OK) { ... attach file to email ... } 

Otherwise, you may try to attach with a damaged / partial / nonexistent file.

0
Aug 01 '12 at 17:13
source share

Use this code to send an attachment with a download file parameter using the html form in phpmailer

  <form method="post" action="" enctype="multipart/form-data"> <input type="text" name="name" placeholder="Your Name *"> <input type="email" name="email" placeholder="Email *"> <textarea name="msg" placeholder="Your Message"></textarea> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <input type="file" name="userfile" /> <input name="contact" type="submit" value="Submit Enquiry" /> </form> <?php if(isset($_POST["contact"])) { /////File Upload // In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead // of $_FILES. $uploaddir = 'uploads/'; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); echo '<pre>'; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "Possible invalid file upload !\n"; } echo 'Here is some more debugging info:'; print_r($_FILES); print "</pre>"; ////// Email require_once("class.phpmailer.php"); require_once("class.smtp.php"); $mail_body = array($_POST['name'], $_POST['email'] , $_POST['msg']); $new_body = "Name: " . $mail_body[0] . ", Email " . $mail_body[1] . " Description: " . $mail_body[2]; $d=strtotime("today"); $subj = 'New enquiry '. date("Ymd h:i:sa", $d); $mail = new PHPMailer(); // create a new object //$mail->IsSMTP(); // enable SMTP $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only ,false = Disable $mail->Host = "mail.yourhost.com"; $mail->Port = '465'; $mail->SMTPAuth = true; // enable $mail->SMTPSecure = true; $mail->IsHTML(true); $mail->Username = "admin@domain.net"; //from@domainname.com $mail->Password = "password"; $mail->SetFrom("admin@domain.net", "Your Website Name"); $mail->Subject = $subj; $mail->Body = $new_body; $mail->AddAttachment($uploadfile); $mail->AltBody = 'Upload'; $mail->AddAddress("recipient@domain.com"); if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo '<p> Success </p> '; } } ?> 

Use the link for reference.

0
Dec 26 '17 at 11:33
source share

This code will help me in sending attachments ....

 $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']); 

Replace the AddAttachment (...) code with the code above

-one
Jun 02 '16 at 10:50
source share



All Articles