溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python 配置郵件發(fā)送服務(wù)器發(fā)送郵件

發(fā)布時間:2020-07-11 20:34:13 來源:網(wǎng)絡(luò) 閱讀:197 作者:wx5dcb7577ac572 欄目:編程語言

郵件發(fā)送腳本

#coding:utf-8
# #!/usr/bin/python
import smtplib ,os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
'''
SERVER_EMAIL = 'xaohuihuitest@gmail.com'
# Mailserver configuration EMAIL_HOST = "smtp.163.com"
EMAIL_PORT = 25
EMAIL_HOST_USER='xaohuihuitest@gmail.com'
EMAIL_HOST_PASSWORD='xaohuihui'
'''
def send_mail(sub, content, status = False, filename = ''):  #to_list:收件人;sub:主題;content:郵件內(nèi)容
    message = MIMEMultipart()
    mailto = os.environ.get('sjryx')
    mailto_list = mailto.split(',')  # 此處設(shè)置 要接受通知郵件的郵箱地址
    mail_host = "smtpdm.aliyun.com"  # 設(shè)置服務(wù)器
    mail_user = "alert@mail.suner.org"  # 用戶名
    mail_pass = "Xaohuihuitest123"  # 口令
    mail_postfix = "suner.org"  # 發(fā)件箱的后綴
    message['To'] = ",".join(mailto_list) #收件郵箱地址
    message['From'] = 'suner:sd_credit' #發(fā)件人詳情
    message['Subject'] = sub  #主題
    message.attach(MIMEText(content, 'html', 'utf-8')) #正文內(nèi)容
    if status:         # 構(gòu)造附件,傳送當(dāng)前目錄下的 test.txt 文件
        fileatt = MIMEText(open(filename, 'rb').read(), 'base64', 'gb2312')
        fileatt["Content-Type"] = 'application/octet-stream'                # 這里的filename可以任意寫,寫什么名字,郵件中顯示什么名字
        fileatt["Content-Disposition"] = 'attachment; filename="' + filename + '"'
        message.attach(fileatt)
    try:
        s = smtplib.SMTP() #創(chuàng)建郵件發(fā)送實例
        s.connect(mail_host)  #連接smtp服務(wù)器
        s.login(mail_user,mail_pass)  #登陸服務(wù)器
        me = "<"+mail_user+"@"+mail_postfix+">"
        s.sendmail(me, mailto_list, message.as_string()) #發(fā)送郵件
        s.close()
        print '郵件發(fā)送成功'
        return True
    except Exception, e:
        print '郵件發(fā)送失敗', str(e)
        import traceback
        traceback.print_exc()
        return False
向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI