python邮件正文
1. 用python发送邮件,可以群发,带有多个附件
'''''
函数说明:Send_email_text()函数实现发送带有附件的邮件,可以群发,附件格式包括:xlsx,pdf,txt,jpg,mp3等
参数说明:
1.subject:邮件主题
2.content:邮件正文
3.filepath:附件的地址,输入格式为["","",...]
4.receive_email:收件人地址,输入格式为["","",...]
'''
defSend_email_text(subject,content,filepath,receive_email):
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.
sender="发送方邮箱"
passwd="填入发送方密码"
receivers=receive_email#收件人邮箱
msgRoot=MIMEMultipart()
msgRoot['Subject']=subject
msgRoot['From']=sender
iflen(receivers)>1:
msgRoot['To']=','.join(receivers)#群发邮件
else:
msgRoot['To']=receivers[0]
part=MIMEText(content)
msgRoot.attach(part)
##添加附件部分
forpathinfilepath:
if".jpg"inpath:
#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"inpath:
#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"inpath:
#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"inpath:
#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"inpath:
#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("邮件发送成功")
exceptsmtplib.SMTPExceptionase:
print("Error,发送失败")
finally:
s.quit()
2. python,使用smtp发送邮件,求实例
from smtplib import SMTP
from RuckusAutoTest.models import TestCase
from email.MIMEMultipart import MIMEMultipart
from email.mime.application import MIMEApplication
def sendFildByMail(config):
print 'Preparing...',
message = MIMEMultipart( )
message['from'] = config['from']
message['to'] = config['to']
message['Reply-To'] = config['from']
message['Subject'] = config['subject']
message['Date'] = time.ctime(time.time())
message['X-Priority'] = '3'
message['X-MSMail-Priority'] = 'Normal'
message['X-Mailer'] = 'Microsoft Outlook Express 6.00.2900.2180'
message['X-MimeOLE'] = 'Proced By Microsoft MimeOLE V6.00.2900.2180'
f=open(config['file'], 'rb')
file = MIMEApplication(f.read())
f.close()
file.add_header('Content-Disposition', 'attachment', filename= os.path.basename(config['file']))
message.attach(file)
print 'OK'
print 'Logging...',
smtp = SMTP(config['server'])#, config['port'])
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(config['username'], config['password'])
print 'OK'
print 'Sending...',
smtp.sendmail (config['from'], [config['from'], config['to']], message.as_string())
print 'OK'
smtp.close()
time.sleep(1)
sendFildByMail({
'from': '[email protected]',
'to': '[email protected]',
'subject': ''This is an email test!',
'server': '123.125.50.132',
'username': 'username',
'password': 'password'})
可以注册一个163邮箱试一下,是可以发邮件的。
在sendFildByMail这个函数里填上正确的参数,from是从哪个邮箱发送,也就是刚注册的163邮箱,to是发送到哪个邮箱,可以填另一个邮箱来检查是否能接收邮件,server不要改,这是163的地址;username和password那里填写163邮箱的用户名和密码。
希望能帮到你,有疑问请追问!
3. demo鍦╬ython涓镄勭敤娉
demo鏄鎸囨紨绀虹▼搴忥纴阃氩父鐢ㄦ潵婕旂ず涓绉嶆妧链鎴栧伐鍏风殑浣跨敤鏂规硶銆傚湪Python涓锛宒emo涔熸槸涓涓闱炲父閲嶈佺殑姒傚康銆备笅闱㈡垜灏嗕负澶у朵粙缁峆ython涓璬emo镄勭敤娉曘
3銆乨emo绋嫔簭镄勫垎鏋
浠ヤ笂绋嫔簭浣滀负Python涓婕旂ず闾浠跺彂阃佸姛鑳界殑涓涓猟emo绋嫔簭锛屾兜鐩栦简闾浠跺彂阃佺殑鍏ㄨ繃绋嬶纴鍏朵腑锛岄氲繃email妯″潡鍒涘缓浜嗛偖浠跺疄渚嬶纴骞堕氲繃smtplib妯″潡鎻愪緵镄凷MTP鍗忚杩涜屼简闾浠剁殑鍙戦併
4銆乨emo绋嫔簭镄勭敤阃
杩欎釜demo绋嫔簭鍙浠ュ府锷╂柊镓嬩简瑙Python涓镄勯偖浠跺彂阃佸姛鑳斤纴镟村ソ鍦扮悊瑙e拰瀛︿範Python涓镄勯偖浠舵ā鍧椼傚傛灉𨱍宠佽嚜宸变娇鐢≒ython𨱒ュ彂阃侀偖浠讹纴鍙浠ラ氲繃淇鏀硅demo涓镄勫彂浠朵汉銆佹敹浠朵汉銆佷富棰樸佹f枃銆丼MTP链嶅姟鍣ㄥ湴鍧銆佺鍙d互鍙婅处鎴蜂俊鎭绛夊弬鏁帮纴𨱒ュ疄鐜拌嚜宸辩殑闾浠跺彂阃佸姛鑳姐
4. python 代码中 ret=True 代表什么意思
这里的ret是用于判断邮件发送成功与否的
先赋值ret为True,如果发送邮件过程中没有遇到任何的故障,则try里面的代码执行完成后,mail()函数则会返回ret的值,此时ret的值即为True;
若发送过程出现差错,则会跳到except的语句中,此时先前被赋予True值的ret就会被赋上新的布尔值False,因此main()函数此时会返回False的结果。
接下来就是if ... else ...的语句对ret的值做判断了,如果ret为True,则打印“邮件发送成功”;否则打印“邮件发送失败”
5. python,如何发送带附件的邮件
可以通过以下代码实现:
importsmtplib
fromemail.mime.textimportMIMEText
importemail.mime.multipart
fromemail.
fromemail.MIMEBaseimportMIMEBase
fromemailimportEncoders
defsend_mail():
mailto_list=['xxxx','xxx']#收件人
mail_host="xxxxx"#设置服务器
mail_user="xxxx"#用户名
mail_pass="xxxxxx"#口令
mail_postfix="xxxx.xxx"#发件箱的后缀
me="hello"+"<"+mail_user+"@"+mail_postfix+">"#这里的hello可以任意设置,收到信后,将按照设置显示
content='Thisistestmail!'#邮件正文
msg=MIMEMultipart()
body=MIMEText(content,_subtype='html',_charset='gb2312')#创建一个实例,这里设置为html格式邮件
msg.attach(body)
msg['Subject']="SubjectTest"#设置主题
msg['From']=me
msg['To']=";".join(mailto_list)
#附件内容,若有多个附件,就添加多个part,如part1,part2,part3
part=MIMEBase('application','octet-stream')
#读入文件内容并格式化,此处文件为当前目录下,也可指定目录例如:open(r'/tmp/123.txt','rb')
part.set_payload(open('test.txt','rb').read())
Encoders.encode_base64(part)
##设置附件头
part.add_header('Content-Disposition','attachment;filename="test.txt"')
msg.attach(part)
try:
s=smtplib.SMTP()
s.connect(mail_host)#连接smtp服务器
s.login(mail_user,mail_pass)#登陆服务器
s.sendmail(me,mailto_list,msg.as_string())#发送邮件
s.close()
print'sendmailsucess'
returnTrue
exceptException,e:
printstr(e)
returnFalse
6. python中如何在邮件正文嵌入变量
html ='''你要发送的内容'''+var1+'''其他部分的内容'''+var2+'''剩下的内容'''
msg.attach(MIMEText(html, 'html'))