phpmailer如何发送内联和文档附件

时间:2016-12-20 03:52:00

标签: php phpmailer icalendar

    require_once( './PHPMailer-master/PHPMailerAutoload.php');
    $mail = new PHPMailer();
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.office365.com';
    $mail->ClearReplyTos();
    $mail->addReplyTo($organizer_email, $organizer);// Specify main and backup server
    $mail->SetFrom('xyx@xyx.com', 'zyx');
    $mail->SMTPAuth = tls;                               // Enable SMTP authentication
    $mail->Username = 'xyz@xyz.com';                   // SMTP username
    $mail->Password = 'xyzzzzz';               // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
    $mail->Port = 587;                                    //Set the SMTP port number - 587 for authenticated TLS
    $mail->addAddress($organizer_email);  // Add a recipient
    $content = file_get_contents($resume_attach_path);
    $content = chunk_split(base64_encode($content));
    $eol = "\r\n";
    $tags = explode(',', $to);
    foreach ($tags as $tag) {
        $mail->addCC($tag);
    }
    $mail->WordWrap = 50;        
    $mail->Subject = $subject;
    $mail->Body = $desc;
    $mail->AltBody = $text; // in your case once more the $text string
    $mail->Ical = $text; // ical format, in your case $text string 
    $mail->isHTML(true); // Set email format to HTML       
    //$mail->addStringAttachment($resume_attach_path,'base64');
    if ($mail->send()) {
       return true;
    }else{
        return false;
    }

如果我使用addAttachment我只能看到附件,看不到讽刺邀请,但如果我删除附件我可以看到邀请,我如何在phpmailer中发送内联文件和外部文件附件。

2 个答案:

答案 0 :(得分:0)

我在这里向您展示一个简单的工作示例

    <?php
function sendMail($subject, $body, $name, $email, $attachment = null){
        $mail = new PHPMailer(true);
        $mail->IsSMTP(); // telling the class to use SMTP
        $mail->SMTPAuth = true; // enable SMTP authentication
        $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
        $mail->Host = "**************"; // sets GMAIL as the SMTP server
        $mail->Port = 465; // set the SMTP port for the GMAIL server
        $mail->Username = "*************"; // GMAIL username
        $mail->Password = "***********"; // GMAIL password

         //Typical mail data
        $mail->AddAddress($email, $name);
        $mail->SetFrom('*********', '************');
        $mail->Subject = $subject;      
        $mail->Body = $body;
        $mail->IsHTML(true);
        if($attachment){            
             $mail->AddAttachment($attachment['tmp_name'],
                         $attachment['name']);
        }
        try{

            $mail->Send();

        } catch(Exception $e){
            //Something went bad
            // print_r($e);
            echo "Fail :(";
            die;
        }
    }
?>

在此代码中,$attachment$_FILES['file']。如何调用此函数如下所示。

sendMail($subject, $body, $name, $email, $_FILES['file']);

要在邮件程序中使用Ical,请参阅Stackoverflow Question

答案 1 :(得分:0)

根据PHPMailer代码中的评论:

  

仅支持简单的alt或alt_inline消息类型

添加其他附件时,会忽略基于Ical属性添加Ical文件的部分内容。