鱼C论坛

 找回密码
 立即注册
查看: 3492|回复: 11

[技术交流] 邮件发送实现

[复制链接]
发表于 2015-6-21 00:52:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ~风介~ 于 2015-11-4 13:29 编辑

原文出处:找不到了


①邮件发送
  1. #!/usr/bin/env python3  
  2. #coding: utf-8  
  3. import smtplib  
  4. from email.mime.text import MIMEText  
  5. from email.header import Header  
  6.   
  7. sender = 'abc@163.com'  #发送邮箱名
  8. receiver = 'def@qq.com'  #接收邮箱名
  9. subject = 'python email test !'  #主题
  10. smtpserver = 'smtp.163.com'  #邮件服务器
  11. username = 'abc'  #昵称
  12. password = '123456'  #密码(发送邮箱密码)
  13.   
  14. msg = MIMEText('Hello Niko !','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要  
  15. msg['Subject'] = Header(subject, 'utf-8')  
  16.   
  17. smtp = smtplib.SMTP()  
  18. smtp.connect('smtp.163.com')  
  19. smtp.login(username, password)  
  20. smtp.sendmail(sender, receiver, msg.as_string())  
  21. smtp.quit()  
复制代码

②带附件邮件发送
  1. #!/usr/bin/env python3  
  2. #coding: utf-8  
  3. import smtplib  
  4. from email.mime.multipart import MIMEMultipart  
  5. from email.mime.text import MIMEText  
  6. from email.mime.image import MIMEImage  
  7.   
  8. sender = 'abc@163.com'  #发送邮箱名
  9. receiver = 'def@qq.com'  #接收邮箱名
  10. subject = 'python email test !'  #主题
  11. smtpserver = 'smtp.163.com'  #邮件服务器
  12. username = 'abc'  #昵称
  13. password = '123456'  #密码(发送邮箱密码)
  14.   
  15. # Create message container - the correct MIME type is multipart/alternative.  
  16. msg = MIMEMultipart('alternative')  
  17. msg['Subject'] = "Link"  
  18.   
  19. # Create the body of the message (a plain-text and an HTML version).  
  20. text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"  
  21. html = """\
  22. <html>
  23.   <head></head>
  24.   <body>
  25.     <p>Hi!<br>
  26.        How are you?<br>
  27.        Here is the <a href="http://www.python.org">link</a> you wanted.
  28.     </p>
  29.   </body>
  30. </html>
  31. """  
  32.   
  33. # Record the MIME types of both parts - text/plain and text/html.  
  34. part1 = MIMEText(text, 'plain')  
  35. part2 = MIMEText(html, 'html')  
  36.   
  37. # Attach parts into message container.  
  38. # According to RFC 2046, the last part of a multipart message, in this case  
  39. # the HTML message, is best and preferred.  
  40. msg.attach(part1)  
  41. msg.attach(part2)  
  42. #构造附件  
  43. att = MIMEText(open('D:\\lover.jpg', 'rb').read(), 'base64', 'utf-8')  
  44. att["Content-Type"] = 'application/octet-stream'  
  45. att["Content-Disposition"] = 'attachment; filename="1.jpg"'  
  46. msg.attach(att)  
  47.      
  48. smtp = smtplib.SMTP()  
  49. smtp.connect('smtp.163.com')  
  50. smtp.login(username, password)  
  51. smtp.sendmail(sender, receiver, msg.as_string())  
  52. smtp.quit()  
复制代码

③html格式邮件发送
  1. #!/usr/bin/env python3  
  2. #coding: utf-8  
  3. import smtplib  
  4. from email.mime.text import MIMEText  
  5.   
  6. sender = 'abc@163.com'  #发送邮箱名
  7. receiver = 'def@qq.com'  #接收邮箱名
  8. subject = 'python email test !'  #主题
  9. smtpserver = 'smtp.163.com'  #邮件服务器
  10. username = 'abc'  #昵称
  11. password = '123456'  #密码(发送邮箱密码)
  12.   
  13. msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8')  
  14.   
  15. msg['Subject'] = subject  
  16.   
  17. smtp = smtplib.SMTP()  
  18. smtp.connect('smtp.163.com')  
  19. smtp.login(username, password)  
  20. smtp.sendmail(sender, receiver, msg.as_string())  
  21. smtp.quit()
复制代码

④html格式带附件邮件发送
  1. #!/usr/bin/env python3  
  2. #coding: utf-8  
  3. import smtplib  
  4. from email.mime.multipart import MIMEMultipart  
  5. from email.mime.text import MIMEText  
  6. from email.mime.image import MIMEImage  
  7.   
  8. sender = 'abc@163.com'  #发送邮箱名
  9. receiver = 'def@qq.com'  #接收邮箱名
  10. subject = 'python email test !'  #主题
  11. smtpserver = 'smtp.163.com'  #邮件服务器
  12. username = 'abc'  #昵称
  13. password = '123456'  #密码(发送邮箱密码)
  14.   
  15. msgRoot = MIMEMultipart('related')  
  16. msgRoot['Subject'] = 'test message'  
  17.   
  18. #构造附件  
  19. att = MIMEText(open('D:\\lover.jpg', 'rb').read(), 'base64', 'utf-8')  
  20. att["Content-Type"] = 'application/octet-stream'  
  21. att["Content-Disposition"] = 'attachment; filename="1.jpg"'  
  22. msgRoot.attach(att)  
  23.          
  24. smtp = smtplib.SMTP()  
  25. smtp.connect('smtp.163.com')  
  26. smtp.login(username, password)  
  27. smtp.sendmail(sender, receiver, msgRoot.as_string())  
  28. smtp.quit()  
复制代码
⑥html格式带附件V图片邮件发送
  1. #!/usr/bin/env python3  
  2. #coding: utf-8  
  3. import smtplib  
  4. from email.mime.multipart import MIMEMultipart  
  5. from email.mime.text import MIMEText  
  6. from email.mime.image import MIMEImage  
  7.   
  8. sender = 'abc@163.com'  #发送邮箱名
  9. receiver = 'def@qq.com'  #接收邮箱名
  10. subject = 'python email test !'  #主题
  11. smtpserver = 'smtp.163.com'  #邮件服务器
  12. username = 'abc'  #昵称
  13. password = '123456'  #密码(发送邮箱密码)
  14.   
  15. msgRoot = MIMEMultipart('related')  
  16. msgRoot['Subject'] = 'test message'  
  17.   
  18. msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8')  
  19. msgRoot.attach(msgText)  
  20.   
  21. fp = open('D:\\lover.jpg', 'rb')  
  22. msgImage = MIMEImage(fp.read())  
  23. fp.close()  
  24.   
  25. msgImage.add_header('Content-ID', '<image1>')  
  26. msgRoot.attach(msgImage)  
  27.   
  28. smtp = smtplib.SMTP()  
  29. smtp.connect('smtp.163.com')  
  30. smtp.login(username, password)  
  31. smtp.sendmail(sender, receiver, msgRoot.as_string())  
  32. smtp.quit()  
复制代码



评分

参与人数 1鱼币 +5 收起 理由
格式天下 + 5 lz可以试试做做邮箱验证这个功能

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-6-22 10:27:07 | 显示全部楼层
看代码不是很难! 我才学到第五节课!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-6 20:25:23 | 显示全部楼层
:lol:厉害
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-8-9 10:29:20 | 显示全部楼层
不明觉厉啊,感觉好高端
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-19 09:57:49 | 显示全部楼层
大家都是转载的,跟百度出来的一样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-5-18 11:21:04 | 显示全部楼层
登陆可能有问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-5-18 14:01:45 | 显示全部楼层
高大上 下来看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-5-18 16:00:44 | 显示全部楼层
为了鱼币
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-5-18 16:14:53 | 显示全部楼层
为了鱼币

评分

参与人数 1鱼币 -10 收起 理由
~风介~ -10 请不要无意义灌水!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-7-6 16:35:08 | 显示全部楼层
谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-9-2 10:08:23 | 显示全部楼层
先收藏起来  肯定用得着
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-30 14:31:44 | 显示全部楼层
好贴  好贴 顶顶这个顶顶
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 01:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表