python之发送邮件~

在之前的工作中,测试web界面产生的报告是自动使用python中发送邮件模块实现,在全部自动化测试完成之后,把报告自动发送给相关人员

其实在python中很好实现,一个是smtplib和mail俩个模块来实现,主要如下:

import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEBaseimport os

sender = ‘[email protected]‘receives = ‘[email protected]‘cc_receiver = ‘[email protected]‘subject = ‘Python auto test‘

msg = MIMEMultipart()msg[‘From‘] = sendermsg[‘To‘] = receivesmsg[‘Cc‘] = cc_receivermsg[‘Subject‘] = subjectmsg.attach(MIMEText(‘Dont reply‘,‘plain‘,‘utf-8‘))

os.chdir(‘H:\\‘)with open(‘auto_report03.html‘,‘r‘,encoding = ‘utf-8‘) as fp:    att = MIMEBase(‘html‘,‘html‘)    att.add_header(‘Content-Disposion‘,‘attachment‘,filename = ‘auto_report03.html‘)    att.set_payload(fp.read())    msg.attach(att)

sendmail = smtplib.SMTP()sendmail.connect(‘smtp.126.com‘,25)sendmail.login(‘lounq10000‘,‘[email protected]‘)sendmail.sendmail(sender,receives,msg.as_string())sendmail.quit()

在这里我们可以把发送mail的代码进行封装成一个函数供外部调用,如下:


import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEBaseimport os

def sendMail(subject,textContent,sendFileName):    sender = ‘[email protected]‘    receives = ‘[email protected]‘    cc_receiver = ‘[email protected]‘    subject = subject

msg = MIMEMultipart()    msg[‘From‘] = sender    msg[‘To‘] = receives    msg[‘Cc‘] = cc_receiver    msg[‘Subject‘] = subject    msg.attach(MIMEText(textContent,‘plain‘,‘utf-8‘))

os.chdir(‘H:\\‘)    with open(sendFileName,‘r‘,encoding = ‘utf-8‘) as fp:        att = MIMEBase(‘html‘,‘html‘)        att.add_header(‘Content-Disposion‘,‘attachment‘,filename = sendFileName)        att.set_payload(fp.read())        msg.attach(att)

sendmail = smtplib.SMTP()    sendmail.connect(‘smtp.126.com‘,25)    sendmail.login(‘lounq10000‘,‘[email protected]‘)    sendmail.sendmail(sender,receives,msg.as_string())    sendmail.quit()

这里把主题、正文和附件文件作为参数代入,函数中的其他变量也可以作为参数输入,个人觉得没什么必要,但可以把其他的一些变量放到文件来读取,这样方便管理

原文地址:https://www.cnblogs.com/watertaro/p/9278906.html

时间: 2024-08-06 05:22:36

python之发送邮件~的相关文章

python模范发送邮件的时候,才smtp.connect的时候总是抛出错误

python发送邮件的时候,总是出现:[Errno 10060] 错误码 根据debug得到在connect的时候出错. 认真检查了下host,没有错呀~应该就是服务器的host. 查看了下网上的一些例子,发现qq的host举例:smtp.qq.com/smtp.163.com/smtp.162.com 于是觉得应该用smtp.+XXX.com来试一下~就可以解决了~ python模范发送邮件的时候,才smtp.connect的时候总是抛出错误,布布扣,bubuko.com

python实现发送邮件功能

前一段时间实现了一个python脚本发送邮件的功能,该脚本是借用smtp服务器发送邮件,邮件以附件的形式发出,如果要添加正文,修改添加即可. #!/usr/bin/env python #coding: utf-8 import smtplib, re, sys, os import xlwt from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.im

Appium+python 自动发送邮件(1)

SMTP:简单传输协议,实在Internet上传输Email的事实标准. Python的smtplib模块提供了一种很方便的途径来发送电子邮件,它对SMTP协议进行了简单的封装. python中发送邮件除了SMTP模块外,还需用到email模块.email模块主要用来定义邮件的标题.正文.附件. 一.SMTP的方法 1.SMTP模块的方法 connect(host,port) host:指定连接的邮箱服务器 port:指定连接服务器的端口号 login(user,passwork) user:登

Python——SMTP发送邮件

一.定义 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式.python的smtplib提供了一种很方便的途径发送电子邮件.它对smtp协议进行了简单的封装. 二.Python创建STMP对象 import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] ) #host: SMTP 服务器主机. 你可

使用python语言发送邮件smtp

使用python语言发送邮件smtp,详细见下面脚本 #!/usr/bin/python # -*- coding: utf-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header # 第三方SMTP服务 mail_host = 'smtp.126.com' mail_user = '[email protected]' mail_pass = '12345678' sen

python+selenium 发送邮件

import time from selenium import webdriver from selenium.webdriver import ChromeOptions from selenium.webdriver.common.keys import Keys from getpass import getpass def run(): driver.get(url='https://mail.qq.com/') # 遇到iframe,需要切换 iframe_list = driver

python实现发送邮件

SMTP是发送邮件的协议.python对SMTP的支持有两个模块,smtplib模块和email模块.email负责构造邮件,smtplib负责发送邮件. 一个例子简单的实现发送邮件的功能 #!/usr/bin/env python # coding=utf-8 import smtplib from email.mime.text import MIMEText mail_to_list=["[email protected]"] mail_host="smtp.126.c

python笔记- 发送邮件

依赖: Python代码实现发送邮件,使用的模块是smtplib.MIMEText,实现代码之前需要导入包: import smtplib from email.mime.text import MIMEText 使用163邮件发送邮件,具体代码实现如下: import smtplib from email.mime.text import MIMEText ''' 发送邮件函数,默认使用163smtp :param mail_host: 邮箱服务器,16邮箱host: smtp.163.com

python smtplib发送邮件遇到的认证问题

python的smtplib模块主要是用来发送邮件的,使用起来比较方便. 使用程序发送邮件只需要写以下几行代码就OK了: #!/usr/bin/env python import smtplib s = smtplib.SMTP(mail server, port) s.login(username, passwd) s.sendmail(fromaddr, toaddrs, msg) 不过使用这种方法不一定总是可行,昨天用这种方式发送邮件的时候程序总是会抛异常: File "/usr/lib6