欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Python發(fā)送email的三種方式

 更新時間:2018年10月18日 13:44:58   作者:mimvp  
這篇文章主要介紹了詳解Python發(fā)送email的三種方式,Python發(fā)送email的三種方式,分別為使用登錄郵件服務(wù)器、使用smtp服務(wù)、調(diào)用sendmail命令來發(fā)送三種方法,非常具有實(shí)用價值,需要的朋友可以參考下

Python發(fā)送email的三種方式,分別為使用登錄郵件服務(wù)器、使用smtp服務(wù)、調(diào)用sendmail命令來發(fā)送三種方法

Python發(fā)送email比較簡單,可以通過登錄郵件服務(wù)來發(fā)送,linux下也可以使用調(diào)用sendmail命令來發(fā)送,還可以使用本地或者是遠(yuǎn)程的smtp服務(wù)來發(fā)送郵件,不管是單個,群發(fā),還是抄送都比較容易實(shí)現(xiàn)。本米撲博客先介紹幾個最簡單的發(fā)送郵件方式記錄下,像html郵件,附件等也是支持的,需要時查文檔即可。

一、登錄郵件服務(wù)器

通過smtp登錄第三方smtp郵箱發(fā)送郵件,支持 25 和 465端口

vim python_email_1.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# author: mimvp.com
# 2015.10.05
 
 
import smtplib 
from email.mime.text import MIMEText 
  
smtpHost = 'smtp.exmail.qq.com' 
sender = 'robot@mimvp.com' 
password = "mimvp-password" 
receiver = 'yanggang@mimvp.com'
  
content = 'hello mimvp.com' 
msg = MIMEText(content) 
  
msg['Subject'] = 'email-subject' 
msg['From'] = sender 
msg['To'] = receiver 
  
## smtp port 25
smtpServer = smtplib.SMTP(smtpHost, 25)     # SMTP
smtpServer.login(sender, password) 
smtpServer.sendmail(sender, receiver, msg.as_string()) 
smtpServer.quit() 
print 'send success by port 25' 
 
## smtp ssl port 465
smtpServer = smtplib.SMTP_SSL(smtpHost, 465)  # SMTP_SSL
smtpServer.login(sender, password) 
smtpServer.sendmail(sender, receiver, msg.as_string()) 
smtpServer.quit() 
print 'send success by port 465'

執(zhí)行命令:

$ python python_email_1.py 
send success by port 25
send success by port 465

發(fā)送結(jié)果,會收到兩封郵件,截圖其中一份郵件如下圖:

二、使用smtp服務(wù)

測試失敗,略過或留言指正

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# author: mimvp.com
# 2015.10.05
 
 
import smtplib 
from email.mime.text import MIMEText 
import subprocess
  
smtpHost = 'smtp.exmail.qq.com' 
sender = 'robot@mimvp.com' 
password = "mimvp-password" 
receiver = 'yanggang@mimvp.com'
  
content = 'hello mimvp.com' 
msg = MIMEText(content)  
  
  
 
if __name__ == "__main__":  
  p = subprocess.Popen(['/usr/sbin/sendmail', '-t'], stdout=subprocess.PIPE) 
  print(str(p.communicate()))
  p_res = str(p.communicate()[0])
  msg = MIMEText(p_res)
 
  msg["From"] = sender 
  msg["To"] = receiver 
  msg["Subject"] = "hello mimvp.com" 
  s = smtplib.SMTP(smtpHost) 
  s.login(sender, password)
  s.sendmail(sender, receiver, msg.as_string()) 
  s.quit() 
  print 'send success'

三、調(diào)用sendmail命令

調(diào)用本機(jī)linux自身sendmail服務(wù)發(fā)送郵件,不需要啟動sendmail后臺進(jìn)程,不需要發(fā)送者登錄,郵件發(fā)送者可以是任意名字,沒有限制。

特別注意:sendmail 命令發(fā)送郵件,默認(rèn)用25端口號,由于阿里云、騰訊云等封禁了25端口號,因此本示例需在開通25端口機(jī)器上測試

vim python_email_3.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# author: mimvp.com
# 2015.10.05
 
 
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
import commands
 
import sys 
reload(sys)
sys.setdefaultencoding('utf-8')
 
def send_mail(sender, recevier, subject, html_content):
    msg = MIMEText(html_content, 'html', 'utf-8')
    msg["From"] = sender
    msg["To"] = recevier
    msg["Subject"] = subject
    p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
    p.communicate(msg.as_string())
 
 
sender = 'robot@mimvp.com'
recevier = 'yanggang@mimvp.com'
subject = 'sendmail-subject'
html_content = 'hello mimvp.com'
send_mail(sender, recevier, subject, html_content)

執(zhí)行命令:

python python_email_3.py

收件結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python selenium抓取微博內(nèi)容的示例代碼

    Python selenium抓取微博內(nèi)容的示例代碼

    本篇文章主要介紹了Python selenium抓取微博內(nèi)容的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Python增強(qiáng)下git那長長的指令詳解

    Python增強(qiáng)下git那長長的指令詳解

    這篇文章主要介紹了Python增強(qiáng)下git那長長的指令 ,在開發(fā)中用到的代碼目錄結(jié)構(gòu),本文也給大家詳細(xì)講解,需要的朋友可以參考下
    2021-09-09
  • python中torch.nn.identity()方法詳解

    python中torch.nn.identity()方法詳解

    今天看源碼時遇到的這個恒等函數(shù),就如同名字那樣占位符,并沒有實(shí)際操作,下面這篇文章主要給大家介紹了關(guān)于python中torch.nn.identity()方法的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • python執(zhí)行CMD指令,并獲取返回的方法

    python執(zhí)行CMD指令,并獲取返回的方法

    今天小編就為大家分享一篇python執(zhí)行CMD指令,并獲取返回的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python實(shí)現(xiàn)隨機(jī)加減法生成器

    python實(shí)現(xiàn)隨機(jī)加減法生成器

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)隨機(jī)加減法生成器,練手小項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • 把MySQL表結(jié)構(gòu)映射為Python中的對象的教程

    把MySQL表結(jié)構(gòu)映射為Python中的對象的教程

    這篇文章主要介紹了簡單地把MySQL表結(jié)構(gòu)映射為Python中的對象的方法,用到了Python中的SQLAlchemy庫,需要的朋友可以參考下
    2015-04-04
  • python爬取免費(fèi)代理并驗(yàn)證代理是否可用

    python爬取免費(fèi)代理并驗(yàn)證代理是否可用

    這篇文章主要介紹了python爬取免費(fèi)代理并驗(yàn)證是否可用,通過本文給大家介紹了在什么情況下會用到代理并分享腳本的完整代碼,需要的朋友可以參考下
    2022-01-01
  • 對python 合并 累加兩個dict的實(shí)例詳解

    對python 合并 累加兩個dict的實(shí)例詳解

    今天小編就為大家分享一篇對python 合并 累加兩個dict的實(shí)例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python實(shí)現(xiàn)去除代碼前行號的方法

    Python實(shí)現(xiàn)去除代碼前行號的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)去除代碼前行號的方法,實(shí)例分析了Python操作字符的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • 使用Python處理json字符串中的非法雙引號問題

    使用Python處理json字符串中的非法雙引號問題

    這篇文章主要介紹了使用Python處理json字符串中的非法雙引號問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02

最新評論