繁体   English   中英

php发送带附件的电子邮件

[英]php send email with attachment

如何发送包含简历附件的电子邮件,

我从这个地方拿走片段点击这里

在这个网站,代码片段工作正常,

即使我收到邮件,但附件无法正常工作,我正在以0kb的身份获得noname

大小文件,该代码段中的问题是什么,

 function mail_attachment($to, $subject, $message, $from, $file) {
  // $file should include path and filename
  $filename = basename($file);
  $file_size = filesize($file);
  $content = chunk_split(base64_encode(file_get_contents($file))); 
  $uid = md5(uniqid(time()));
  $from = str_replace(array("\r", "\n"), '', $from); // to prevent email injection
  $header = "From: ".$from."\r\n"
      ."MIME-Version: 1.0\r\n"
      ."Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"
      ."This is a multi-part message in MIME format.\r\n" 
      ."--".$uid."\r\n"
      ."Content-type:text/plain; charset=iso-8859-1\r\n"
      ."Content-Transfer-Encoding: 7bit\r\n\r\n"
      .$message."\r\n\r\n"
      ."--".$uid."\r\n"
      ."Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"
      ."Content-Transfer-Encoding: base64\r\n"
      ."Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"
      .$content."\r\n\r\n"
      ."--".$uid."--"; 
  return mail($to, $subject, "", $header);
 }

如果您不想手动学习如何操作,并且只想发送带附件的电子邮件,那么您最好使用某种类型的库。 我推荐SwiftMailer ,我尝试了很多库,这个最好用。 检查使用SwiftMailer添加附件是多么容易: http ://swiftmailer.org/docs/attaching-files

您最好的选择是使用Mime Mail PEAR库来处理附件。 它更容易,更清洁,你不会容易出错。

梨Mime邮件

您可以将文件附加到电子邮件,如下所示:

$headers['From'] = 'from@domain.com';
$headers['To'] = 'to@domain.com';
$headers['Subject'] = 'Email Subject';

$mime = new Mail_mime("\r\n");
$mime->setTXTBody('Email text');
$mime->addAttachment($filedata, 'application/octet-stream', 'filename', true, 'base64');

//Prepare the message to be sent
$body = $mime->get();
$headers = $mime->headers($headers);

//Send the message via SMTP
$mail_obj =& Mail::factory('smtp', array('host' => 'mail.domain.com', 'port' => 25));
$mail_obj->send($to, $headers, $body);

如果你只是想发送一个带有单个附件的简单表单,那么这段代码可能是最好的,最容易使用的是以下代码,它运行良好。

function submitSupportTicket(){

if(isset($_POST['submit']))
{

    $company = $_POST['company'];
    $url = $_POST['url'];
    $issue = $_POST['issue'];

    $screenshot = basename($_FILES['screenshot']['name']);

    $fileType = substr($screenshot, strrpos($screenshot, '.') + 1);

    $fileSize = $_FILES['screenshot']['size']/1024;

    $allowedFileTypes = array("jpg", "jpeg", "gif", "bmp", 'png');

    $allowedExt = false;

    for($i=0; $i<sizeof($allowedFileTypes); $i++)
    {

        if(strcasecmp($allowedFileTypes[$i],$fileType) == 0)
        {

            $allowedExt = true;

        }

    }

    if(!$allowedExt)
    {

        setMessage('The uploaded file is not supported file type. Only the following file types are supported: '.implode(',',$allowed_extensions), 0);
        header('Location: '.currentURL());
        exit;

    }

    $filePath = 'mediaLibrary/attachments'.$screenshot;

    $tmpPath = $_FILES['screenshot']['tmp_name'];

    if(is_uploaded_file($tmpPath))
    {

        if(!copy($tmpPath, $filePath))
        {

            echo 'There was an error attaching the file.';
            exit;

        }

    }

    $attachment = chunk_split(base64_encode(file_get_contents($_FILES['screenshot']['tmp_name'])));

    $fileName = $_FILES['screenshot']['name'];

    $boundary = md5(date('r', time())); 



    $headers = "From: noreply@minttwist.com\r\nReply-To: justin@minttwist.com";
        $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

        $message = "This is a multi-part message in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

Company: $company
URL: $url
Issue: $issue

Submitted from minttwist.com

--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--_1_$boundary--";

    $to = 'your@email.com';

    $subject = 'Support Request - '.$company;

    mail($to, $subject, $message, $headers);

    echo 'We have received your support request.';

    header('Location: '.currentURL());

}

}

我在上面的任何地方都没有看到你问题的答案,所以我会尝试提供一些可能有用的信息。

我今天遇到了同样的问题。 我构建了一系列PDF文件,然后将其转换为单个tar.gz文件,我希望通过电子邮件将此文件包发送给人工处理代理,后者将以实物形式回复这些文档。 但是,当我使用各种脚本生成正确的MIME消息格式时,我最终发送了一个总大小0 kB且名称为“noname”的附件。

有这么多人提供相同的答案,我心里想,答案必须是正确的,问题必须在其他地方。 问题的答案不是消息内容的格式 ,也不是标题中的格式 那里的一切都是正确的。 问题在于应用程序和收件人电子邮件地址之间存在的邮件应用程序。

我将我的代码移动到生产服务器并且发送的消息没有任何问题,并且之前作为“noname 0kb”发送的相同文件现在被发送为“MikeyPoochigian_2013_05_10__16_28_27.tar.gz 241kb”。

我不知道是什么导致了这个特定的失败,但我想这是一个类似的答案,我今年早些时候学到的,当我的邮件应用程序发送到Gmail而不是发送到其他邮件服务器。 在该特定情况下,邮件应用程序在我的开发笔记本电脑(具有DevelopmentLaptop.local的内部域)和最终电子邮件地址之间过滤SPAM的内容。 因为我的原始服务器是从域“DovelopmentLaptop.local”发送的,并且因为该域未在任何DNS中注册为已知地址,所以这些邮件服务器将我的测试邮件解释为垃圾邮件。 我怀疑同样的问题正在干扰正在发送的消息。

现在很长的答案很短(如果可能的话),尝试将代码移植到具有注册公共域的生产服务器,看看它是否有效。 如果是,那么您的代码不需要修复。 你的代码可能没问题。

暂无
暂无

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

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