douzhao9608 2017-06-08 01:03
浏览 60

打开使用PHP版SendGrid邮件发送的电子邮件附件时,我收到了损坏的文件

I am trying to send attachments with SendGrid through PHP, but keep getting a corrupted file error when I open the attachment. The error message "We're sorry. We can't open 'file.docx' because we found a problem with its contents" and when I click on the details of the error I see "The file is corrupt and cannot be opened"

My code looks like the below:

$sendGridLoginInfo = $contactViaEmail->getSendGridLoginInfo();    
        $sendgrid = new SendGrid($sendGridLoginInfo['Username'], $sendGridLoginInfo['Password']);    
        $mail = new SendGrid\Mail();
        //Add the tracker args
        $mail->addUniqueArgument("EmailID", $emailID);
        $mail->addUniqueArgument("EmailGroupID", $emailGroupID);
        /*
        *    INSERT THE SUBSITUTIONS FOR SEND GRID
        */
        foreach ($availableSubstitutions as $availableSubstitution)
        {
            $mail->addSubstitution("[[" . $availableSubstitution . "]]", $substitutions[$availableSubstitution]);
        }

        /*
        *    ADD EACH EMAIL AS A NEW ADD TO
        *    This makes it BCC (because each person gets their own copy) and each person gets their own individualized email.
        */
        foreach ($emailInfo['SendToEmailAddress'] as $toEmail)
        {


            if ($sendToLoggedInUser)
            {
                $mail->addTo($adminEmailAddress);
            }
            else
            {
                $mail->addTo($toEmail);
            }
            $trashCount++;
        }

        //Set the subject
        $mail->setSubject($emailInfo['EmailSubject']);

        //Instantiate the HTML Purifier (for removing the html)
        $config = HTMLPurifier_Config::createDefault();
        $config->set('HTML', 'Allowed', '');
        $purifier = new HTMLPurifier($config);
        $mail->setText($purifier->purify($emailBody));
        $mail->setHtml($emailBody);

        if ($emailInfo['AttachmentID'])
        {
            $sql = "SELECT
                        AttachmentPath
                    FROM
                        EmailAttachments
                    WHERE
                        EmailAttachments.AttachmentID = :attachmentID";    
            if ($query = $pdoLink->prepare($sql))
            {
                $bindValues = array();
                $bindValues[":attachmentID"] = $emailInfo['AttachmentID'];

                if ($query->execute($bindValues))
                {
                    if ($row = $query->fetch(\PDO::FETCH_ASSOC))
                    {
                        $attachment = "";
                        $mail->addAttachment($sitedb . $row['AttachmentPath']);
                    }
                }
            }                
        }




        if ($sendToLoggedInUser)
        {
            $mail->setFrom($adminEmailAddress);
            $mail->setReplyTo($adminEmailAddress);
        }
        else
        {
            $mail->setFrom($emailInfo['FromAddress']);
            $mail->setReplyTo($emailInfo['ReplyTo']);
        }            
        $mail->setFromName($emailInfo['FromName']);

        $sendgrid->web->send($mail);

I've played with the content type and everything else that I can think of and just cannot find out what is causing the attachments to be corrupted.

  • 写回答

1条回答 默认 最新

  • dongxieli3839 2017-06-08 01:25
    关注

    You need to create an attachment object to add an attachment, you can't use the path directly like you are. SendGrid requires files to be sent as base64 encoded strings.

    You'll need to create the attachment object, you could do this as a method:

    public function getAttachment($path) 
    {
        if (!file_exists($path)) {
            return false;
        }
    
        $attachment = new SendGrid\Attachment;
        $attachment->setContent(base64_encode(file_get_contents($path))); 
        $attachment->setType(mime_content_type($path));
        $attachment->setFilename(basename($path));
        $attachment->setDisposition('attachment');
        return $attachment;
    }
    

    Then add it to your email:

    $attachment = $this->getAttachment($sitedb . $row['AttachmentPath']);
    if ($attachment instanceof SendGrid\Attachment) {
        $mail->addAttachment($attachment);
    }  
    
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行