1. 程式人生 > >python實現傳送郵件

python實現傳送郵件

本文目錄

  • 一 使用SMTP模組傳送郵件
  • 二 傳送html格式郵件
  • 三 傳送帶附件的郵件
  •  四 Django傳送郵件
  • 各大郵箱smtp伺服器及埠
  •  qq郵箱配置smtp
 

一 使用SMTP模組傳送郵件

複製程式碼
import smtplib
from email.mime.text import MIMEText
from email.header import Header
msg_from = '***@qq.com'  # 傳送方郵箱
passwd = '****'  # 填入傳送方郵箱的授權碼(填入自己的授權碼,相當於郵箱密碼)
msg_to = ['****@qq.com','**@163.com','*****@163.com']  # 收件人郵箱
# msg_to = '
[email protected]
' # 收件人郵箱 subject = "郵件標題" # 主題 content = "郵件內容,我是郵件內容,哈哈哈" # 生成一個MIMEText物件(還有一些其它引數) msg = MIMEText(content) # 放入郵件主題 msg['Subject'] = subject # 也可以這樣傳參 # msg['Subject'] = Header(subject, 'utf-8') # 放入發件人 msg['From'] = msg_from # 放入收件人 # msg['To'] = '[email protected]
' # msg['To'] = '發給你的郵件啊' try: # 通過ssl方式傳送,伺服器地址,埠 s = smtplib.SMTP_SSL("smtp.qq.com", 465) # 登入到郵箱 s.login(msg_from, passwd) # 傳送郵件:傳送方,收件方,要傳送的訊息 s.sendmail(msg_from, msg_to, msg.as_string()) print('成功') except s.SMTPException as e: print(e) finally: s.quit()
複製程式碼  

二 傳送html格式郵件

複製程式碼
import smtplib
from email.mime.text import MIMEText
from email.header import Header

msg_from = '[email protected]'  # 傳送方郵箱
passwd = 'ldoetnwqdjqqbjjj'  # 填入傳送方郵箱的授權碼(填入自己的授權碼,相當於郵箱密碼)
msg_to = ['[email protected]']  # 收件人郵箱
# msg_to = '[email protected]'  # 收件人郵箱

subject = "郵件標題"  # 主題
# *************傳送html的郵件**********
content = '''
<p>Python 郵件傳送測試...</p>
<p><a href="http://www.baidu.com">這是一個連結</a></p>
'''
# 生成一個MIMEText物件
msg = MIMEText(content)
# 放入郵件主題
msg['Subject'] = subject
# 也可以這樣傳參
# msg['Subject'] = Header(subject, 'utf-8')
# 放入發件人
msg['From'] = msg_from
# 放入收件人
# msg['To'] = '[email protected]'
# msg['To'] = '發給你的郵件啊'
try:
    # 通過ssl方式傳送
    s = smtplib.SMTP_SSL("smtp.qq.com", 465)
    # 登入到郵箱
    s.login(msg_from, passwd)
    # 傳送郵件:傳送方,收件方,要傳送的訊息
    s.sendmail(msg_from, msg_to, msg.as_string())
    print('成功')
except s.SMTPException as e:
    print(e)
finally:
    s.quit()
複製程式碼  

三 傳送帶附件的郵件

複製程式碼
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email import  encoders
msg_from = '[email protected]'  # 傳送方郵箱
passwd = '***'  # 填入傳送方郵箱的授權碼(填入自己的授權碼,相當於郵箱密碼)
msg_to = ['[email protected]']  # 收件人郵箱

subject = "郵件標題"  # 主題
# 建立一個帶附件的例項
msg = MIMEMultipart()
# 放入郵件主題
msg['Subject'] = subject
# 也可以這樣傳參
# msg['Subject'] = Header(subject, 'utf-8')
# 放入發件人
msg['From'] = msg_from

# 郵件正文內容
msg.attach(MIMEText('Python 郵件傳送測試……', 'plain', 'utf-8'))

# 構造附件1,傳送當前目錄下的 test.txt 檔案
att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 這裡的filename可以任意寫,寫什麼名字,郵件中顯示什麼名字
att1["Content-Disposition"] = 'attachment; filename="test.txt"'
msg.attach(att1)

# 構造附件2,
with open('test.png', 'rb') as f:
    # 設定附件的MIME和檔名,這裡是png型別:
    mime = MIMEBase('image', 'png', filename='test.png')
    # 加上必要的頭資訊:
    mime.add_header('Content-Disposition', 'attachment', filename='test.png')
    mime.add_header('Content-ID', '<0>')
    mime.add_header('X-Attachment-Id', '0')
    # 把附件的內容讀進來:
    mime.set_payload(f.read())
    # 用Base64編碼:
    encoders.encode_base64(mime)
    # 新增到MIMEMultipart:
    msg.attach(mime)
# 構造附件3,圖片格式
fp = open('test.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# 定義圖片 ID,在 HTML 文字中引用
msgImage.add_header('Content-ID', '<image1>')
msg.attach(msgImage)
try:
    # 通過ssl方式傳送
    s = smtplib.SMTP_SSL("smtp.qq.com", 465)
    # 登入到郵箱
    s.login(msg_from, passwd)
    # 傳送郵件:傳送方,收件方,要傳送的訊息
    s.sendmail(msg_from, msg_to, msg.as_string())
    print('成功')
except s.SMTPException as e:
    print(e)
finally:
    s.quit()
複製程式碼  

 四 Django傳送郵件

在setting中配置

複製程式碼
# EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.qq.com'  # 如果是 163 改成 smtp.163.com
EMAIL_PORT = 465
EMAIL_HOST_USER = '[email protected]'  # 帳號
EMAIL_HOST_PASSWORD = '***'  # 密碼
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
#這樣收到的郵件,收件人處就會這樣顯示
#DEFAULT_FROM_EMAIL = 'lqz<'[email protected]>'
EMAIL_USE_SSL = True   #使用ssl
#EMAIL_USE_TLS = False # 使用tls

#EMAIL_USE_SSL 和 EMAIL_USE_TLS 是互斥的,即只能有一個為 True
複製程式碼

view檢視函式

複製程式碼
    from django.core.mail import send_mail
    import threading
    from mybbs import settings

    t = threading.Thread(target=send_mail, args=("您的文章%s新增了一條評論內容" ,
                                                 'ddd',
                                                 settings.EMAIL_HOST_USER,
                                                 ["[email protected]"])
                         )
    t.start()
複製程式碼

一次性發多封郵件

複製程式碼
from django.core.mail import send_mass_mail

message1 = ('第一封郵件標題', '這是郵件內容', '[email protected]', ['[email protected]', '[email protected]'])
message2 = ('第二封郵件標題', '這是郵件內容', '[email protected]', ['[email protected]'])
'''
fail_silently: (可選)布林值。為 False 時, send_mail 會丟擲 smtplib.SMTPException 異常。smtplib 文件列出了所有可能的異常。 這些異常都是 SMTPException 的子類
'''
send_mass_mail((message1, message2), fail_silently=False)
'''
send_mail 每次發郵件都會建立一個連線,發多封郵件時建立多個連線。而 send_mass_mail 是建立單個連線傳送多封郵件,所以一次性發送多封郵件時 send_mass_mail 要優於 send_mail。
'''
複製程式碼

攜帶附件或傳送html(需要接收方支援)

複製程式碼
from django.core.mail import EmailMultiAlternatives
# subject 主題 content 內容 to_addr 是一個列表,傳送給哪些人
msg = EmailMultiAlternatives('郵件標題', '郵件內容', '傳送方', ['接收方'])
msg.content_subtype = "html"
# 新增附件(可選)
msg.attach_file('test.txt')
# 傳送
msg.send()
複製程式碼

備註:send_mail 每次發郵件都會建立一個連線,發多封郵件時建立多個連線。而 send_mass_mail 是建立單個連線傳送多封郵件,所以一次性發送多封郵件時 send_mass_mail 要優於 send_mail。

 

各大郵箱smtp伺服器及埠

新浪郵箱smtp伺服器
外發伺服器:smtp.vip.sina.com
收件伺服器:pop3.vip.sina.com
新浪免費郵件
外發伺服器:smtp.sina.com.cn
收件伺服器:pop3.sina.com.cn
163郵箱smtp伺服器
pop: pop.163.com
smtp: smtp.163.com
QQ郵箱smtp伺服器及埠
接收郵件伺服器:imap.exmail.qq.com,使用SSL,埠號993
傳送郵件伺服器:smtp.exmail.qq.com,使用SSL,埠號465或587
yahoo郵箱smtp伺服器
接:pop.mail.yahoo.com.cn
發:smtp.mail.yahoo.com
126郵箱smtp伺服器
pop: pop.126.com
smtp: smtp.126.com
新浪免費郵箱
POP3:pop.sina.com
SMTP:smtp.sina.com
SMTP埠號:25
新浪VIP郵箱
POP3:pop3.vip.sina.com
SMTP:smtp.vip.sina.com
SMTP埠號:25
新浪企業郵箱
POP3:pop.sina.com
SMTP:smtp.sina.com
SMTP埠號:25
雅虎郵箱
POP3:pop.mail.yahoo.cn
SMTP:smtp.mail.yahoo.cn
SMTP埠號:25
搜狐郵箱
POP3:pop3.sohu.com
SMTP:smtp.sohu.com
SMTP埠號:25
TOM郵箱
POP3:pop.tom.com
SMTP:smtp.tom.com
SMTP埠號:25
Gmail郵箱
POP3:pop.gmail.com
SMTP:smtp.gmail.com
SMTP埠號:587 或 25
QQ郵箱
POP3:pop.exmail.qq.com
SMTP:smtp.exmail.qq.com
SMTP埠號:25
263郵箱
域名:263.net
POP3:263.net
SMTP:smtp.263.net
SMTP埠號:25
域名:x263.net
POP3:pop.x263.net
SMTP:smtp.x263.net
SMTP埠號:25
域名:263.net.cn
POP3:263.net.cn
SMTP:263.net.cn
SMTP埠號:25
域名:炫我型
POP3:pop.263xmail.com
SMTP:smtp.263xmail.com
SMTP埠號:25
21CN 免費郵箱
POP3:pop.21cn.com
SMTP:smtp.21cn.com
IMAP:imap.21cn.com
SMTP埠號:25
21CN 經濟郵郵箱
POP3:pop.21cn.com
SMTP:smtp.21cn.com
SMTP埠號:25
21CN 商務郵郵箱
POP3:pop.21cn.net
SMTP:smtp.21cn.net
SMTP埠號:25
21CN 快感郵箱
POP3:vip.21cn.com
SMTP:vip.21cn.com
SMTP埠號:25
21CN Y郵箱
POP3:pop.y.vip.21cn.com
SMTP:smtp.y.vip.21cn.com
SMTP埠號:25
中華網任我郵郵箱
POP3:rwpop.china.com
SMTP:rwsmtp.china.com
SMTP埠號:25
中華網時尚、商務郵箱
POP3:pop.china.com
SMTP:smtp.china.com
SMTP埠號:25
View Code  

 qq郵箱配置smtp

登入qq郵箱:

 

開啟smtp服務,生成授權碼