简体   繁体   English

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

[英]php email function not sending attachment

I am trying to send a email with attachment my code is below let me know if i am doing missing something or doing something wrong我正在尝试发送带有附件的电子邮件,我的代码在下面,如果我做错了什么或做错了什么,请告诉我

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

use $mail->addAttachment($path); 使用$ mail-> addAttachment($ path); It is better to use this. 最好使用这个。

you can get phpmailer file from https://github.com/PHPMailer/PHPMailer 您可以从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';
?>

There is lots here to try to digest but the following line looks wrong. 这里有很多要尝试的摘要,但以下几行看起来不对。

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

When resolved, this would appear as: 解决后,它将显示为:

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

The reply-to address is also wrong, see the full printout of the header to see what I mean. 回复地址也有误,请参阅标题的完整打印输出,以了解我的意思。

Tip: To make it easier I'd suggest using variables that are named the same as the parameters supplied to the function mail_attachment ~ I keep having to count which parameter refers to which variable. 提示:为了mail_attachment ,我建议使用与提供给函数mail_attachment的参数命名相同的变量〜我必须不断计算哪个参数引用哪个变量。

The entire header appears like this: 整个标题如下所示:

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--

From previous experience of dealing with attachments the formatting is critical - it MUST be exactly right in terms of line endings or it will fail. 从以前处理附件的经验来看,格式化非常关键-就行尾而言,它必须完全正确,否则将失败。 Some pointers that might help: 一些可能会有所帮助的指针:

  • There should be only one new line before a boundary. 边界之前应该只有一条新线。
  • There should be 2 dashes only after the last boundary. 仅在最后一个边界之后应有2个破折号。
  • There are 2 places where Content-Transfer-Encoding needs to have a new line before. Content-Transfer-Encoding在2个地方之前需要换行。

Also, I forgot to mention that you call the function mail_attachment with 7 parameters yet the function takes 8 parameters. 另外,我忘了提到您使用7个参数调用函数mail_attachment ,但该函数需要8个参数。 The one that appears to not be supplied in the function call is $from_mail 函数调用中似乎未提供的一个是$from_mail

Please use PHP mail() function and use this code to send attachment 请使用PHP mail()函数并使用此代码发送附件

$mail->AddAttachment($certificate);

if you need more details please ask 如果您需要更多详细信息,请询问

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

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