【问题标题】:PHP: SendGrid mail attachments from S3 serverPHP:来自 S3 服务器的 SendGrid 邮件附件
【发布时间】:2017-12-04 05:48:07
【问题描述】:

我需要能够将存储在 Amazon S3 服务器上的文件作为附件发送到使用 SendGrid 创建的电子邮件中。

我的问题是我不是网络开发专家,我能找到的稀疏 PHP 示例对我没有多大帮助。

我是否需要将文件从 S3 服务器下载到本地 /tmp 目录并以这种方式将它们添加为附件,或者我可以从 FileController 传递文件的正文并以这种方式将其作为附件插入?

我不确定从哪里开始,但这是我目前所做的:

$attachments = array();
// Process the attachment_ids
foreach($attachment_ids as $attachment_id) {
        // Get the file if it is attached to the Activity
        if (in_array($attachment_id, $activity_file_ids)) {
                $file = File::find($attachment_id);
                $fileController = new FileController($this->_app);
                $fileObject = $fileController->getFile($attachment_id);
error_log(print_r($fileObject, true));
                $attachment = array();
                $attachment['content'] = $fileObject;
                $attachment['type'] = $fileController->mime_content_type($file->file_ext);
                $attachment['name'] = explode(".", $file->filename, 2)[0];
                $attachment['filename'] = $file->filename;
                $attachment['disposition'] = "inline";
                $attachment['content_id'] = '';
        }
}

我的下一步是将 $attachment 数组推送到 $attachments 数组。一旦 $attachments 完成,遍历它并将每个 $attachment 添加到 SendGrid 电子邮件对象(电子邮件在没有附件的情况下工作正常,顺便说一句。)

问题是,我不确定我是否走在正确的道路上,或者是否有更短、更整洁(且有效)的方法?

FileController->getFile() 本质上是这样做的:

$file = $this->_s3->getObject(array(
    'Bucket' => $bucket,
    'Key'    => $filename,
));
return $file['Body'];

任何帮助(尤其是代码示例)将不胜感激!

【问题讨论】:

    标签: php email amazon-s3 attachment sendgrid


    【解决方案1】:

    好的,我现在有一个可行的解决方案 - 这是代码:

    // Process the attachment_ids
    foreach($attachment_ids as $attachment_id) {
            // Get the file if it is attached to the Activity
            if (in_array($attachment_id, $activity_file_ids)) {
                    // Get the file record
                    $file = File::find($attachment_id);
                    // Get an instance of FileController
                    $fileController = new FileController($this->_app);
                    // Set up the Attachment object
                    $attachment = new \SendGrid\Attachment();
                    $attachment->setContent(base64_encode($fileController->getFile($attachment_id)));
                    $attachment->setType($fileController->mime_content_type($file->file_ext));
                    $attachment->setFilename($file->filename);
                    $attachment->setDisposition("attachment");
                    $attachment->setContentId($file->file_desc);
                    // Add the attachment to the mail
                    $mail->addAttachment($attachment);
             }
    }
    

    不知道它是否会帮助其他人,但确实如此。解决方案是从 S3 服务器获取文件并将 base64_encode($file['Body']) 传递给实例化 Attachment 对象的 setContent 函数,同时为其设置一些其他字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多