LoginSignup
30
42

More than 5 years have passed since last update.

メールを送信するシェルスクリプト

Last updated at Posted at 2015-01-27

口上

よく使うのでメモ。サブジェクト日本語対応。なんとなく関数化。

環境

  • CentOS6
  • yumでpostfixとnkfコマンドが入っている

コード

#!/bin/bash

export PATH=$PATH:/usr/sbin

MAIL_TO="ore@gmail.com"
MAIL_FROM="noreply@hogehoge.com"
SUBJECT="メールテスト"
DATA="データ"

mail_send () {

cat << EOD | nkf -j -m0 | sendmail -t
From: ${MAIL_FROM}
To: ${MAIL_TO}
Subject: ${SUBJECT}
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-2022-JP"
Content-Transfer-Encoding: 7bit


メールが送信されます。
ここに${DATA}が入るよ。

EOD
}

mail_send

exit 0

実行例

$ sh -x mail_send.sh
+ MAIL_TO=ore@gmail.com
+ MAIL_FROM=noreply@hogehoge.com
+ SUBJECT=$'\343\203\241\343\203\274\343\203\253\343\203\206\343\202\271\343\203\210'
+ DATA=$'\343\203\207\343\203\274\343\202\277'
+ mail_send
+ cat
+ nkf -j -m0
+ sendmail -t
+ exit 0

メール受信

gmailだとこんな感じ。
cap01.png

30
42
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
30
42