Skip to content

Instantly share code, notes, and snippets.

@sendgrid-gists
Last active August 9, 2018 17:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sendgrid-gists/bd1b8a0b2adce2acf72aa8ecb82b51a5 to your computer and use it in GitHub Desktop.
Save sendgrid-gists/bd1b8a0b2adce2acf72aa8ecb82b51a5 to your computer and use it in GitHub Desktop.
v3 "Hello World" for email, using SendGrid with PHP.
<?php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("test@example.com", "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>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
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";
}
@paoga87
Copy link

paoga87 commented Feb 17, 2017

@ashwin31:
Try this

$file_to_attach = 'path/to/file.pdf';
$file_encoded = base64_encode(file_get_contents($file_to_attach));

$attachment = new SendGrid\Attachment();
$attachment-> setContent($file_encoded); //You need BASE64 encoding for your file
$attachment-> setType("application/pdf");
$attachment-> setDisposition("attachment");
$attachment-> setFilename("myFile.pdf");
$attachment-> setContentId("quote");

$mail->addAttachment($attachment);

Let me know how it goes!

@iiooiio
Copy link

iiooiio commented Jul 30, 2018

replace
$attachment = new SendGrid\Attachment();
with
$attachment = new SendGrid\Mail\Attachment();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment