3
2

More than 3 years have passed since last update.

【AWS】AWSのSESとnodemailerを使って、独自ドメインからメールを送信してみた

Posted at

皆さんこんにちは!

最近AWSの様々な機能に触れるようになったのですが、何と言っても便利!!!

こういう機能無いかなと調べたらほぼ確実にある!

そんなこんなで今回はAWSのSES(Simple Email Service)を使って、nodemailerで独自ドメインからメールを送信して見ようと思います!

はじめに

独自ドメインは取得しているていで話を進めていきます。

また、SESでの設定も今回は省きます。

SESでメール送信の設定を行っていないという方は、下記の記事をご覧ください。

【AWS】AWS初心者がRoute53+SESを使って、独自ドメインでGmailからメールを送信してみた

それでは説明を見ていきましょう!

nodemailerの準備

まずは、パッケージをインストールします。

npm i nodemailer

次に、SESでメールの登録を行った際に、ユーザー名、パスワードの情報をダウンロードしているかと思います。

していない場合はもう一度設定を行ってSESのユーザー名、パスワードが記載してあるファイルをダウンロードしてください。

それらのユーザー名とパスワードを以下のusernamepasswordに定義してください。

また、ホスト名もSESで作成したリージョンを指定して定義してください。

index.js
  const userName = 'ユーザー名'
  const password = 'パスワード'
  const mailAddress = 'info@example.com'
  const host = 'email-smtp.<リージョン(例:ap-northeast-1)>.amazonaws.com'

そして、この情報を元に以下のようにtransporterを作成します。

index.js
  const userName = 'ユーザー名'
  const password = 'パスワード'
  const mailAddress = 'info@example.com'
  const host = 'email-smtp.<リージョン(例:ap-northeast-1)>.amazonaws.com'

  const transporter = nodemailer.createTransport({
    host: host,
    port: 587,
    secure: false,
    auth: {
      user: userName,
      pass: password
    }
  })

メールオプションの設定

次に、メール内容を決めます。

index.js
  const userName = 'ユーザー名'
  const password = 'パスワード'
  const mailAddress = 'info@example.com'
  const host = 'email-smtp.<リージョン(例:ap-northeast-1)>.amazonaws.com'

  const transporter = nodemailer.createTransport({
    host: host,
    port: 587,
    secure: false,
    auth: {
      user: userName,
      pass: password
    }
  })

  const mailOptions = {
    from: mailAddress,
    to: 'example@gmail.com',
    subject: 'お問い合わせ内容の確認',
    html: `
      <p>テスト</p>
      `
  }

meilOptionsにメール内容を記述してください!

メールの送信

最後にメールを送信してみましょう!

index.js
    const userName = 'ユーザー名'
  const password = 'パスワード'
  const mailAddress = 'info@example.com'
  const host = 'email-smtp.<リージョン(例:ap-northeast-1)>.amazonaws.com'

  const transporter = nodemailer.createTransport({
    host: host,
    port: 587,
    secure: false,
    auth: {
      user: userName,
      pass: password
    }
  })

  const mailOptions = {
    from: mailAddress,
    to: 'example@gmail.com',
    subject: 'お問い合わせ内容の確認',
    html: `
      <p>テスト</p>
      `
  }

  return transporter.sendMail(mailOptions, (erro, info) => {
    if (erro) {
      return {
        error: erro.toString(),
        message: 'To send mail is failed'
      }
    }
    return {
      message: 'To send mail is successed'
    }
  })

このようにしてSESを使って、独自ドメインでメールを送信することができます。

そこまで難しくないとは思います!

メールが届かない場合は認証情報が間違っている可能性が高いです。例えばユーザー名とかパスワードとか。

それでも送信されない場合はSESでの設定を疑ってみてください!

以上、「【AWS】AWSのSESとnodemailerを使って、独自ドメインからメールを送信してみた」でした!

また、何か間違っていることがあればご指摘頂けると幸いです。

他にも初心者さん向けに記事を投稿しているので、時間があれば他の記事も見て下さい!!

あと、最近「ココナラ」で環境構築のお手伝いをするサービスを始めました。

気になる方はぜひ一度ご相談ください!

Thank you for reading

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2