php - 未找到 Sendgrid 附件资源
问题描述
电子邮件是从相同的代码发送的,现在我在添加附件代码后从 sandgrid api 收到“找不到资源”错误,“sendgrid/sendgrid”:“~6.0”和“laravel/framework”:“5.4.*”,
$from = new SendGrid\Email($data['from_name'], $data['from']);
$subject = $data['subject'];
$to = new SendGrid\Email("user","user.name@domain.name");
$content = new SendGrid\Content("text/html", $data['view']);
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail();
//Attachment code start
if(isset($data['attach_files']) && is_array($data['attach_files']) && count($data['attach_files'])>=1){
foreach($data['attach_files'] as $attach_files_i=>$attach_files_path){
$filename = basename($attach_files_path);
$file_encoded = base64_encode(file_get_contents($attach_files_path));
//echo '<pre>--$filename';print_r($filename);echo '</pre>'; //working fine
//echo '<pre>--$file_encoded';print_r($file_encoded);echo '</pre>'; //working fine
//echo '<pre>--mime_content_type($attach_files_path)';print_r(mime_content_type($attach_files_path));echo '</pre>'; //working fine
$attachment = new SendGrid\Attachment();
$attachment->setType(mime_content_type($attach_files_path));
$attachment->setContent($file_encoded);
$attachment->setDisposition("attachment");
$attachment->setFilename($filename);
$response->addAttachment($attachment);
}
}
//Attachment code end
$response = $response->send()->post($mail);
echo "<pre>"; print_r($response); die;
来自 sendgrid 的响应
SendGrid\Response Object
(
[statusCode:protected] => 404
[body:protected] => {"errors":[{"field":null,"message":"resource not found"}]}
[headers:protected] => Array
(
[0] => HTTP/1.1 404 NOT FOUND
[1] => Server: nginx
[2] => Date: Wed, 15 Apr 2020 18:43:55 GMT
[3] => Content-Type: application/json
[4] => Content-Length: 58
[5] => Connection: keep-alive
[6] =>
[7] =>
)
)
解决方案
这可能是错误的类名称空间使用
使用这个命名空间
use SendGrid\Mail\Attachment;
推荐阅读
- google-app-engine - Google App Engine 中的实际应用二进制限制是多少?
- qt - 如果静态构建,QML 会崩溃
- ios - 如何像应用商店安装按钮一样让小振动?
- python - Django模板中显示的时间错误
- graphql - GraphQL 是否将 Int 作为枚举
- mongodb - 删除后无法恢复 MongoDB
- java - 如何在 Java 中编写 hashcode() 方法?
- asp.net - 如何在 asp.net 中以 4*2 表格布局显示数据列表?
- python - 使用 contextvars 而不是包装类来存储附加数据
- python - 气流火花提交操作员 - 没有这样的文件或目录