note 14524 deleted from ref.mail by jimw
| From: | jimw@php.net | Date: | Thu, 01 Nov 2001 19:59:44 +0000 |
| Subject: | note 14524 deleted from ref.mail by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-20010@lists.php.net to get a copy of this message | ||
This is a way to painlessly upload and send mail attachments via mail.
First, download mime_mail.inc - this is a file from the WROX book on PHP - I'm sure it can be
found online somewhere. Include or require this file in your page before the stuff below.
Then, make the page as follows:
1. Make a form that gets the file - use <input type="file"
name="userfile"> in your form. When the form is submitted, so is the file.
2. The file that actually sends the e-mail must move the uploaded file to a directory on your server
- the /tmp directory is perfect for it, because you'll only need a file for a second, and
don't have to worry about keeping it on a server, since it will be sent.
This is accomplished as follows:
// start attachments
// copy uploaded file from post to the /tmp directory
if(is_uploaded_file($userfile)){
move_uploaded_file($userfile, "/tmp/" . $userfile_name);
}
// use copied file
$filename = "/tmp/" . $userfile_name;
$content_type = $userfile_type;
// read file from the drive (in this case, a /tmp directory)
$fd = fopen($filename, "rb");
$data = fread($fd, filesize($filename));
fclose($fd);
// end attachments
With this you don't have to worry about file name or the content type.
3. Now you can specify your regular $recipient, $subject, $sender, and $message
4. Now, use the file you downloaded:
// create object instance
$mail = new mime_mail;
// set all data slots
$mail->from = $sender;
$mail->to = $recipient;
$mail->subject= $subject;
$mail->body = $message;
// append the attachment
$mail->add_attachment($data, $filename, $content_type);
// send email
$mail->send();
5. For more information on how PHP handles POST method uploads, go to http://www.php.net/manual/en/features.file-upload.php