Skip to content

Conversation

timstl
Copy link
Contributor

@timstl timstl commented Dec 11, 2022

Adds the ability to attach files to a SendGrid email (https://github.com/sendgrid/sendgrid-php/blob/main/USE_CASES.md#attachments) with new attach() function.

attach() accepts an array of attachments, allowing for multiple files. Each attachment array should contain the content, type, filename, and (optionally) the disposition.

The file content should be base64 encoded prior to attaching. I found that without doing this, the queued jobs were unable to json_encode the payload and failed.

Example:

// At this point $content has been created using file_get_contents, by generating a PDF, or another means.
// Pass this $attachments array into a Notification class with the payload, templateId, etc.
$attachments = array(
    array(
        'content' => base64_encode($content),
        'type' => 'application/pdf',
        'filename' => 'filename.pdf'
    )
);


// In Notification class, pass them to SendGridMessage's attach function.
public function toSendGrid($notifiable)
{
    $email = ( new \NotificationChannels\SendGrid\SendGridMessage($this->templateId) );
    $email->payload($this->payload);

    if ($this->attachments && !empty($this->attachments)) {
        $email->attach($this->attachments);
    }

    return $email;
}

@aozisik aozisik changed the base branch from master to dev December 12, 2022 11:51
@aozisik
Copy link
Contributor

aozisik commented Dec 12, 2022

Thank you for the contribution @timstl. Attachments is a nice addition! I'm seeing some potential improvements like supporting different ways of attaching / embedding files. I'll merge this to dev and work on it a bit as well.

@aozisik aozisik merged commit 2c72004 into swiftmade:dev Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants