Python撰写mail
版本1 指定邮箱进行发送
"""
说明:指定账户密码进行邮件发送
由312051952@qq.com-->c4kaichen@163.com
""" import smtplib
from email.mime.text import MIMEText
from email.header import Header
#登录邮箱步骤
smtpConnect=smtplib.SMTP('smtp.qq.com',587)
smtpConnect.ehlo()
smtpConnect.starttls()
smtpConnect.login('312051952@qq.com','邮箱密码')
sender='312051952@qq.com'
#receiver=[input('输入邮箱地址:')]
receiver=['c4kaichen@163.com'] #显示收件人
message=MIMEText('python邮件发送测试3','plain','utf-8')#邮箱内容
message['To'] = Header('c4kaichen@163.com')
subject='python smtp测试3' #邮箱主题
message['Subject']=Header(subject,'utf-8')
smtpConnect.sendmail(sender,receiver,message.as_string())
smtpConnect.quit()
版本2 手动输入对方邮箱进行登录
#发送邮件并手动输入对方邮箱
import smtplib
from email.mime.text import MIMEText
from email.header import Header
#登录邮箱步骤
smtpConnect=smtplib.SMTP('smtp.qq.com',587)
smtpConnect.ehlo()
smtpConnect.starttls()
smtpConnect.login('312051952@qq.com','邮箱密码')
sender='312051952@qq.com'
receiver=input('输入邮箱地址:')
receiverdone=[receiver] #要用列表显示
#receiver=['c4kaichen@163.com'] #显示收件人
message=MIMEText('python邮件发送测试5','plain','utf-8')#邮箱内容
message['To'] = Header(receiver)
subject='python smtp测试4' #邮箱主题
message['Subject']=Header(subject,'utf-8')
smtpConnect.sendmail(sender,receiverdone,message.as_string())
smtpConnect.quit()
最新版本:
分别输入要登录的QQ邮箱密码账号
以及需要发送的对方账号以及邮件内容
#发送邮件并手动输入对方邮箱
import smtplib
from email.mime.text import MIMEText
from email.header import Header
#登录邮箱步骤
smtpConnect=smtplib.SMTP('smtp.qq.com',587)
smtpConnect.ehlo()
smtpConnect.starttls()
user=input('请输入QQ邮箱地址:')
passwd=input('请输入QQ邮箱密码:')
smtpConnect.login(user,passwd)
sender=user
receiver=input('输入对方邮箱地址:')
content=input('请输入要发送的内容:')
receiverdone=[receiver] #要用列表显示
#receiver=['c4kaichen@163.com'] #显示收件人
message=MIMEText(content,'plain','utf-8')#邮箱内容
message['To'] = Header(receiver)
subject='python smtp测试4' #邮箱主题
message['Subject']=Header(subject,'utf-8')
smtpConnect.sendmail(sender,receiverdone,message.as_string())
smtpConnect.quit()
手动输入邮箱及密码之后手动输入对方的邮箱,邮件主题,内容进行邮件的发送工作
# 发送邮件并手动输入对方邮箱
# 注意此处是针对QQ邮箱进行撰写
import smtplib
from email.mime.text import MIMEText
from email.header import Header # 登录邮箱步骤
smtpConnect = smtplib.SMTP('smtp.qq.com', 587)
smtpConnect.ehlo()
smtpConnect.starttls()
user = input('请输入QQ邮箱地址:')
passwd = input('请输入QQ邮箱密码:')
smtpConnect.login(user, passwd)
sender = user
receiver = input('输入对方邮箱地址:')
subject = input('请输入邮件主题:')
content = input('请输入要发送的内容:')
receiverdone = [receiver] # 要用列表显示
# receiver=['c4kaichen@163.com'] #显示收件人
message = MIMEText(content, 'plain', 'utf-8') # 邮箱内容
message['To'] = Header(receiver)
message['Subject'] = Header(subject, 'utf-8')
smtpConnect.sendmail(sender, receiverdone, message.as_string())
smtpConnect.quit()
新版本:每天的菜谱进行选择然后自动发送邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time
def current_time():
time_format='%Y-%d-%m %X'
time_current=time.strftime(time_format)
return time_current
#登录邮箱步骤
def sendmail(x):
smtpConnect=smtplib.SMTP('smtp.qq.com',587)
smtpConnect.ehlo()
smtpConnect.starttls()
smtpConnect.login('312051952@qq.com','邮箱密码')
sender='312051952@qq.com'
receiver=['c4kaichen@163.com'] #显示收件人
message=MIMEText(x,'plain','utf-8')#邮箱内容 此处建议采用plain 如果换成text发送过去的会是附件形式
message['To'] = Header('c4kaichen@163.com')
time_current=current_time()
subject='%s每日菜单'%time_current #邮箱主题:每日菜单
message['Subject']=Header(subject,'utf-8')
smtpConnect.sendmail(sender,receiver,message.as_string())
smtpConnect.quit()
import random
list_menu=['排骨冬瓜','清炒包菜',' 蒜泥西蓝花',' 清炒青菜',
' 筒骨汤',' 香干肉丝',' 丝瓜炒蛋',' 面条',' 沙县',' 品客']
def write_menu(args):
with open('menu_info','w',encoding='utf-8') as f:
f.writelines(args)
def read_menu():
with open('menu_info','r',encoding='utf-8') as f2:
data=f2.read()
return data num=0
item_menu=[]
count=input('请输入今天要吃几个菜:>>').strip()
if count.isdigit():
count=int(count)
while num<count:
item=random.choice(list_menu)
item_menu.append(item)
num+=1
if num==count:
print('\033[31;1m今日选择的菜单如下:\033[0m')
for p_item in item_menu:
print(p_item)
write_menu(item_menu)
x=read_menu()
sendmail(x)
else:
print('请重新输入')
input('press Enter to exit')
#add at 18-11-11
import smtplib
from email.header import Header
from email.mime.text import MIMEText
# 第三方 SMTP 服务
mail_host = "smtp.163.com" # SMTP服务器
mail_user = "c4kaichen" # 用户名
mail_pass = "XXXXX" # 授权密码,非登录密码
sender = 'c4kaichen@163.com' # 发件人邮箱(最好写全, 不然会失败)
receivers = '312051952@qq.com' # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
title = '标题info'#标题
content = '正文info' # 正文
def sendEmail():
message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码
message['From'] = "{}".format(sender)
#message['To'] = ",".join(receivers)
message['To'] = receivers
message['Subject'] = title
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465
smtpObj.login(mail_user, mail_pass) # 登录验证
smtpObj.sendmail(sender, receivers, message.as_string()) # 发送
print("mail has been send successfully.")
except smtplib.SMTPException as e:
print(e) def send_email2(SMTP_host, from_account, from_passwd, to_account, subject, content):
email_client = smtplib.SMTP(SMTP_host)
email_client.login(from_account, from_passwd)
# create msg
msg = MIMEText(content, 'plain', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8') # subject
msg['From'] = from_account
msg['To'] = to_account
email_client.sendmail(from_account, to_account, msg.as_string())
email_client.quit()
if __name__ == '__main__':
sendEmail()
# receiver = '***'
# send_email2(mail_host, mail_user, mail_pass, receiver, title, content)
### add at 18-11-11 通过163邮箱每日IP推送
import socket # 获取本机计算机名称
hostname = socket.gethostname()
#print(hostname)
# 获取本机ip列表
#ip = socket.gethostbyname_ex(hostname)
ip = socket.gethostbyname_ex(hostname)
addr=ip[2] #获取当前时间
import time
current_time=time.ctime() #获取计算机名
pc_name=socket.gethostname()
ip_info=current_time+' '+pc_name+' '+str(addr) import smtplib
from email.header import Header
from email.mime.text import MIMEText
# 第三方 SMTP 服务
mail_host = "smtp.163.com" # SMTP服务器
mail_user = "c4kaichen" # 用户名
mail_pass = "XXXXX" # 授权密码,非登录密码
sender = 'c4kaichen@163.com' # 发件人邮箱(最好写全, 不然会失败)
receivers = '312051952@qq.com' # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
title = '每日ip' # 标题
content = ip_info#正文
def sendEmail():
message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码
message['From'] = "{}".format(sender)
#message['To'] = ",".join(receivers)
message['To'] = receivers
message['Subject'] = title
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465
smtpObj.login(mail_user, mail_pass) # 登录验证
smtpObj.sendmail(sender, receivers, message.as_string()) # 发送
print("mail has been send successfully.")
except smtplib.SMTPException as e:
print(e) def send_email2(SMTP_host, from_account, from_passwd, to_account, subject, content):
email_client = smtplib.SMTP(SMTP_host)
email_client.login(from_account, from_passwd)
# create msg
msg = MIMEText(content, 'plain', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8') # subject
msg['From'] = from_account
msg['To'] = to_account
email_client.sendmail(from_account, to_account, msg.as_string())
email_client.quit()
if __name__ == '__main__':
sendEmail()
# receiver = '***'
# send_email2(mail_host, mail_user, mail_pass, receiver, title, content)
升级版 邮件轰炸
import socket
from threading import Timer # 获取本机计算机名称
hostname = socket.gethostname()
#print(hostname)
# 获取本机ip列表
#ip = socket.gethostbyname_ex(hostname)
ip = socket.gethostbyname_ex(hostname)
addr=ip[2] #获取当前时间
import time
current_time=time.ctime() #获取计算机名
pc_name=socket.gethostname()
ip_info=current_time+' '+pc_name+' '+str(addr) import smtplib
from email.header import Header
from email.mime.text import MIMEText
# 第三方 SMTP 服务
mail_host = "smtp.163.com" # SMTP服务器
mail_user = "c4kaichen" # 用户名
mail_pass = "XXXXX" # 授权密码,非登录密码
sender = 'c4kaichen@163.com' # 发件人邮箱(最好写全, 不然会失败)
receivers = '312051952@qq.com' # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
title = '每日ip' # 标题
content = ip_info#正文
def sendEmail():
message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码
message['From'] = "{}".format(sender)
#message['To'] = ",".join(receivers)
message['To'] = receivers
message['Subject'] = title
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465
smtpObj.login(mail_user, mail_pass) # 登录验证
smtpObj.sendmail(sender, receivers, message.as_string()) # 发送
t=Timer(10,sendEmail)
t.start()
print("mail has been send successfully.")
except smtplib.SMTPException as e:
print(e) def send_email2(SMTP_host, from_account, from_passwd, to_account, subject, content):
email_client = smtplib.SMTP(SMTP_host)
email_client.login(from_account, from_passwd)
# create msg
msg = MIMEText(content, 'plain', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8') # subject
msg['From'] = from_account
msg['To'] = to_account
email_client.sendmail(from_account, to_account, msg.as_string())
email_client.quit()
if __name__ == '__main__':
sendEmail()
# receiver = '***'
# send_email2(mail_host, mail_user, mail_pass, receiver, title, content)
Python撰写mail的更多相关文章
- Python之Mail编程
# Mail编程- 管理程序 - Euroda使邮件普及 - Netscape,outlook,forxmail后来居上 - Hotmail使用浏览器发送邮件 ## 邮件工作流程- MUA邮件用户代理 ...
- Python 目录整理
基础部分: 1 常量 2 字符编码 字符编码补充 3 编码集 4 break与continue 5装饰器 6 迭代器 7生成器 8面向过程 9模块 10包 11 递归调用 12文件的用途 13类 ...
- python之进程与线程
什么是操作系统 可能很多人都会说,我们平时装的windows7 windows10都是操作系统,没错,他们都是操作系统.还有没有其他的? 想想我们使用的手机,Google公司的Androi ...
- Sentry 开发者贡献指南 - 后端服务(Python/Go/Rust/NodeJS)
内容整理自官方开发文档 系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Map ...
- Python用smtplib发送邮件
参照了下面: 1. 先随便照着试试这个: http://blog.csdn.net/zhaoweikid/article/details/1638349 2. 这个写了一个很简洁的代码,看过NO.1就 ...
- 使用python发送简单的邮件
from:http://blog.csdn.net/zhaoweikid/article/details/125898 前些时间,论坛上有人讨论怎么用python发送需要认证的邮件,我在我的FreeB ...
- python实现邮件发送完整代码(带附件发送方式)
实例一:利用SMTP与EMAIL实现邮件发送,带附件(完整代码) __author__ = 'Administrator'#coding=gb2312 from email.Header import ...
- python实现邮件发送
实例补充: #**************************利用STMP自动发送邮件******************************import smtplibsmtp = smtp ...
- python之路: 线程、进程和协程
进程和线程 既然看到这一章,那么你肯定知道现在的系统都是支持“多任务”的操作,比如: Mac OS X,UNIX,Linux,Windows等. 多任务:简单地说就是同时运行多个任务.譬如:你可以一边 ...
随机推荐
- input 标签的 disabled 和 readonly 属性
首先这两种属性都会使显示出来的文本框不能输入. disabled 属性:规定禁用 input 元素.被禁用的 input 元素既不可用,也不可点击和编辑,使用 tab 键时将会被跳过,用户的所有操作对 ...
- 关于window 图片系统功能
直接选择文件悬浮 分辨率 大小 修改时间 文件选择较少的 可以选择反选 找到自己想要的 大小排序 找到最大的压缩 名称排序 同一类的图片 按时间排序找自己刚修改的图片 文档类预览
- ThinkPHP5.0源码学习之框架启动流程
ThinkPHP5框架的启动流程图如下: ThinkPHP5的启动流程按照文件分为三步: 1.请求入口(public/index.php) 2.框架启动(thinkphp/start.php) 3.应 ...
- EOS使用
公司要玩区块链,听说EOS交易快,就弄来玩玩.弄了一天终于编译成功了. Scanning dependencies of target nodeos [%] Building CXX object p ...
- mysql中关于关联索引的问题——对a,b,c三个字段建立联合索引,那么查询时使用其中的2个作为查询条件,是否还会走索引?
情况描述:在MySQL的user表中,对a,b,c三个字段建立联合索引,那么查询时使用其中的2个作为查询条件,是否还会走索引? 根据查询字段的位置不同来决定,如查询a, a,b a,b, ...
- 【问题】用ant编译时,提示编码utf为不可映射字符
分析:eclipse默认的编码为gbk,而ant里的build.xml文件里定义的为utf-8格式.两者格式不统一. 建议:将工程的编码改成utf-8的格式,一般java工程也建议为utf-8格式.
- Spring Boot + Spring Cloud 实现权限管理系统(解决跨域问题)
什么是跨域? 同源策略是浏览器的一个安全功能,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源. 同源策略是浏览器安全的基石. 如果一个请求地址里面的协议.域名和端口号都相同,就属于同源. ...
- day35-python 操作memcache二
Memcache常用命令 存储命令: set/add/replace/append/prepend/cas 获取命令: get/gets 其他命令: delete/stats.. add方法 添加一条 ...
- vue-新建项目-构建-打包-环境切换
一.新建项目 二.运行 npm install npm run start 三.多环境切换 踩坑后总结的方法.. 首先看到package.json 前面的参数都是命令.比如“start”的意思就是np ...
- 超简单的实现wordcount
worcount1.0,源码参见GitHub:https://github.com/18382271904/spring_lee_flag.git