簡體   English   中英

php電子郵件功能不發送附件

[英]php email function not sending attachment

我正在嘗試發送帶有附件的電子郵件,我的代碼在下面,如果我做錯了什么或做錯了什么,請告訴我

<?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();
    }
?>

使用$ mail-> 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';
?>

這里有很多要嘗試的摘要,但以下幾行看起來不對。

$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 ,我建議使用與提供給函數mail_attachment的參數命名相同的變量〜我必須不斷計算哪個參數引用哪個變量。

整個標題如下所示:

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在2個地方之前需要換行。

另外,我忘了提到您使用7個參數調用函數mail_attachment ,但該函數需要8個參數。 函數調用中似乎未提供的一個是$from_mail

請使用PHP mail()函數並使用此代碼發送附件

$mail->AddAttachment($certificate);

如果您需要更多詳細信息,請詢問

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM