Question

Using form on web site, I want to upload attachment first and send it to arbitary email address. I've tried to use phpmailer, but I've never managed to attach file just browsing it, only directly:

$mail->attachment("images/hello.jpg");

Does anyone knows how to solve this problem, or any alternative solution to PHPMailer?

Was it helpful?

Solution

Something like this on the server processing ought to work:

if ($_FILES['file']['error'] <= 0 ){ // success, no error

    // read in the contents of temporary file
    $file_contents = file_get_contents( $_FILES['file']['tmp_name'] );

    // add the contents to phpmailer object, with original filename from upload
    $mail->addStringAttachment( $file_contents, $_FILES['file']['name'] );
}

This assumes your form element is named file, like:

<input type="file" name="file" />

OTHER TIPS

For this to work you'll need to create an upload form, see http://www.php.net/manual/en/features.file-upload.post-method.php for more information on that. After uploading the file it can be attached to a mail just as you did in your example. The file name is then replaced by the path and file name where you saved the upload.

Try this one, from http://www.finalwebsites.com/forums/topic/php-e-mail-attachment-script :

<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
        echo "mail send ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
    }
}
?>

Is it possible that you need to use the AddAttachment() instead of attachment()?

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

You may also have to make sure that the path to the file is correct in relation to your script on the server.

From https://github.com/Synchro/PHPMailer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top