Last active
August 9, 2018 17:07
-
-
Save sendgrid-gists/bd1b8a0b2adce2acf72aa8ecb82b51a5 to your computer and use it in GitHub Desktop.
v3 "Hello World" for email, using SendGrid with PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} |
@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!
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
How to send emails with attachments?
I tried with this but no luck.