Php 在电子邮件中发送正文内容和日历邀请

Php 在电子邮件中发送正文内容和日历邀请,php,sendgrid,Php,Sendgrid,我正在尝试发送一封包含日历邀请和HTML正文内容的电子邮件,但我似乎无法将两者都添加到要通过SendGrid发送的电子邮件对象中 我能够自己发送日历邀请,自己发送HTML正文内容,但不能一起发送 function sendgridAPI(){ GLOBAL $mgClient,$domain,$toName, $toEmail, $fromName, $fromEmail, $subj, $body, $cc, $bcc, $attachments, $mimeMessage, $sendgr

我正在尝试发送一封包含日历邀请和HTML正文内容的电子邮件,但我似乎无法将两者都添加到要通过SendGrid发送的电子邮件对象中

我能够自己发送日历邀请,自己发送HTML正文内容,但不能一起发送

function sendgridAPI(){

GLOBAL $mgClient,$domain,$toName, $toEmail, $fromName, $fromEmail, $subj, $body, $cc, $bcc, $attachments, $mimeMessage, $sendgrid_api_key;


    $email = new \SendGrid\Mail\Mail();
    $email->setFrom($fromEmail, $fromName);
    $email->setSubject($subj);
    $toEmails = [$toEmail => $toName,];
    $email->addTos($toEmails);    



    if ($mimeMessage != ""){
        echo "<br> 1 <br>";

       $contents = [
        "text/calendar" => $mimeMessage,
        "text/html" => $body
        ];
        $email->addContents($contents);

    }
    else{
         $content = ["text/html" => $body];
         $email->addContent($content);
    }

    if($cc != ""){
        $ccEmails = [$cc => "CC",];
        $email->addCcs($ccEmails);     
    }

    if ($attachments != ""){
        $filePath = $attachments;
        $fileName = substr($attachments, strrpos($attachments, '/') + 1);
        $fileData = base64_encode(file_get_contents($filePath));
        $fileExtension = substr($attachments, strrpos($attachments, '.') + 1);
        $fileType = 'application/'. $fileExtension;

        $email->addAttachment(
            $fileData,
            $fileType,
            $fileName,
            "attachment"
        );
        $email->addAttachments($attachments);
    }


    $sendgrid = new \SendGrid($sendgrid_api_key);
    try {
        $response = $sendgrid->send($email);
        $data = $response->headers();
        print_r($data);
        gettype($data['5']);
        $responseSG = substr($data['5'], strpos($data['5'], ":") + 1);   
        return $responseSG;
        //echo $responseSG;
    } catch (Exception $e) {
        echo 'Caught exception: '. $e->getMessage() ."\n";
        return "";
    }
}
?>
函数sendgridAPI(){ 全局$mgClient、$domain、$toName、$toEmail、$fromName、$fromeail、$subc、$body、$cc、$bcc、$attachments、$mimessage、$sendgrid\u api\u key; $email=new\SendGrid\Mail\Mail(); $email->setFrom($fromEmail,$fromName); $email->setSubject($subj); $toEmails=[$toEmail=>$toName,]; $email->addTos($toemail); 如果($mimessage!=“”){ 回声“
1
”; $contents=[ “文本/日历”=>$mimessage, “text/html”=>$body ]; $email->addContents($contents); } 否则{ $content=[“text/html”=>$body]; $email->addContent($content); } 如果($cc!=“”){ $ccEmails=[$cc=>“cc”,]; $email->addCcs($ccEmails); } 如果($attachments!=“”){ $filePath=$attachments; $fileName=substr($attachments,strrpos($attachments,“/”)+1); $fileData=base64_编码(文件获取内容($filePath)); $fileExtension=substr($attachments,strrpos($attachments,'.')+1); $fileType='application/'。$fileExtension; $email->addAttachment( $fileData, $fileType, $fileName, “附件” ); $email->addAttachments($attachments); } $sendgrid=new\sendgrid($sendgrid\u api\u key); 试一试{ $response=$sendgrid->send($email); $data=$response->headers(); 打印(数据); gettype($data['5']); $responseSG=substr($data['5'],strpos($data['5'],“:”)+1); 返回$responseSG; //echo$responseSG; }捕获(例外$e){ 回显“捕获的异常:”。$e->getMessage()。“\n”; 返回“”; } } ?>
变量被传递到此函数,然后电子邮件对象被构造为使用SendGrid API发送您需要为
addAttachment()
创建附件对象,而不是传递文件名。以及
addAttachments()

以下是附件的构造函数: