简体   繁体   English

如何发送带有附件的邮件

[英]How to send mail with attachment

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: 我尝试使用phpmailer,但是我从来没有设法仅浏览文件就直接附加文件:

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

Does anyone knows how to solve this problem, or any alternative solution to PHPMailer? 有谁知道如何解决此问题,或PHPMailer的任何替代解决方案?

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: 假设您的表单元素名为file ,例如:

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

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. 为此,您需要创建一个上传表单,有关更多信息,请参见http://www.php.net/manual/en/features.file-upload.post-method.php 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 : 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()? 是否可能需要使用AddAttachment()而不是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 https://github.com/Synchro/PHPMailer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM