To send emails using the AWS SES API, you need to set up AWS credentials and obtain access keys.
- Go to AWS.
- Sign up for an account if you don't already have one.
- Navigate to the IAM service in the AWS Management Console.
- Click on "Users" and then "Add user".
- Enter a user name and select "Programmatic access" under "Access type".
- Click "Next: Permissions" and attach the "AmazonSESFullAccess" policy (or create a custom policy with necessary permissions).
- Click "Next: Tags", "Next: Review", and then "Create user".
- Download the generated AWS access key ID and secret access key.
accessKeyID := "your-aws-access-key-id"
secretAccessKey := "your-aws-secret-access-key"
region := "your-aws-region"
sender := "[email protected]"
// Replace these with your actual values (in production, retrieve from a secure file or secret manager)
accessKeyID := "your-aws-access-key-id"
secretAccessKey := "your-aws-secret-access-key"
region := "your-aws-region"
sender := "[email protected]"
// Create SESEmailSender with the credentials
emailSender, err := NewSESEmailSender(region, sender, accessKeyID, secretAccessKey)
if err != nil {
log.Fatalf("Failed to create email sender: %v", err)
}
// Read attachment content
attachmentContent, err := os.ReadFile("path/to/attachment.jpg")
if err != nil {
log.Fatalf("Failed to read attachment: %v", err)
}
// Define email message
message := gomail.NewEmailMessage(sender,[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").
AddAttachments(*gomail.NewAttachment("attachment.jpg", attachmentContent))
// Send email
if err := emailSender.SendEmail(message); err != nil {
log.Fatalf("Failed to send email: %v", err)
}
fmt.Println("Email sent successfully")