ses_sendemailsmtp.js - AWS Code Sample


ses_send_email_smtp demonstrates how to send a transactional email message by using the Amazon SES SMTP interface and the Nodemailer module.

/** * Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ /* This code uses callbacks to handle asynchronous function responses. It currently demonstrates using an async-await pattern. AWS supports both the async-await and promises patterns. For more information, see the following: */ "use strict"; const nodemailer = require("nodemailer"); // If you're using Amazon SES in a region other than US West (Oregon), // replace with the Amazon SES SMTP // endpoint in the appropriate AWS Region. const smtpEndpoint = ""; // The port to use when connecting to the SMTP server. const port = 587; // Replace with your "From" address. // This address must be verified with Amazon SES. const senderAddress = "Mary Major <>"; // Replace with a "To" address. If your account // is still in the sandbox, this address must be verified. To specify // multiple addresses, separate each address with a comma. var toAddresses = ""; // CC and BCC addresses. If your account is in the sandbox, these // addresses have to be verified. To specify multiple addresses, separate // each address with a comma. var ccAddresses = ","; var bccAddresses = ""; // Replace smtp_username with your Amazon SES SMTP user name. const smtpUsername = "AKIAIOSFODNN7EXAMPLE"; // Replace smtp_password with your Amazon SES SMTP password. const smtpPassword = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"; // (Optional) the name of a configuration set to use for this message. var configurationSet = "ConfigSet"; // The subject line of the email var subject = "Amazon SES test (Nodemailer)"; // The email body for recipients with non-HTML email clients. var body_text = `Amazon SES Test (Nodemailer) --------------------------------- This email was sent through the Amazon SES SMTP interface using Nodemailer. `; // The body of the email for recipients whose email clients support HTML content. var body_html = `<html> <head></head> <body> <h1>Amazon SES Test (Nodemailer)</h1> <p>This email was sent with <a href=''>Amazon SES</a> using <a href=''>Nodemailer</a> for Node.js.</p> </body> </html>`; // The message tags that you want to apply to the email. var tag0 = "key0=value0"; var tag1 = "key1=value1"; async function main(){ // Create the SMTP transport. let transporter = nodemailer.createTransport({ host: smtpEndpoint, port: port, secure: false, // true for 465, false for other ports auth: { user: smtpUsername, pass: smtpPassword } }); // Specify the fields in the email. let mailOptions = { from: senderAddress, to: toAddresses, subject: subject, cc: ccAddresses, bcc: bccAddresses, text: body_text, html: body_html, // Custom headers for configuration set and message tags. headers: { 'X-SES-CONFIGURATION-SET': configurationSet, 'X-SES-MESSAGE-TAGS': tag0, 'X-SES-MESSAGE-TAGS': tag1 } }; // Send the email. let info = await transporter.sendMail(mailOptions) console.log("Message sent! Message ID: ", info.messageId); } main().catch(console.error);

Sample Details

Service: Amazon SES

Last tested: 2020-06-17

Author: AWS

Type: full-example