Justgrant2009
Justgrant2009

Reputation: 603

browse to image file then attach to PHP mail

I'm not exactly a pro at this, so please forgive me for asking such a (probably) stupid question.

I need help setting up (on an html page) a method to allow a visitor to browse to a file (actually 2 files) on their computer that will be attached to a PHP mail form when they choose to submit.

I know with the PHP mailing images, I will need to use something like PearMime or PHPMailer.

*Update*

Ok, now that I know how to put the HTML form to get the files. When I use the following PHP, what am I doing wrong? I have 2 files requested from the HTML page by IDs (the HTML has both files correctly created), that is the location of the PHPMailer_5.2.1, the $mail->MsgHTML(file_get_contents('testfiles.html')); is actually looking at a html page I created at that location that just has a template starter for an html doc with the word Sample in the body.

<?php

require("PHPMailer_5.2.1/class.phpmailer.php");

$mail = new PHPMailer(); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch


$file1 = $_REQUEST['file1'];
$file2 = $_REQUEST['file2'];

try {
  $mail->AddReplyTo('[email protected]', 'FirstName LastName');
  $mail->AddAddress('[email protected]', 'FirstName LastName');
  $mail->SetFrom('[email protected]', 'FirstName LastName');
  $mail->AddReplyTo('[email protected]', 'FirstName LastName');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('testfiles.html'));
  $mail->AddAttachment($file1);      // attachment
  $mail->AddAttachment($file2); // attachment
  $mail->Send();
  echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}


?>

Upvotes: 1

Views: 1128

Answers (1)

MasterGberry
MasterGberry

Reputation: 2860

You are looking for the HTML attribute?

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />

It is extremely important that enctype="multipart/form-data is in your tag or it will not allow images to be attached. Then your PHP Scripts should process the fields as needed. Good luck :)

Upvotes: 1

Related Questions