使用msmtp發送郵件的例子

最近在搞一個東西須要作郵件通知,又不想在服務器上多搞個郵件服務.
怎麼辦呢?
可否用各類免費郵箱來發郵件呢?
答案固然是能夠啦!
用什麼客戶端呢?
我想到了msmtp,msmtp配置好後發郵件很是方便.
固然啦!經過msmtp使用免費郵箱發郵件有風險.
發多了,發頻繁了,極可能郵箱就被封.
想當年哥讀書時,以爲msmtp很神奇,
因而乎用msmtp在個人兩個163郵箱之間,
連續發了幾十封郵件.結果兩個郵箱全被網易封了,
說我惡意發郵件.哥,對燈發誓哥絕無惡意!
好了,不說廢話了!看腳本吧!
#!/bin/bash

#       filename: msmtp_send
#       
#       Copyright 2010 wkt <weikting@gmail.com>
#       

#
#注意:
#  1 請不要使用這個腳本亂來
#  2 這個腳本有風險,要你的郵箱由於使用這個腳本中的方法發郵件而被被封殺,請不要怪我!

#使用方法:
#   msmtp_send from@from_domain.com mailto@mailto_domain.com
#
#from@from_domain.com能夠
#支持是gmail,163,hotmail,網易免費企業郵箱,QQ郵箱
#

rc=$(mktemp /tmp/.mailrc.XXXXXXXX)
#rc=/tmp/.mailrc.XXXXXXXX

from='user@my_domain.com'
to='mailto@mailto_domain.com'
smtp_server=smtp.ym.163.com

test -n "$1" && from=$1
test -n "$2" && to=$2
test -n "$3" && smtp_server=$3

get_user()
{
    dn=$(get_domain $1)
    if test "$dn = hotmail.com";then
        echo $1
    else
        echo $1 |sed 's|@.*||g'
    fi
}

get_domain()
{
    echo $1 |sed 's|.*@||g'
}

get_smtp_server()
{
    local dn
    dn=$(get_domain $1)
    if test "$dn" = hotmail.com;then
        echo smtp.live.com
    else
        echo smtp.$dn
    fi
}

cat <<___eof_ > ${rc}
defaults
#keepbcc on

###QQ郵箱不支持tls,使用QQ郵箱須要關閉tls_starttls
#tls_starttls off

tls on

###網易免費企業郵箱的ssl證書通不過驗證,因此使用 網易免費企業郵箱 時,只能關閉tls證書驗證
#tls_certcheck off

syslog on
auth on

#使用 網易免費企業郵箱 時,須要註釋掉tls_trust_file
tls_trust_file /usr/lib/ssl/certs/ca-certificates.crt

account normal
host $(get_smtp_server $from)
from $from
user $(get_user $from)

##能夠在這裏填寫郵箱密碼,固然這樣不是很安全
#password my_password

account qiye
host $smtp_server
from $from
user $from
#password my_password
tls_certcheck off
tls_trust_file

#設置默認msmtp使用的帳號信息
#使用 網易免費企業郵箱,須要把normal改成qiye
account default : normal

___eof_

#更改配置文件權限,權限不對mstp拒絕幹活
chmod og-rwx $rc

send()
{
cat <<__mail_ |msmtp -C $rc $to
TO:$to
FROM:$from
SUBJECT:test msmtp

This is a Test for msmtp!

$(date '+%Y/%m/%d %T.%N')

__mail_
}

send

rm -f ${rc}
相關文章
相關標籤/搜索