简单了解python 邮件模块的使用方法

yipeiwu_com6年前Python基础

我们在开发程序的时候,有时候需要开发一些自动化的任务,执行完之后,将结果自动的发送一份邮件,python发送邮件使用smtplib模块,是一个标准包,直接import导入使用即可,代码如下:

import smtplib    
from email.mime.text import MIMEText
email_host = 'smtp.163.com'   #邮箱地址
email_user = 'xxxx@163.com' # 发送者账号
email_pwd = 'xxxx'    # 发送者密码
maillist ='511402865@qq.com'
#收件人邮箱,多个账号的话,用逗号隔开
me = email_user
msg = MIMEText('邮件发送测试内容')  # 邮件内容
msg['Subject'] = '邮件测试主题'  # 邮件主题
msg['From'] = me  # 发送者账号
msg['To'] = maillist  # 接收者账号列表
smtp = smtplib.SMTP(email_host,port=25) # 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25
smtp.login(email_user, email_pwd)  # 发送者的邮箱账号,密码
smtp.sendmail(me, maillist, msg.as_string())
# 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
smtp.quit() # 发送完毕后退出smtp
print ('email send success.')

下面是发送带附件的邮件

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
username='xxx@xx.com'
email_host = 'smtp.163.com'
passwd='123456'
recv=['511402865@qq.com',]
title='邮件标题'
content='发送邮件测试'
msg = MIMEMultipart()
file='a.txt'
att = MIMEText(open(file,encoding='utf-8').read())
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="%s"'%file
msg.attach(att)
msg.attach(MIMEText(content))#邮件正文的内容
msg['Subject'] = title # 邮件主题
msg['From'] = username # 发送者账号
msg['To'] = recv # 接收者账号列表
#smtp = smtplib.SMTP_SSL(eail_host,port=456)#qq邮箱
smtp = smtplib.SMTP_SSL(eail_host,port=25)#其他邮箱
smtp.login(username,passwd)
smtp.sendmail(username,recv,msg.as_string())
smtp.quit()

当然,我们可以封装成一个函数,使用的时候,直接调用函数,传入邮箱账号密码,收件人,发件人,标题和内容即可。

  import smtplib      
  from email.mime.text import MIMEText
  def send_mail(username,passwd,recv,title,content,mail_host='smtp.163.com',port=25):
    '''
    发送邮件函数,默认使用163smtp
    :param username: 邮箱账号 xx@163.com
    :param passwd: 邮箱密码
    :param recv: 邮箱接收人地址,多个账号以逗号隔开
    :param title: 邮件标题
    :param content: 邮件内容
    :param mail_host: 邮箱服务器
    :param port: 端口号
    :return:
    '''
    msg = MIMEText(content)  # 邮件内容
    msg['Subject'] = title  # 邮件主题
    msg['From'] = username  # 发送者账号
    msg['To'] = recv  # 接收者账号列表
    smtp = smtplib.SMTP(mail_host,port=port) # 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25
    smtp.login(username, passwd)  # 发送者的邮箱账号,密码
    smtp.sendmail(username, recv, msg.as_string())
    # 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
    smtp.quit() # 发送完毕后退出smtp
    print ('email send success.')
     
email_user = 'xxxx@163.com' # 发送者账号
email_pwd = 'xxxxx'    # 发送者密码
maillist ='511402865@qq.com'
title = '测试邮件标题'
content = '这里是邮件内容'
send_mail(email_user,email_pwd,maillist,title,content)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

PyQt5实现拖放功能

PyQt5实现拖放功能

在这节教程中,我们将探讨PyQt5中的拖放操作。 在计算机图形用户界面(GUI)中,拖放是在某个虚拟对象上点击并拖动到另一个位置或虚拟对象上的操作。它通常用于调用多个动作,或为两个抽象对...

python3实现mysql导出excel的方法

python3实现mysql导出excel的方法

Mysql中'employee'表内容如下: # __Desc__ = 从数据库中导出数据到excel数据表中 import xlwt import pymysql class...

MySQLdb ImportError: libmysqlclient.so.18解决方法

安装MySQLdb后,import MySQLdb出错如下: 复制代码 代码如下: [root@lizhong MySQL-python-1.2.3]# /usr/local/bin/p...

详细讲解Python中的文件I/O操作

详细讲解Python中的文件I/O操作

 本章将覆盖所有在Python中使用的基本I/O功能。有关更多函数,请参考标准Python文档。 打印到屏幕上: 产生输出最简单的方法是使用print语句,可以通过用逗号分隔的...

Django实现跨域请求过程详解

Django实现跨域请求过程详解

前言 CORS 即 Cross Origin Resource Sharing 跨域资源共享. 跨域请求分两种:简单请求、复杂请求. 简单请求 简单请求必须满足下述条件. HTTP方法为...