只适用于本机向外发送邮件(前提是本机已经配置好邮箱设置,能正常收发邮件)
1、写一个通用的发送邮件的调用模块的脚本。
EmailCommon.py
#!/usr/bin/env python # -*- coding:UTF-8 -*-
import os class EmailCommon: def __init__(self, debug=False): self.debug = debug def email(self, from_address, to_addresses, subject, body, server='localhost', mimetext='plain'): if type(to_addresses) != list: to_addresses = [to_addresses] import smtplib from email.mime.text import MIMEText msg = MIMEText(body, mimetext, 'utf-8') msg['Subject'] = subject msg['From'] = from_address msg['To'] = ','.join(to_addresses) msg['Accept-Language'] = "zh-CN" msg['Accept-Charset'] = "ISO-8859-1,utf-8" s = smtplib.SMTP(server) s.sendmail(from_address, to_addresses, msg.as_string()) s.quit()
2、发送邮件的脚本。
email_server.py
from email_server import EmailCommon #从 email_server 文件导入 EmailCommon 类模块 emailServer = EmailCommon() #实例化 address = 'test@163.com'
to_address = 'test2@163.com'
subject = "Welocom..." body = """ Just only test mail. """ emailServer.email(address,[to_address,'12345@qq.com'],subject,body)