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";
}
@ashwin31
Copy link

ashwin31 commented Dec 13, 2016

How to send emails with attachments?

I tried with this but no luck.

<?php
require 'sendgrid/vendor/autoload.php';

$subject="Contact us";

$body= "<html><body>hi, this is test mail</body></html>";

$from = new SendGrid\Email("Quote", "mail@mail.com");
$to = new SendGrid\Email("", 'mail@mail.com');
$content = new SendGrid\Content("text/html", $body);
$mail = new SendGrid\Mail($from, $subject, $to, $content);
if(isset($_FILES["file1"]))
{
	$dir = "uploads/";
	$file = $dir . basename($_FILES["file1"]["name"]);
	move_uploaded_file($_FILES["file1"]["tmp_name"], $file);

	$attachment = new SendGrid\Attachment();
    $attachment->setContent(file_get_contents($file));
    $attachment->setType(mime_content_type($file));
    $attachment->setFilename(basename($_FILES["file1"]["name"]));
    $attachment->setDisposition("attachment");
    $attachment->setContentId("quote");
    $mail->addAttachment($attachment);

}
$sg = new \SendGrid("key");

$response = $sg->client->mail()->send()->post($mail);
?>
```php

I have added required code but not all of my stuff.

its working if i don't attach files.
uploaded files are going to upload folder but not to emails.

@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