I need to be able to send an email from my python script, and I
wanted to be able to use my GMail for the outgoing SMTP server. It
becomes a little tricky because the GMail servers require
authentication. I searched around and found some good examples on the
Internet and then fine tuned them a bit.
import os
import smtplib
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEAudio import MIMEAudio
from email.MIMEImage import MIMEImage
from email.Encoders import encode_base64
def sendMail(subject, text, *attachmentFilePaths):
gmailUser = 'yo.mama@gmail.com'
gmailPassword = 'bogus!'
recipient = 'test@test.com'
msg = MIMEMultipart()
msg['From'] = gmailUser
msg['To'] = recipient
msg['Subject'] = subject
msg.attach(MIMEText(text))
for attachmentFilePath in attachmentFilePaths:
msg.attach(getAttachment(attachmentFilePath))
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
print('Sent email to %s' % recipient)
def getAttachment(attachmentFilePath):
contentType, encoding = mimetypes.guess_type(attachmentFilePath)
if contentType is None or encoding is not None:
contentType = 'application/octet-stream'
mainType, subType = contentType.split('/', 1)
file = open(attachmentFilePath, 'rb')
if mainType == 'text':
attachment = MIMEText(file.read())
elif mainType == 'message':
attachment = email.message_from_file(file)
elif mainType == 'image':
attachment = MIMEImage(file.read(),_subType=subType)
elif mainType == 'audio':
attachment = MIMEAudio(file.read(),_subType=subType)
else:
attachment = MIMEBase(mainType, subType)
attachment.set_payload(file.read())
encode_base64(attachment)
file.close()
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(attachmentFilePath))
return attachment
Derived from: http://kutuma.blogspot.com/2007/08/sending-emails-via-gmail-with-python.html and http://mail.python.org/pipermail/python-list/2003-September/225540.html
分享到:
相关推荐
比如Gmail邮箱里有垃圾邮件分类器,一开始的时候可能什么都不过滤,在日常使用过程中,我人工对于每一封邮件点选“垃圾”或“不是垃圾”,过一段时间,Gmail就体现出一定的智能,能够自动过滤掉一些垃圾邮件了。...
复制代码 代码如下:# -*- coding: utf-8 -*-# Author: ToughGuy# Email: wj0630@gmail.com# 写这玩意儿是为了初步了解下python的多线程机制# 平时没写注释的习惯, 这次花时间在代码里面写上注释也是希望有问题的地方...
本文实例讲述了Python实现的简单模板引擎功能。分享给大家供大家参考,具体如下: #coding:utf- 8 __author__=sdm __author_email='sdmzhu3@gmail.com' __date__ =$2009-8-25 21:04:13$ '' ' pytpl 类似 php的模板...
脚本和示例项目是论文“集成 Python 和 HEC-RAS 实现河流流量和运输模拟自动化”的补充。 该论文由 Tomasz Dysarz 编写。 软件名称:HEC-RAS 和 Python 开发人员:Tomasz Dysarz 邮件地址:波兹南生命科学大学水利与...
用git来管理工程的开发,git log是非常有用的‘历史’资料,需求就是...复制代码 代码如下:# -*- coding: utf-8 -*-# created by vince67 Feb.2014# nuovince@gmail.com import reimport osimport subprocess def ru
主要给大家介绍了关于利用Python yagmail三行代码实现发送邮件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
代码的示例输入是 {"收件人":[" ", " "],"subject":"聚会提醒","location":"Koramangala 5th Block, Bangalore","description":"Hangout ","meetingStartDate":"20150512T083000Z","meetingEndDate":"20150512T...
这是示例代码,用于支持Qxf2服务关于Selenium和Python的页面对象模型的教程。 网址: : 设置一种。 安装Python 2.x b。 安装Seleniumc。 将两者都添加到PATH环境变量d中。 如果您还没有,请获得点子e。 'pip ...
合理的警告:此代码很多不雅致。 不要将其作为正确使用Pandas / Matplotlib的示例进行研究。快速开始确保已安装ipython , pip和virtualenv 。 git clone ...
让我们通过 #Python 使用 Gmail API 我尝试使用GmailApi,所以备忘录这里是执行示例代码的过程。最后,当您执行sample.py时,您可以在邮箱中看到类似电子邮件列表的内容。 ##必要的东西 Python2.7 google-api-...
这篇文章主要介绍了Python 如何提高元组的可读性,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 假设学生系统中数据为固定格式:(名字,年龄,性别,邮箱) ('...
Economy-bot-discord.py 加入我们的不和谐服务器: : 成为CODING PALACE的编码员之一
欢迎使用Envmanager for Python Docs Envmanager是通过以最有效的方式提供键入模式来验证和解析环境变量的可靠工具。 :check_mark_button: 快速验证和解析(或强制转换)您的环境变量 :check_mark_button: 将...
Python代码。 从带有学生笔记的excel电子表格自动通过电子邮件(gmail)发送个性化的报告给每个学生的负责人。 电子邮件都同时触发。 这是电子邮件的示例: 在这个项目中,我创建了两个数据库(一个源SQL Server...
模拟神经算法matlab代码计算物理示例代码 关键词: 蒙特卡罗方法 复杂网络 约束优化 王兰道采样 模拟退火 编程语言 C C++ Python MATLAB 这个存储库包含一些示例代码,用于我在计算研究方面的研究。 所有代码都是...
这是一个个人代码段,用于提醒人们按时发送电子邮件来定期做一些事情,例如做家务。 配置 首先创建一个.env文件,以保存发送电子邮件地址/密码和收件人的电子邮件地址的凭据。 (需要python-dotenv ) .env文件的一...
send_daily_report.py此python代码...使用python3运行它gmail帐户要求安全性较低的应用程序访问权限才能运行此示例( )您的datadog帐户中的$ API_KEY,$ APP_KEY sender_email,来自您的GMail帐户的密码。电子邮件样本
让代码= auth.generate(4); assert_eq!(true,auth.verify(code,0,100)); } TOTP示例使用std :: time :: {SystemTime,UNIX_EPOCH}; 使用otpauth :: TOTP; fn main(){let auth = TOTP :: new(“ python...
到目前为止,大多数都是MATLAB代码,但是python 3.5版本将在不久的将来可用... 主要文件和说明: 教程:脚本以及有关如何运行代码的示例 Pcalc_2Group:使用ANOVA计算2组之间均值差的功效计算 Pcalc_Continuous:...
该存储库包含我的博客文章“的示例代码。 该应用程序提供了两个使用Celery进行后台任务的示例: 示例1异步发送电子邮件。 示例2启动一个或多个异步作业,并在网页中显示进度更新。 这是此应用程序的屏幕截图: ...