【发布时间】: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