Python 指令碼傳送郵件

2021-08-20 21:47:57 字數 1859 閱讀 2863

使用python完成郵件自動傳送:

# !/usr/bin/python

# -*- coding: utf-8 -*-

import smtplib

from email.header import header

from email.mime.text import mimetext

class email_sender(object):

def __init__(self,mailto_host,mail_user,mail_pass,mail_postfix):

self.mailto_host = mailto_host

self.mail_user = mail_user

self.mail_pass = mail_pass

self.mail_postfix = mail_postfix

class email_reciever(object):

def __init__(self,reciever):

self.mail_reciever = reciever

def send_email(sender, receivers, sub, content):

me = 'from jon'

rec = 'to sun'

msg = mimetext(content, 'plain', 'utf-8')

#顯示郵件來自,這裡可以自定義

msg['from'] = header(me,'utf-8')

#顯示郵件標題

msg['to'] = header(rec, 'utf-8')

#顯示郵件內容

msg['subject'] = header(sub, 'utf-8')

try:

server = smtplib.smtp()

#指定連線的郵箱伺服器,伺服器的埠號

server.connect(sender.mailto_host,25)

#登入郵箱

server.login(sender.mail_user, sender.mail_pass)

#傳送郵件

server.sendmail(sender.mail_user+'@'+sender.mail_postfix, receivers.mail_reciever, msg.as_string())

server.quit()

server.close()

return true

except exception as e:

print(str(e))

return false

if __name__ == '__main__':

#傳送方郵件引數初始化

mailto_host = 'smtp.163.com'

mail_user = '*********'

#這裡要是163郵箱則密碼是授權碼,授權碼,授權碼!!!!!

mail_pass = '******x'

mail_postfix = '163.com'

sender = email_sender(mailto_host,mail_user,mail_pass,mail_postfix);

#接收方郵件初始化

reciever = '******xx'

recivers = email_reciever(reciever)

#郵件內容

content = '內容是python smtp 郵件測試....'

#郵件主題

sub = '測試'

send_email(sender, recivers, sub, content)

python郵件傳送指令碼

複製 usr bin python coding utf 8 author dengyike date 2010 09 28 version 1.0 description auto sending email with attachment file import email import mim...

使用python指令碼傳送郵件

import smtplib from email.mime.text import mimetext from email.header import header 傳送方 sender 163.com 接收方 receiver 163.com 中文需要utf 8引數,不好使需要調整 在這裡輸入郵...

Python 指令碼自動傳送郵件

自動傳送郵件功能是我們經常要用到的,比如每天定時統計報表資訊,然後自動傳送給運營人員,協助運營人員進行業務資料分析。本文是用python寫的乙個自動傳送郵件的指令碼,呼叫函式時,直接把發件人郵箱位址 密碼 收件人郵箱位址 郵件標題 內容等資訊傳遞給函式,即可實現自動傳送郵件功能。指令碼 如下 pyt...