邮件发送

一、通过python发邮件步骤:

1.1、前提是:开通了第三方授权,可以使用smtp服务

  1. 创建一个smtp对象
  2. 连接smp服务器,默认端口都是25
  3. 登录自己邮箱账号,
  4. 调用发送消息函数,参数:发件人,收件人,消息内容
  5. 关闭连接

示例:

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com', '25')
smtp.login('18910148469@163.com', '******')
smtp.sendmail('18910148469@163.com', '974644081@qq.com', msg.as_string())
smtp.quit()

1.2、邮件消息注册:

①、首先创建一个消息对象:

msg = email.mime.multipart.MIMEMultipart()
msg['from'] = '18910148469@163.com'
msg['to'] = '974644081@qq.com;1414873973@qq.com;lingjing@jd.com'
msg['subject'] = 'ajing1111‘

分别指明邮件的发件人,收件人, 主题

②、消息内容:

首先,先定义一个字符串,来表示你得消息内容:

context= ‘’’hello world’’’
txt = email.mime.text.MIMEText(_text=content, _subtype="html")
msg.attach(txt)

_subtype这个参数就决定了,你是以html解析的形式去发送,还是以text的形式去发送。

示例:

import smtplib
import email.mime.multipart
import email.mime.text

msg = email.mime.multipart.MIMEMultipart()
msg['from'] = '18910148469@163.com'
msg['to'] = '974644081@qq.com'
msg['subject'] = 'ajing'
content = '''
<h1>老师好</h1>
你好,这是一封自动发送的邮件。
www.ustchacker.com hello
'''
txt = email.mime.text.MIMEText(_text=content, _subtype="html")
msg.attach(txt)

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com', '25')
smtp.login('18910148469@163.com', '******')
smtp.sendmail('18910148469@163.com', '974644081@qq.com', msg.as_string())
smtp.quit()

③、发送附件:

先找一个本地的文件,

打开文件,读出文件字符串,

通过MIMT  ext()类来创建一个对象att,

传入文件读出内容,增加att的头部信息,并指定文件名字,添加到msg消息中msg.attach(att)

attfile = 'test.py'
basename = os.path.basename(attfile)
fp = open(attfile, 'rb')
att = email.mime.text.MIMEText(fp.read(), 'html', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att.add_header('Content-Disposition', 'attachment', filename=('utf-8', '', basename))

# three-tuple of (charset, language, value),
# encoders.encode_base64(att)
msg.attach(att)

示例:

import smtplib
import email.mime.multipart
import email.mime.text
import os
from email import encoders

msg = email.mime.multipart.MIMEMultipart()
msg['from'] = '18910148469@163.com'
msg['to'] = '974644081@qq.com;1414873973@qq.com;lingjing@jd.com'
msg['subject'] = 'ajing1111'
content = '''
<h1>老师好</h1>
你好,这是一封自动发送的邮件。
www.ustchacker.com hello
'''

txt = email.mime.text.MIMEText(_text=content, _subtype="html")
msg.attach(txt)

#############
attfile = 'test.py'
basename = os.path.basename(attfile)
fp = open(attfile, 'rb')
att = email.mime.text.MIMEText(fp.read(), 'html', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att.add_header('Content-Disposition', 'attachment',filename=('utf-8', '', basename))
#three-tuple of (charset, language, value),
# encoders.encode_base64(att)
msg.attach(att)
##########

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com', '25')
smtp.login('18910148469@163.com', '******')
smtp.sendmail(from_addr='18910148469@163.com', to_addrs=['974644081@qq.com', '1414873973@qq.com', 'lingjing@jd.com'], msg=msg.as_string())
smtp.quit()

④、发送图片:

本地必须存在一张图片;

打开图片,并读取图片内容,创建发邮件相对应的图片对象:imgattr = MIMEImage(fimg.read());

增加图片的头信息:imgattr.add_header('Content-ID', '<image1>');

指定了图片的id,图片如果想在正文中显示,必须通过html的格式显示出来,在前端代码中指定图片id:<img src = 'cid:image1'>,添加到message的信息中

示例:

import smtplib
import email.mime.multipart
import email.mime.text
from email.mime.image import MIMEImage
import os

# from email import encoders, MIMEImage

msg = email.mime.multipart.MIMEMultipart()
msg['from'] = '18910148469@163.com'
msg['to'] = '974644081@qq.com;1414873973@qq.com;lingjing@jd.com'
msg['subject'] = 'ajing1111'
content = '''
<h1>老师好</h1>
你好,这是一封自动发送的邮件。
www.ustchacker.com hello
<html>
<body>
<h1>hello world</h1>
<p>
图片演示:<br><img src = 'cid:image1'></br>
</p>
</body>
</html>
'''
txt = email.mime.text.MIMEText(_text=content, _subtype="html")
msg.attach(txt)

#############
attfile = 'test.py'
basename = os.path.basename(attfile)
fp = open(attfile, 'rb')
att = email.mime.text.MIMEText(fp.read(), 'html', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att.add_header('Content-Disposition', 'attachment',filename=('utf-8', '', basename))
# three-tuple of (charset, language, value),
# encoders.encode_base64(att)
msg.attach(att)
##########

#################
img = "1.jpg"
fimg = open(img, "rb")
imgattr = MIMEImage(fimg.read())
fimg.close()
imgattr.add_header('Content-ID', '<image1>')
msg.attach(imgattr)
################

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com', '25')
smtp.login('18910148469@163.com', '******')
smtp.sendmail(from_addr='18910148469@163.com', to_addrs=['974644081@qq.com', '1414873973@qq.com', 'lingjing@jd.com'], msg=msg.as_string())
smtp.quit()

⑤、抄送

示例:

import smtplib
import email.mime.multipart
import email.mime.text

msg = email.mime.multipart.MIMEMultipart()
msg['from'] = '18910148469@163.com'
msg['to'] = '1414873973@qq.com'
msg['subject'] = 'ajing'
msg['cc'] = '974644081@qq.com'
content = '''
<h1>老师好</h1>
'''
txt = email.mime.text.MIMEText(_text=content, _subtype="html")
msg.attach(txt)

smtp = smtplib.SMTP_SSL()
smtp.connect('smtp.163.com', '465')
smtp.login('18910148469@163.com', '******')
smtp.sendmail(from_addr='18910148469@163.com', to_addrs=['1414873973@qq.com'] + ['974644081@qq.com'], msg=msg.as_string())
smtp.quit()

1.3、直接使用别人封装好的第三方库(yagmail模块):

# pip install yagmail
import yagmail
yag=yagmail.SMTP(user='18910148469@163.com',password='******',host='smtp.163.com', port='465' )
yag.send(to='974644081@qq.com',subject="test",contents='this is a testing',attachments='test.py',cc='1414873973@qq.com')

User
:用户民

Password:用户密码,很多情况需要使用授权码
Host:smtp的地址
Port:默认使用ssl协议,默认是465端口
To:收件人
Subject:主题
Contents:消息内容
Attachments:附件
Cc:抄送人

 

posted @ 2018-06-07 00:28  一条咸鱼的梦想  阅读(187)  评论(0编辑  收藏  举报