AWS SES SDK send email with attachments

15,330

To send attachments, use the SendRawEmail API instead of SendEmail. AWS documentation will generally refer to this as constructing a 'raw message' instead of explicitly calling out how to send attachments.

Example

From the AWS SDK for Go API Reference, linked below:

params := &ses.SendRawEmailInput{
    RawMessage: &ses.RawMessage{ // Required
        Data: []byte("PAYLOAD"), // Required
    },
    ConfigurationSetName: aws.String("ConfigurationSetName"),
    Destinations: []*string{
        aws.String("Address"), // Required
        // More values...
    },
    FromArn:       aws.String("AmazonResourceName"),
    ReturnPathArn: aws.String("AmazonResourceName"),
    Source:        aws.String("Address"),
    SourceArn:     aws.String("AmazonResourceName"),
    Tags: []*ses.MessageTag{
        { // Required
            Name:  aws.String("MessageTagName"),  // Required
            Value: aws.String("MessageTagValue"), // Required
        },
        // More values...
    },
}
resp, err := svc.SendRawEmail(params)

Further Reading

Share:
15,330
i.van
Author by

i.van

Professional Backend Developer with more than 5 years of experience.

Updated on June 11, 2022

Comments

  • i.van
    i.van almost 2 years

    I'm using the official AWS Golang SDK to integrate with SES but can't find any information about how to add some attachments (pdf file represented as []byte in code) to the email.

    Could you help me?

    The current email sending code looks like this:

    sesEmailInput := &ses.SendEmailInput{
        Destination: &ses.Destination{
            ToAddresses: []*string{aws.String("To address")},
        },
        Message: &ses.Message{
            Subject: &ses.Content{
                Data: aws.String("Some text"),
            },
            Body: &ses.Body{
                Html: &ses.Content{
                    Data: aws.String("Some Text"),
                },
            },
        },
        Source: aws.String("From address"),
        ReplyToAddresses: []*string{
            aws.String("From address"),
        },
    }
    if _, err := s.sesSession.SendEmail(sesEmailInput); err != nil {
        return err
    }
    
  • i.van
    i.van almost 7 years
    Thank you so much! Did I understood correctly that PAYLOAD must be base64 encoded? Also, where can I see which MIME headers are required?
  • Anthony Neace
    Anthony Neace almost 7 years
    The SDK should base64 encode Data in RawMessage for you (reference), and for more info on headers check here: docs.aws.amazon.com/ses/latest/DeveloperGuide/email-format.h‌​tml
  • Elmatsidis Paul
    Elmatsidis Paul over 2 years