使用SendGrid PHP发送文件附件(sendgrid-php库)

App*_*kie 3 php email email-attachments sendgrid

当我尝试使用SendGrid PHP库(https://github.com/sendgrid/sendgrid-php)发送附件时,该功能失败(白屏).删除"setAttachment"行使其再次起作用.

这是我的代码:

require "sendgrid-php/sendgrid-php.php";
function sendgrid() {

   $recips = array("me@mydomain.ca");
   $categories = array("test");



   $sendgrid = new SendGrid("API key removed");

   $email = new SendGrid\Email();
   $email
   ->setSmtpapiTos($recips)
   ->setFrom('mailroom@mydomain.ca')
   ->setSubject('Testing Sendgrid')
   ->setText('Hello World! Testing...')
   ->setHtml('<strong>Hello World!</strong>')
   ->setCategories($categories)
   ->setAttachment('test.txt')
   ;


   //$sendgrid->send($email);

   $res = $sendgrid->send($email);

   var_dump($res);
}

sendgrid();
Run Code Online (Sandbox Code Playgroud)

据我所知,我正在关注文档,但我想知道我是否没有正确格式化文件的路径."Test.txt"与包含上述代码的文件位于同一目录中.

有人可以提供任何建议吗?

小智 9

试试这个

 ->setAttachment(realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'test.txt');
Run Code Online (Sandbox Code Playgroud)