用python函数发邮件 Python如何发邮件
如何通过python发送邮件啊?
一般最好有个smtp服务器,比如说你在163注册个邮箱,这样可以用smtplib通过这个邮箱来发送。以下是示例:
创新互联建站是一家集成都网站设计、成都做网站、网站页面设计、网站优化SEO优化为一体的专业网站制作公司,已为成都等多地近百家企业提供网站建设服务。追求良好的浏览体验,以探求精品塑造与理念升华,设计最适合用户的网站页面。 合作只是第一步,服务才是根本,我们始终坚持讲诚信,负责任的原则,为您进行细心、贴心、认真的服务,与众多客户在蓬勃发展的市场环境中,互促共生。
#-*- coding:utf8 -*-
import smtplib
import email
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.mime.text import MIMEText
mail_host="smtp.163.com"
mail_user="yourusername"
mail_pass="yourpassword"
mail_postfix="mail.163.com"
def sendmail(to_list,sub,con):
"""发送邮件
"""
# translation
me = mail_user+""+mail_user+"@"+mail_postfix+""
msg = MIMEMultipart('related')
msg['Subject'] = email.Header.Header(sub,'utf-8')
msg['From'] = me
msg['To'] = ";".join(to_list)
msg.preamble = 'This is a multi-part message in MIME format.'
msgAlternative = MIMEMultipart('alternative')
msgText = MIMEText(con, 'plain', 'utf-8')
msgAlternative.attach(msgText)
msg.attach(msgAlternative)
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.quit()
except Exception,e:
return False
return True
if __name__ == '__main__':
if sendmail(['test@test.com'],"测试","测试"):
print "Success!"
else:
print "Fail!"
如果要不经过邮件系统直接发,通常会被当作垃圾邮件扔了,所以还是这样吧。
如何用python发送html格式的邮件
'''
Created on 2014-12-4
@author: Dana、Li
'''
#encoding utf-8
import smtplib
from email.mime.text import MIMEText
mailto_list=["1669852599@qq.com"]
mail_host="smtp.qq.com" #设置服务器
mail_user="1669852599@qq.com" #用户名
mail_pass="xxxxxxx" #口令
mail_postfix="qq.com" #发件箱的后缀
def send_mail(to_list,sub,content): #to_list:收件人;sub:主题;content:邮件内容
me="hello"+""+mail_user+"@"+mail_postfix+"" #这里的hello可以任意设置,收到信后,将按照设置显示
msg = MIMEText(content,_subtype='html',_charset='gb2312') #创建一个实例,这里设置为html格式邮件
msg['Subject'] = sub #设置主题
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
s = smtplib.SMTP()
s.connect(mail_host) #连接smtp服务器
s.login(mail_user,mail_pass) #登陆服务器
s.sendmail(me, to_list, msg.as_string()) #发送邮件
s.close()
return True
except (Exception):
print("失败咯...")
return False
if __name__ == '__main__':
if send_mail(mailto_list,"hello","a href=''Dana、Li/a"):
print ("发送成功")
else:
print ("发送失败")
python如何发送紧急级别的邮件?
引用头文件
#!/usr/bin/env python
#-*-coding:utf-8-*-
import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
创建“连接邮件服务器”函数
def smtp_connect( smtp_server, account, password, target ):
global smtp
global e_smtpserver
global e_account
global e_password
global e_target
global msg
e_smtpserver = smtp_server
e_account = account
e_password = password
e_target = target
try:
smtp = smtplib.SMTP()
smtp.connect(e_smtpserver)
smtp.login(e_account, e_password)
except Exception, e:
print 'Mail connect failed: %s' % e
exit( -1 )
msg = MIMEMultipart('related')
创建“写邮件内容”函数
def smtp_set_content( subject, main_body ):
global msg
msg['Subject'] = subject
body = MIMEText( main_body, 'plain', 'utf-8' )
msg.attach( body )
创建“添加附件”函数
def smtp_set_attachment( file_path ):
global msg
part = MIMEBase( 'application', 'octet-stream' )
part.set_payload( open(file_path,'rb').read() )
encoders.encode_base64(part)
part.add_header( 'Content-Disposition', 'attachment; filename=%s' % file_path.split('/')[-1] )
msg.attach(part)
创建“发送邮件”函数
def smtp_send():
global smtp
global e_smtpserver
global e_account
global e_password
global e_target
global msg
try:
smtp.sendmail( e_account, e_target, msg.as_string() )
except Exception, e:
print 'Mail send Failed: %s' % e
exit( -1 )
使用示例
if __name__ == '__main__':
smtp_connect( 'smtp.xxxx.com.cn', 'xxxx@xxxx.com.cn', 'password',
'xxxx@xxxx.com.cn' )
smtp_set_content( 'Test subject', 'Test_content' )
smtp_set_attachment( os.getcwd() + '/mail.py' )
smtp_send()
如何用python自动发送邮件
注意如果使用qq的smtp转发功能的话,需要在设置页面中将该功能打开,然后设定密码,改密码就是赋值给下文password中的。
开启smtp转发功能文档
[python] view plain copy
#from email.MIMEText import MIMEText #old version cannot recongnize by new python
from email.MIMEText import MIMEText
from email.Header import Header
msg = MIMEText('hello, send by python','plain','utf-8');
from_addr = "*****@qq.com" #email addresss include domain name
password = "******" #password
to_addr = "****@qq.com" # target email address
smtp_server = "smtp.qq.com" #smtp sever domain for qq is smtp.qq.com
import smtplib
server = smtplib.SMTP(smtp_server,25);
server.set_debuglevel(1)
server.login(from_addr,password);
server.sendmail(from_addr,[to_addr], msg.as_string())
server.quit()
上面这个是没有主题的,有主题的话建议采用下面这个代码
下面是连续发送有主题的文件10封
[python] view plain copy
from email.Header import Header
from email.MIMEText import MIMEText
from email import encoders
from email.utils import parseaddr, formataddr
import smtplib
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
from_addr = "*****@qq.com" #email addresss include domain name
password = "******" #password
to_addr = "****@qq.com" # target email address
smtp_server = "smtp.qq.com" #smtp sever domain for qq is smtp.qq.com
for num in range(1,11):
msg = MIMEText('hello, send by Tom', 'plain', 'utf-8');
msg['From'] = _format_addr('Tom %s' % from_addr)
msg['To'] = _format_addr('addministrator %s' % to_addr)
msg['Subject'] = Header('The greeting from Tom','utf-8').encode()
server = smtplib.SMTP(smtp_server,25)
server.set_debuglevel(1)
server.login(from_addr, password)
#server.sendmail(from_addr, [to_addr], msg.as_string())
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
用Python发送邮件,可以群发,带有多个附件
'''''
函数说明:Send_email_text() 函数实现发送带有附件的邮件,可以群发,附件格式包括:xlsx,pdf,txt,jpg,mp3等
参数说明:
1. subject:邮件主题
2. content:邮件正文
3. filepath:附件的地址, 输入格式为["","",...]
4. receive_email:收件人地址, 输入格式为["","",...]
'''
def Send_email_text(subject,content,filepath,receive_email):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
sender = "发送方邮箱"
passwd = "填入发送方密码"
receivers = receive_email #收件人邮箱
msgRoot = MIMEMultipart()
msgRoot['Subject'] = subject
msgRoot['From'] = sender
if len(receivers)1:
msgRoot['To'] = ','.join(receivers) #群发邮件
else:
msgRoot['To'] = receivers[0]
part = MIMEText(content)
msgRoot.attach(part)
##添加附件部分
for path in filepath:
if ".jpg" in path:
#jpg类型附件
jpg_name = path.split("\\")[-1]
part = MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition', 'attachment', filename=jpg_name)
msgRoot.attach(part)
if ".pdf" in path:
#pdf类型附件
pdf_name = path.split("\\")[-1]
part = MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition', 'attachment', filename=pdf_name)
msgRoot.attach(part)
if ".xlsx" in path:
#xlsx类型附件
xlsx_name = path.split("\\")[-1]
part = MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition', 'attachment', filename=xlsx_name)
msgRoot.attach(part)
if ".txt" in path:
#txt类型附件
txt_name = path.split("\\")[-1]
part = MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition', 'attachment', filename=txt_name)
msgRoot.attach(part)
if ".mp3" in path:
#mp3类型附件
mp3_name = path.split("\\")[-1]
part = MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition', 'attachment', filename=mp3_name)
msgRoot.attach(part)
try:
s = smtplib.SMTP()
s.connect("smtp.mail.aliyun.com") #这里我使用的是阿里云邮箱,也可以使用163邮箱:smtp.163.com
s.login(sender, passwd)
s.sendmail(sender, receivers, msgRoot.as_string())
print ("邮件发送成功")
except smtplib.SMTPException as e:
print("Error, 发送失败")
finally:
s.quit()
如何使用Python控制摄像头拍照并发邮件?
这个实现起来非常容易,主要分为2步,首先控制摄像头拍照,然后将拍到的照片作为邮件附件发送就行,下面我简单介绍一下实现过程,实验环境win7+python3.6+pycharm5.0,主要内容如下:
控制摄像头拍照
这里我们直接使用opencv的python接口包就行,借助于opencv强大的功能,我们只需要很少量的代码就可以实现摄像头拍照的功能,具体步骤如下:
1.首先,安装opencv-python模块,这个直接在cmd窗口输入命令“pip install opencv-python”就行,如下,很快就能安装成功:
2.安装完成后,我们就可以编写代码来实现控制摄像头拍照的功能了,测试代码如下,非常简单,打开摄像头,按帧播放,当按下q键时保存图片并退出,当前目录下就会出现拍到的照片,后面就是将这个照片作为邮件附件进行发送:
发送邮件
这里以发送163邮件为例,主要用到Python自带的smtplib模块,专门用于发送邮件,具体步骤如下:
1.首先,需要开启开启邮箱POP3/SMTP服务,这个直接登录邮箱,在上面菜单栏点击“设置”,勾选POP3/SMPT服务就行,如下:
接着点击左边管理列表的“客户端授权密码”,设置授权密码,后面的python代码中需要借助这个密码才能登陆邮箱服务器发送邮件:
2.一切设置完成后,我们就可以直接编写代码来发送带有照片的邮件了,测试代码如下,非常简单,只需要在邮件中attach一下照片就行,函数输入参数分别为发送人邮箱、密码、接收人邮件列表、主题、内容和附件名称:
运行这个程序,邮件就可以正常发送,并且可以看到发送的照片附件,如下:
至此,我们就完成了利用python来控制摄像头拍照并发送邮件。总的来说,整个过程不难,思路也比较清晰,只要你有一定的python基础,熟悉一下上面的代码,多调试几遍程序,很快就能掌握的,网上也有相关教程和资料,非常丰富详细,感兴趣的话,可以搜一下,希望以上分享的内容能对你有所帮助吧,也欢迎大家评论、留言。
网页标题:用python函数发邮件 Python如何发邮件
文章位置:http://scjbc.cn/article/hhdsgd.html