import smtplib;
from email.mime.text import MIMEText
def send_email(host,username,passwd,send_to,subject,content):
msg = MIMEText( content.encode('utf8'), _subtype = 'html', _charset = 'utf8')
msg['From'] = username #发件人的名字
msg['Subject'] = u'%s' % subject #邮件的主题
msg['To'] = ",".join(send_to)
try:
s = smtplib.SMTP_SSL(host,465)
s.login(username, passwd )
s.sendmail(username, send_to,msg.as_string())
s.close()
except Exception as e:
print ('Exception: send email failed', e)
if __name__ == '__main__':
host = 'smtp.qq.com'
username = '[email protected]'
passwd = 'uhheymgfhmoyegcc'
to_list = ['[email protected]','[email protected]','[email protected]']
subject = "邮件主题"
content = '使用Python发送邮件'
send_email(host,username,passwd,to_list,subject,content)
以上为发送简单邮件的格式,其中465为qq的smtp服务器地址默认的端口,此外当使用QQ邮箱发送时需得在QQ邮箱设置获取验证码。
该邮件有主题以及收件人与发件人,但是显示不太友好
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
from_addr = '[email protected]'
password = 'uhheymgfhmoyegcc'
to_addr = '[email protected]'
smtp_server = 'smtp.qq.com'
msg = MIMEText('hello, send by Python...', 'plain', 'utf-8')
msg['From'] = _format_addr('爱你的文 <%s>' % from_addr)
msg['To'] = _format_addr('么么哒 <%s>' % to_addr)
msg['Subject'] = Header('文对你的问侯', 'utf-8').encode()
try:
server = smtplib.SMTP_SSL(smtp_server, 465)
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
except Exception as e:
print ('Exception: send email failed', e)
上述代码可以自拟收件人与发件人名字,但QQ邮箱貌似不行。
若我们需要发送html文件而非简单的纯文本文件,只需在构建MIMEText对象时将HTML字符串传进去即可,
msg = MIMEText('Hello
' +
'send by Python...
' +
'', 'html', 'utf-8')
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
import smtplib
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
from_addr = '[email protected]'
password = 'uhheymgfhmoyegcc'
to_addr = '[email protected]'
smtp_server = 'smtp.qq.com'
#发送附件用MIMEMultipart代表邮件本身 然后往里面添加MIMEText作为邮件正文
#再继续往里面加上表示附件的MIMEBase对象即可
msg = MIMEMultipart()
msg['From'] = _format_addr('爱你的文 <%s>' % from_addr)
msg['To'] = _format_addr('小仙女收 <%s>' % to_addr)
msg['Subject'] = Header('文的问侯', 'utf-8').encode()
# 邮件正文是MIMEText:
msg.attach(MIMEText('send with file...', 'plain', 'utf-8'))
# 添加附件就是加上一个MIMEBase,从本地读取一个图片:
with open(r'C:\Users\lpw007\Desktop\1.jpg', 'rb') as f:
# 设置附件的MIME和文件名,这里是png类型:
mime = MIMEBase('image', 'png', filename='1.jpg')
# 加上必要的头信息:
mime.add_header('Content-Disposition', 'attachment', filename='1.jpg')
mime.add_header('Content-ID', '<0>')
mime.add_header('X-Attachment-Id', '0')
# 把附件的内容读进来:
mime.set_payload(f.read())
# 用Base64编码:
encoders.encode_base64(mime)
# 添加到MIMEMultipart:
msg.attach(mime)
try:
server = smtplib.SMTP_SSL(smtp_server, 465)
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
except Exception as e:
print ('Exception: send email failed', e)
若想让图片在正文中显示,只需要将红色字体变为
# 邮件正文是MIMEText:
msg.attach(MIMEText('加油
' +
'' +
'', 'html', 'utf-8'))