ssmtpを使用してcentos7からメールを送信する設定
1,ssmtpインストール
yum install ssmtp
※見つからない場合は、epelリポジトリをインストール(有効化)する。
yum --enablerepo=epel install ssmtp
2,コンフィグファイル修正
vi /etc/ssmtp/ssmtp.conf
#/etc/ssmtp.conf -- a config file for sSMTP sendmail.
#See the ssmtp.conf(5) man page for a more verbose explanation of the
#available options.
#The person who gets all mail for userids < 1000
#Make this empty to disable rewriting.
#root=postmaster ← コメントアウト
root=XXXXXXXX@gmail.com ←___アドレス記入___
#The place where the mail goes. The actual machine name is required
#no MX records are consulted. Commonly mailhosts are named mail.domain.com
#The example will fit if you are in domain.com and your mailhub is so named.
#mailhub=mail ←___コメントアウト___
#Example for SMTP port number 2525
#mailhub=mail.your.domain:2525
#Example for SMTP port number 25 (Standard/RFC)
#mailhub=mail.your.domain
#Example for SSL encrypted connection
#mailhub=mail.your.domain:465
mailhub=smtp.gmail.com:587 ←___smtpサーバ追記___
#Where will the mail seem to come from?
#RewriteDomain=
#The full hostname
#Hostname=
AuthUser=XXXXXX@gmail.com ←___ログインID___
AuthPass=XXXXXX ←___パスワード___
# Set this to never rewrite the "From:" line (unless not given) and to
# use that address in the "from line" of the envelope.
FromLineOverride=YES ←___コメントアウト外す___
# Use SSL/TLS to send secure messages to server.
#UseTLS=YES
#IMPORTANT: The following line is mandatory for TLS authentication
TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt ←___コメントアウト外す___
# Use SSL/TLS certificate to authenticate against smtp host.
#UseTLSCert=YES
# Use this RSA certificate.
#TLSCert=/etc/pki/tls/private/ssmtp.pem
# Get enhanced (really enhanced) debugging information in the logs
# If you want to have debugging of the config file parsing, move this option
# to the top of the config file and uncomment
#Debug=YES
UseSTARTTLS=YES ←___追記___
3,メール送信
echo -ne "To: XXXXXX@gmail.com(宛先)\nSubject(件名): TestSend1(本文)\n\n送信テスト1" | ssmtp XXXXXX@gmail.com(宛先)
echo -ne "To: XXXXXXX@gmail.com\nSubject: TestSend1\n\n送信テスト1" | ssmtp XXXXXXX@gmail.com
※UTF-8に変換しないとOutlook等で文字化けするため以下を追加する。
\nContent-Type: text/plain;charset="UTF-8"\n
例:
echo -ne "To: XXXXXXX@gmail.com\nContent-Type: text/plain;charset="UTF-8"\nSubject: TestSend1\n\n送信テスト1" | ssmtp XXXXXXX@gmail.com
※シェルで流すときに「sh」ではなく「bash」で実施する。
「sh」だと宛先や件名にアドレスが入らず本文に記載されてしまう。
#!/bin/sh ×
#!/bin/bash 〇
以上でメール送信できます。