用python的smtplib可以很方便地发邮件。
linux下的调用例子:
./send.sh "*@*;*@*" "title" "content"
shell, send.sh:
#!/bin/sh
HOST=`hostname`
SENDMAIL=send_mail.py
MAIL_HOST="smtp.*"
MAIL_USER="*"
MAIL_PASS="*"
MAIL_FROM="*"
MAIL_TO=$1
MAIL_SUBJECT=$2
TEXT=$3
python $SENDMAIL $MAIL_HOST $MAIL_USER $MAIL_PASS $MAIL_FROM "$MAIL_TO" "$MAIL_SUBJECT" "$TEXT" #"hello world"
python, send_mail.py:
#!/usr/bin/python
import sys;
import smtplib;
from email.MIMEText import MIMEText
mail_host = sys.argv[1]
mail_user = sys.argv[2]
mail_pass = sys.argv[3]
mail_from = sys.argv[4]
mail_to = sys.argv[5]
mail_to_list = mail_to.split(';')
mail_subject = sys.argv[6]
mail_content = sys.argv[7]
msg = MIMEText(mail_content, _subtype = 'html', _charset = 'utf-8')
msg['From'] = mail_from
msg['Subject'] = mail_subject
msg['To'] = mail_to
msg['MIME-Version'] = "1.0"
msg['Content-Type'] = "text/html"
try:
client = smtplib.SMTP()
client.connect(mail_host)
client.login(mail_user, mail_pass)
client.sendmail(mail_from, mail_to_list, msg.as_string())
except:
print "something error..."