溫馨提示×

溫馨提示×

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

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

python 配置郵件發送服務器發送郵件

發布時間:2020-07-11 20:34:13 來源:網絡 閱讀:235 作者:wx5dcb7577ac572 欄目:編程語言

郵件發送腳本

#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:郵件內容
    message = MIMEMultipart()
    mailto = os.environ.get('sjryx')
    mailto_list = mailto.split(',')  # 此處設置 要接受通知郵件的郵箱地址
    mail_host = "smtpdm.aliyun.com"  # 設置服務器
    mail_user = "alert@mail.suner.org"  # 用戶名
    mail_pass = "Xaohuihuitest123"  # 口令
    mail_postfix = "suner.org"  # 發件箱的后綴
    message['To'] = ",".join(mailto_list) #收件郵箱地址
    message['From'] = 'suner:sd_credit' #發件人詳情
    message['Subject'] = sub  #主題
    message.attach(MIMEText(content, 'html', 'utf-8')) #正文內容
    if status:         # 構造附件,傳送當前目錄下的 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() #創建郵件發送實例
        s.connect(mail_host)  #連接smtp服務器
        s.login(mail_user,mail_pass)  #登陸服務器
        me = "<"+mail_user+"@"+mail_postfix+">"
        s.sendmail(me, mailto_list, message.as_string()) #發送郵件
        s.close()
        print '郵件發送成功'
        return True
    except Exception, e:
        print '郵件發送失敗', str(e)
        import traceback
        traceback.print_exc()
        return False
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女