I have a simple SendGrid API setup but I am wondering how I can include attachments in the email that gets sent. I am passing the attachment URL into the $_POST variable so can return the full path but unsure where to include it in my setup.
//
$i = 1; foreach ($cart as $download) {
$file . $i = $download['download'];
$file_encoded . $i = base64_encode(file_get_contents($file));
$attachment . $i = new SendGrid\Attachment();
$attachment . $i->setContent($file_encoded);
$attachment . $i->setType("application/zip");
$attachment . $i->setDisposition("attachment");
$attachment . $i->setFilename("test.zip");
$mail->addAttachment($attachment . $i);
i++;
}
// Set email confirmation settings
$email_subject = 'New order from ' . $user_details['first-name-billing'] . ' ' . $user_details['last-name-billing'] . ' via www.***.com';
$email_admin = '******@***.com'; // Client
$email_customer = $user_details['email'];
ob_start();
require_once './email-confirmation.php';
$email_body = ob_get_contents();
ob_end_clean();
// Send to confirmation to admin
if ($email_admin) {
send_email($email_admin, $email_admin, $email_subject, $email_body);
}
// Send confirmation to customer
if ($email_customer) {
send_email($email_admin, $email_customer, $email_subject, $email_body);
}
// SendGrid init
function send_email($from_email, $to_email, $subject, $body) {
global $sgAPIKey;
$from = new SendGrid\Email(null, $from_email);
$to = new SendGrid\Email(null, $to_email);
$content = new SendGrid\Content("text/html", $body);
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$sg = new \SendGrid($sgAPIKey);
$response = $sg->client->mail()->send()->post($mail);
}
SendGrid-PHP library helps you in a quick SMTP setting to send emails through SendGrid using PHP. SendGrid-PHP by SendGrid is the official PHP wrapper to send emails through the SendGrid API. Use MailGet – email service provider for Sending Emails, just connect and send with SenGrid API or SMTP services.
You're sending emails from your Node.js app with the Twilio SendGrid API. Now you want to attach files to your emails. The Twilio SendGrid API makes it very straightforward to include attachments to the emails that you send.
In this post, we’ll attach a pdf document to an email sent via SendGrid. If you have not sent your first email with SendGrid, my colleague Sam has written a post about it, or if you prefer video my colleague Brent has published a video about it. We’ll be picking up where that post ended.
The library does not officially support the V2 API, but you can use V2 with an older version of the library. For more information, see Continue Using V2 in PHP. If you choose not to use SendGrid's client library you may use PHP's cURL function to query the web API.
Use the SendGrid\Attachment
class. A complete example can be found in the documentation:
$filename = basename($url);
$file_encoded = base64_encode(file_get_contents($url));
$attachment = new SendGrid\Attachment();
$attachment->setType("application/text");
$attachment->setContent($file_encoded);
$attachment->setDisposition("attachment");
$attachment->setFilename($filename);
$mail->addAttachment($attachment);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With