addAttachment不向电子邮件添加任何附件

我写出了以下代码,并且电子邮件正常发送,但是我无法弄清楚为什么电子邮件中没有包含预期的附件(pdf文件)。

我也尝试使用MIME附加$ mail-> attach,并给出500个内部服务器错误。

    $pdf = public_path() . '/assets/attachments/TermSheet.pdf';
    $mail = new PHPMailer\PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPDebug = 0;
    $mail->SMTPSecure = '';
    $mail->SMTPAuth = true;
    $mail->IsHTML(true);

    //ENV VARIABLES
    $mail->Host =  env("MAIL_HOST");
    $mail->Port = env("MAIL_PORT");
    $mail->username = env("MAIL_username");
    $mail->Password = env("MAIL_PASSWORD");
    $mail->setfrom($fromemail,$fromname);
    $mail->Subject = $emailsubject;

    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $emailsubject;
    $mail->addAttachment($pdf);
    $mail->Body    = $initbody;  
    $mail->AddAddress($user);
    if(isset($ccemaillist)){                        //cc
        $ccemail = explode(";",$ccemaillist);
        foreach ($ccemail as $toccmail) {
            $mail->AddAddress($toccmail);
        }
    }                  

    if($mail->Send()) {    

        $ch = curl_init();

        curl_setopt($ch,CURLOPT_URL,$currentapp.'/mail/send?api_user_id='.$creatorid);
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,"recipient_email=$user&email_type=WelcomeEmail&sys_status=1&creator=$creatorid");

        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

        $server_output = curl_exec($ch);

        curl_close ($ch);     

        return "SUC";
    }

我不确定为什么它不起作用,对此我将不胜感激。预先谢谢你:)

hahahahaha2009 回答:addAttachment不向电子邮件添加任何附件

它可能会给

  

500内部服务器错误

由于文件路径错误。

您可以尝试像 $pdf = asset('assets/attachments/TermSheet.pdf'); public_path()在某些情况下不起作用。

本文链接:https://www.f2er.com/3126714.html

大家都在问