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

时间:2017-06-08 01:03:36

标签: php attachment sendgrid corrupt

我正在尝试通过PHP向SendGrid发送附件,但在打开附件时不断收到损坏的文件错误。错误消息"我们很抱歉。我们无法打开' file.docx'因为我们发现其内容存在问题"当我点击错误的详细信息时,我看到"文件已损坏,无法打开"

我的代码如下所示:

$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);

我玩过内容类型以及我能想到的所有其他内容,但却找不到导致附件损坏的原因。

1 个答案:

答案 0 :(得分:1)

您需要创建附件对象以添加附件,您不能像您一样直接使用该路径。 SendGrid要求将文件作为base64编码的字符串发送。

您需要创建附件对象,您可以将其作为方法:

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

然后将其添加到您的电子邮箱中:

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