为了账号安全,请及时绑定邮箱和手机立即绑定

未找到 Sendgrid 附件资源

未找到 Sendgrid 附件资源

PHP
侃侃无极 2022-12-23 15:48:20
电子邮件是从相同的代码发送的,现在我在添加附件代码后从 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 startif(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;
查看完整描述

2 回答

?
互换的青春

TA贡献1797条经验 获得超6个赞

删除函数 base64_encode 是错误的


$attachment = new \SendGrid\Attachment();

$attachment->setContent(file_get_contents($attach_files_path));

$attachment->setType(mime_content_type($attach_files_path));

$attachment->setFilename($filename);

$attachment->setDisposition("attachment");

$mail->addAttachment($attachment);


查看完整回答
反对 回复 2022-12-23
?
DIEA

TA贡献1820条经验 获得超2个赞

这可能是错误的类命名空间使用

使用这个命名空间

use SendGrid\Mail\Attachment;


查看完整回答
反对 回复 2022-12-23
  • 2 回答
  • 0 关注
  • 56 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信