php电子邮件功能不发送附件

时间:2015-09-30 12:38:47

标签: php email-attachments

我正在尝试发送附带我的代码的电子邮件,如果我错过了某些内容或做错了,请告诉我

<?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));
        $separator = md5(uniqid(time()));
        $eol = PHP_EOL;

        $header = "From: ".$from_name." <usman_86@rocketmail.com>".$from_mail. $eol;
        $header .= "Reply-To: ".$replyto.$eol;
        $header .= "MIME-Version: 1.0".$eol;
        $header .= "Content-Type: multipart/mixed; boundary=\"".$separator. "\"" . $eol . $eol;
        $header .= "Content-Transfer-Encoding: 7bit" . $eol;
        $header .= "--".$separator. $eol;
        $header .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
        $header .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
        $header .= $message. $eol . $eol;
        $header .= "--".$separator. $eol;
        $header .= "Content-Type: application/octet-stream; name=\"".$filename. $eol; // use different content types here
        $header .= "Content-Transfer-Encoding: base64".$eol;
        $header .= "Content-Disposition: attachment; filename=\"".$filename. $eol;
        $header .= $content. $eol;
        $header .= "--".$separator."--";
        ob_start(); //Turn on output buffering 

        if( mail( $mailto, $subject, "", $header ) ) {
            echo "mail send ... OK"; // or use booleans here
        } else {
            echo "mail send ... ERROR!";
        }
    }

    if( isset($_REQUEST['FindDealer']) ){

        $my_file = "pdf.pdf";
        $my_path = "/";
        $my_name = " Find Dealer @ flowsleeve";
        $my_mail = "stifstone@gmail.com";
        $my_replyto = "my_reply_to@flowsleeve.com";
        $my_subject = "Find Dealer @ flowsleeve";
        $my_message = "Hallo,\r\ndoPlease find Attached PDF For Dealers";

        mail_attachment( $my_file, $my_path, $my_mail, $my_name, $my_replyto, $my_subject, $my_message );
        header("Location: index.php#section8");
        exit();
    }
?>

3 个答案:

答案 0 :(得分:1)

使用$ mail-&gt; addAttachment($ path); 最好使用它。

您可以从https://github.com/PHPMailer/PHPMailer

获取phpmailer文件
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
?>

答案 1 :(得分:0)

这里有很多尝试消化,但以下行看起来不对。

$header = "From: ".$from_name." <usman_86@rocketmail.com>".$from_mail. $eol;

解决后,会显示为:

 "From: my_reply_to@flowsleeve.com <usman_86@rocketmail.com>Find Dealer @ flowsleeve\r\n"

回复地址也是错误的,请参阅标题的完整打印输出以了解我的意思。

  

提示:为了方便起见,我建议使用名为的变量   与提供给函数mail_attachment ~I的参数相同   不断计算哪个参数指的是哪个变量。

整个标题如下所示:

From: my_reply_to@flowsleeve.com <usman_86@rocketmail.com>Find Dealer @ flowsleeve
Reply-To: Find Dealer @ flowsleeve
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="2642aff44ba290c53e0574e15444e12f"

Content-Transfer-Encoding: 7bit
--2642aff44ba290c53e0574e15444e12f
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit



--2642aff44ba290c53e0574e15444e12f
Content-Type: application/octet-stream; name="pdf.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="pdf.pdf


--2642aff44ba290c53e0574e15444e12f--

根据之前处理附件的经验,格式化至关重要 - 它必须完全正确的行结尾或它将失败。 一些可能有帮助的指针:

  • 边界前应该只有一条新线。
  • 只有在最后一个边界之后才有2个破折号。
  • 有两个地方,Content-Transfer-Encoding之前需要换行。

另外,我忘了提到你用7个参数调用函数mail_attachment但该函数需要8个参数。似乎在函数调用中未提供的是$from_mail

答案 2 :(得分:-1)

请使用PHP mail()函数并使用此代码发送附件

$mail->AddAttachment($certificate);

如果您需要更多详细信息,请询问