Python 傳送 email 的三種方式

2021-09-13 03:46:07 字數 3718 閱讀 2669

python傳送email的三種方式,分別為使用登入郵件伺服器、使用smtp服務、呼叫sendmail命令來傳送三種方法

python傳送email比較簡單,可以通過登入郵件服務來傳送,linux下也可以使用呼叫sendmail命令來傳送,還可以使用本地或者是遠端的smtp服務來傳送郵件,不管是單個,**,還是抄送都比較容易實現。本公尺撲部落格先介紹幾個最簡單的傳送郵件方式記錄下,像html郵件,附件等也是支援的,需要時查文件即可。

一、登入郵件伺服器

通過smtp登入第三方smtp郵箱傳送郵件,支援 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 = '[email protected]'

password = "mimvp-password"

receiver = '[email protected]'

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'

執行命令:

$ python python_email_1.py 

send success by port 25

send success by port 465

傳送結果,會收到兩封郵件,截圖其中乙份郵件如下圖:

二、使用smtp服務

#!/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 = '[email protected]'

password = "mimvp-password"

receiver = '[email protected]'

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'

三、呼叫sendmail命令呼叫本機linux自身sendmail服務傳送郵件,不需要啟動sendmail後台程序,不需要傳送者登入,郵件傳送者可以是任意名字,沒有限制。

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 = '[email protected]'

recevier = '[email protected]'

subject = 'sendmail-subject'

html_content = 'hello mimvp.com'

send_mail(sender, recevier, subject, html_content)

執行命令:

python python_email_3.py
收件結果:

利用Python傳送email

引入smtplib和email.mime.text.mimetextimport smtplib 將你寫的字串轉化為郵件的文字形式 from email.mime.text import mimetext smtp伺服器位址 smtp server smtp.163.com 傳送者是誰 sender...

python傳送email郵件

1.使用qq郵箱做服務 郵 2。使用 coding utf 8 from import res import smtplib from email.mime.text import mimetext from email.utils import formataddr my sender 15964...

Python 傳送email的幾種方式

python傳送email還是比較簡單的,能夠通過登入郵件服務來傳送,linux下也能夠使用呼叫sendmail命令來傳送,還能夠使用本地或者是遠端的smtp服務來傳送郵件,無論是單個,還是抄送都比較easy實現。先把幾個最簡單的傳送郵件方式記錄下,像html郵件,附件等也是支援的,須要時查文件就可...