python 发送邮件

发送邮件

邮箱开启smtp

django发送邮件

  • django配置文件配置
EMAIL_HOST = 'smtp.qq.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = ''  # 帐号
EMAIL_HOST_PASSWORD = ''  # 密码
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
#这样收到的邮件,收件人处就会这样显示
#DEFAULT_FROM_EMAIL = 'lqz<'306334678@qq.com>'
EMAIL_USE_SSL = True
  • 发送邮件 view.py
from rest_framework.views import APIView
from django.core.mail import send_mail as django_send_email
from utils.common_response import APIResponse


class EmailView(APIView):
    def get(self, request, *args, **kwargs):
        to_user = request.query_params.get('to_user')
        django_send_email('邮件标题,非常重要', '内容:你好', settings.EMAIL_HOST_USER, [to_user, ])
        return APIResponse(msg=f'邮件已经发送:{str(to_user)}')
  • 路由
from .views import EmailView
urlpatterns = [
path('email/', EmailView.as_view()),
]

原生python 发送邮件

1 使用SMTP模块发送邮件

import smtplib
from email.mime.text import MIMEText

msg_from = '***@qq.com  # 发送方邮箱
passwd = '****'  # 发送方邮箱的授权码
msg_to = ['***@qq.com']  # 收件人邮箱

subject = "主题: 原生python"  # 主题
content = "邮件内容:我是用python发送信息"
# 生成一个MIMEText对象(还有一些其它参数)
msg = MIMEText(content)
# 放入邮件主题
msg['Subject'] = subject
# 也可以这样传参
# msg['Subject'] = Header(subject, 'utf-8')
# 放入发件人
msg['From'] = msg_from
# 放入收件人
msg['To'] = ','.join(msg_to)

try:
    # 通过ssl方式发送,服务器地址,端口
    s = smtplib.SMTP_SSL("smtp.qq.com", 465)
    # 登录到邮箱
    s.login(msg_from, passwd)
    # 发送邮件:发送方,收件方,要发送的消息
    s.sendmail(msg_from, msg_to, msg.as_string())
    print('成功')
except smtplib.SMTPException as e:
    print(e)
finally:
    s.quit()

2 发送html格式邮件

import smtplib
from email.mime.text import MIMEText

msg_from = '***@qq.com'
passwd = '****'
msg_to = ['***@qq.com']

subject = "主题: 原生python"
content = '''
<p>Python 邮件发送测试...</p>
<p><a href="http://www.baidu.com">这是一个链接</a></p>
'''

msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = ','.join(msg_to)

try:
    s = smtplib.SMTP_SSL("smtp.qq.com", 465)
    s.login(msg_from, passwd)
    s.sendmail(msg_from, msg_to, msg.as_string())
    print('成功')
except smtplib.SMTPException as e:
    print(e)
finally:
    s.quit()

3 发送带附件的邮件

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

msg_from = '***@qq.com'
passwd = '****'
msg_to = ['***@qq.com']

subject = "主题: 原生python"
content = 'Python 邮件发送测试……'

# 创建一个带附件的实例
msg = MIMEMultipart()

msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = ','.join(msg_to)
msg.attach(MIMEText(content))

# 构造附件1, 携带1.txt 文件
att1 = MIMEText(open('1.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1['Content-Disposition'] = 'attachment;filename="1.txt"'
msg.attach(att1)


# 构造附件2, 携带图片
from email.mime.base import MIMEBase
from email import  encoders
with open('1.jpg', 'rb') as f:
    # 设置附件的MIME和文件名,这里是jpg类型:
    mime = MIMEBase('image', 'jpg', 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)
   

# 构造附件3, 携带视频
with open('1.mp4', 'rb') as file:
    video = MIMEBase('application', 'octet-stream')
    video.set_payload(file.read())
    encoders.encode_base64(video)
    video.add_header('Content-Disposition', 'attachment', filename='video.mp4')
    msg.attach(video)    
    
    
# 执行
try:
    s = smtplib.SMTP_SSL("smtp.qq.com", 465)
    s.login(msg_from, passwd)
    s.sendmail(msg_from, msg_to, msg.as_string())
    print('成功')
except smtplib.SMTPException as e:
    print(e)
finally:
    s.quit()

4 发送网页里带图片的邮件

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

msg_from = '***@qq.com'
passwd = '****'
msg_to = ['***@qq.com']

subject = "主题: 原生python"
content = """
<html>
<body>
<h2>这是一封带图片的邮件</h2>
<p>邮件内容:这是一封带有图片的电子邮件。</p>
<p>图片展示:</p>
<img src="cid:image1">
</body>
</html>
"""

msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = ','.join(msg_to)
msg.attach(MIMEText(content,'html'))


with open('1.jpg', 'rb') as file:
    image = MIMEImage(file.read())
    image.add_header('Content-ID', '<image1>')
    msg.attach(image)

try:
    s = smtplib.SMTP_SSL("smtp.qq.com", 465)
    s.login(msg_from, passwd)
    s.sendmail(msg_from, msg_to, msg.as_string())
    print('成功')
except smtplib.SMTPException as e:
    print(e)
finally:
    s.quit()

各大邮箱smtp服务器及端口

新浪邮箱smtp服务器
外发服务器:smtp.vip.sina.com
收件服务器:pop3.vip.sina.com
新浪免费邮件
外发服务器:smtp.sina.com.cn
收件服务器:pop3.sina.com.cn
163邮箱smtp服务器
pop: pop.163.com
smtp: smtp.163.com
QQ邮箱smtp服务器及端口
接收邮件服务器:imap.exmail.qq.com,使用SSL,端口号993
发送邮件服务器:smtp.exmail.qq.com,使用SSL,端口号465或587
yahoo邮箱smtp服务器
接:pop.mail.yahoo.com.cn
发:smtp.mail.yahoo.com
126邮箱smtp服务器
pop: pop.126.com
smtp: smtp.126.com
新浪免费邮箱
POP3:pop.sina.com
SMTP:smtp.sina.com
SMTP端口号:25
新浪VIP邮箱
POP3:pop3.vip.sina.com
SMTP:smtp.vip.sina.com
SMTP端口号:25
新浪企业邮箱
POP3:pop.sina.com
SMTP:smtp.sina.com
SMTP端口号:25
雅虎邮箱
POP3:pop.mail.yahoo.cn
SMTP:smtp.mail.yahoo.cn
SMTP端口号:25
搜狐邮箱
POP3:pop3.sohu.com
SMTP:smtp.sohu.com
SMTP端口号:25
TOM邮箱
POP3:pop.tom.com
SMTP:smtp.tom.com
SMTP端口号:25
Gmail邮箱
POP3:pop.gmail.com
SMTP:smtp.gmail.com
SMTP端口号:587 或 25
QQ邮箱
POP3:pop.exmail.qq.com
SMTP:smtp.exmail.qq.com
SMTP端口号:25
263邮箱
域名:263.net
POP3:263.net
SMTP:smtp.263.net
SMTP端口号:25
域名:x263.net
POP3:pop.x263.net
SMTP:smtp.x263.net
SMTP端口号:25
域名:263.net.cn
POP3:263.net.cn
SMTP:263.net.cn
SMTP端口号:25
域名:炫我型
POP3:pop.263xmail.com
SMTP:smtp.263xmail.com
SMTP端口号:25
21CN 免费邮箱
POP3:pop.21cn.com
SMTP:smtp.21cn.com
IMAP:imap.21cn.com
SMTP端口号:25
21CN 经济邮邮箱
POP3:pop.21cn.com
SMTP:smtp.21cn.com
SMTP端口号:25
21CN 商务邮邮箱
POP3:pop.21cn.net
SMTP:smtp.21cn.net
SMTP端口号:25
21CN 快感邮箱
POP3:vip.21cn.com
SMTP:vip.21cn.com
SMTP端口号:25
21CN Y邮箱
POP3:pop.y.vip.21cn.com
SMTP:smtp.y.vip.21cn.com
SMTP端口号:25
中华网任我邮邮箱
POP3:rwpop.china.com
SMTP:rwsmtp.china.com
SMTP端口号:25
中华网时尚、商务邮箱
POP3:pop.china.com
SMTP:smtp.china.com
SMTP端口号:25
posted @ 2024-05-23 16:44  蓝幻ﹺ  阅读(10)  评论(0编辑  收藏  举报