ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python发送邮件的简单使用

2022-03-01 10:00:52  阅读:157  来源: 互联网

标签:python send port 发送 import msg mail email 邮件


主代码如下:

点击查看代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/12/26 9:46 上午
# @Author  : Ayden
# @Site    : 
# @File    : send_email.py
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
import smtplib
import pathlib
from conf.setting import *
from common.log import Log

log = Log().log()

def send_mail(host="xxxxxx", port=None, title=None, send_files=None, content=None):
    if not content:
        title = "车联网接口自动化测试报告"
    if not content:
        content = "     \n\n附件为车联网云平台接口自动化测试报告,请使用浏览器进行查看,访问地址为:\n" \
                  "     http://{}:{}\n" \
                  "\n\n本邮件为自动发出,请勿回复".format(host, port)
    mail_host = send_email["mail_host"]
    mail_port = send_email["mail_port"]
    mail_user = send_email["mail_user"]
    mail_password = send_email["mail_pass"]
    # log.debug("发件信息为:{}".format(send_email))
    try:
        msg = MIMEMultipart()
        msg['Subject'] = title
        msg['To'] = ",".join(receive_email)
        print(msg['To'])
        msg['From'] = mail_user
        body = MIMEText(content, 'plain', 'utf-8')
        if send_files:
            #for send_file in send_files:
            part_attach = MIMEApplication(open(send_files, 'rb').read())  # 打开附件
            # 为附件命名
            part_attach.add_header('Content-Disposition', 'attachment', filename=pathlib.Path(send_files).name)
            # 添加附件
            msg.attach(part_attach)
        msg.attach(body)
        smtp = smtplib.SMTP(mail_host, mail_port)
        smtp.starttls()
        smtp.login(mail_user, mail_password)
        smtp.sendmail(mail_user, receive_email, msg.as_string())
        # log.debug("邮件发送成功")
        smtp.quit()
    except Exception as e:
        log.debug("邮件发送失败,原因是{}".format(e))

其中setting配置如下:

点击查看代码
# 发件人电子邮箱
send_email = {"mail_host": "smtp.163.com", "mail_port": 25, "mail_user": "api_auto_test@163.com",
             "mail_pass": "XSJLZTQHSMAKPCMX"}
# 收件人电子邮箱
receive_email = ['api_auto_test@163.com', 'lizhaoyong94@163.com']

备注:以上日志模块请自行去除

标签:python,send,port,发送,import,msg,mail,email,邮件
来源: https://www.cnblogs.com/seeyog/p/15948777.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有