Reputation: 55
When I try to send an attachment using the SendGrid PHP library (https://github.com/sendgrid/sendgrid-php), the function fails (white screen). Removing the "setAttachment" line makes it work again.
Here is my code:
require "sendgrid-php/sendgrid-php.php";
function sendgrid() {
$recips = array("[email protected]");
$categories = array("test");
$sendgrid = new SendGrid("API key removed");
$email = new SendGrid\Email();
$email
->setSmtpapiTos($recips)
->setFrom('[email protected]')
->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();
As far as I can tell, I'm following the documentation, but I wonder if I haven't formatted the path to the file correctly. "Test.txt" is in the same directory as the file that contains the above code.
Can anyone offer any suggestions?
Upvotes: 3
Views: 8568
Reputation: 2546
this doesn't really belong here but as of April 17th 2019 SendGrid broke their postfix
mechanism and enforced v3 PHP client without "proper" documentation. And I mean: nothing is properly documented so (since this post came up top of google when I typed in SendGrid - here goes)
so if you see this.. do not panic this post will help u
invalid authentication method - declined because you are using basic authentication with 2FA enabled. to fix, update to using an API key or disable 2FA and switch to using IP Access Management for security.
Some problems with the docs
SendGrids docs on v3 are (in my opinion)
for example : (just a flavour) here we see "they" (SendGrid) simply took out \SendGrid\Email() and replaced it with \SendGrid\Mail\Mail() in their code base but didn't update their docs - so their posted examples will not work. - it's very minor as a change - but as written the callers are different and they never updated their examples.. with everything else they omitted it is making very hard work of a easy thing.
i.e. THIS EXAMPLE WORKS v3
$sendgrid = new SendGrid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
$email = new \SendGrid\Mail\Mail();
$email->setFrom("[email protected]", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("[email protected]", "Example User");
$email->addContent(
"text/plain", "and easy to do anywhere, even with PHP"
);
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
//optional (seems to be bullet proof, pdf, png, etc) to attach a file<START>
$att1 = new \SendGrid\Mail\Attachment();
$att1->setContent(file_get_contents("/path/to/some/test_attach.txt"));
$att1->setType("application/octet-stream");
$att1->setFilename(basename("/path/to/some/test_attach.txt"));
$att1->setDisposition("attachment");
$email->addAttachment($att1);
//optional to attach a file</END>
try {
$response = $sendgrid->send($email);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
}
catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}`
TO UPGRADE TO v3 SEND GRID PHP CLIENT without composer on PHP 5.6
if you're like me and hate composer (don't even get me started) THE GOOD NEWS IS you can get SendGrid to work so- just so you know .. almost everything "they" (SendGrid) direct you to online is going to struggle to help you
After April 17th 2019 SendGrid said you NEED to use v3 YOU WILL NEED PHP 5.6. (rightly or wrongly PHP 5.4 is the ceiling of redhat/centos at time of writing) and YOU CANNOT USE postfix anymore (despite them never specifically saying this nor labeling their postfix examples v2 max, i.e. disabled as of 17th April 2019)
So as of 17th April 2019
-> you have to use v3
-> you have to use the PHP client... (no postfix)
-> you have to upgrade to 5.6
-> you **DO NOT** have to use composer (but if you don't you need to pay attention, see below)
ALSO -> (IMPORTANT) you are stuck with the client or the curl at v3 (they don't simply say this) no postfix. forget it. also re: the Curl mechanism (while it does work with 5.4) carries a) no documentation on using attachments AND b) utilizes illegal json_encoding PHP cannot output ( PHP cannot give "{[{}]}" it's always "{"0":[{}]}")
but regardless not only MUST you use the SendGrid PHP client at 5.6 PHP (again no, server postfix option) you still need to know you need TWO projects: "php-http-client-master" and "sendgrid-php-master" (BOTH repos) and this is undocumented: then you need to do the following: and the /lib/loader.php
in "sendgrid-php-master" needs these two lines adding at the end
require_once '/your/path/to/php-http-client-master/lib/Client.php';
require_once '/your/path/to/php-http-client-master/lib/Response.php';
you also need to add this (sadly more sloppiness from SendGrid)
require_once __DIR__ . '/mail/TypeException.php';
Incidentally I also edited sendgrid-php-master\sendgrid-php.php
to this
<? php
require __DIR__ . '/lib/loader.php';
?>
theoretically adding composer would mean you could probably avoid all this but for some servers this is out of the question (implementing composer on a different branch of my PHP project took 3 weeks - I am not repeating this) since composer introduces a breaking change in the way PHP handles includes and class autoloading
PS: I have alerted SendGrid to all of this you may well find that this is cleared up pretty fast
Upvotes: 2
Reputation: 774
try this
->setAttachment(realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'test.txt');
Upvotes: 9