發(fā)送郵件的時候,需要發(fā)送人,收件人,和一臺郵件服務(wù)器,這里使用python發(fā)送一個郵件,主要需要引入smtplib和email庫。
下面是源碼,粘貼即可用:
創(chuàng)新互聯(lián)成立十余年來,這條路我們正越走越好,積累了技術(shù)與客戶資源,形成了良好的口碑。為客戶提供網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、網(wǎng)站策劃、網(wǎng)頁設(shè)計、域名注冊、網(wǎng)絡(luò)營銷、VI設(shè)計、網(wǎng)站改版、漏洞修補等服務(wù)。網(wǎng)站是否美觀、功能強大、用戶體驗好、性價比高、打開快等等,這些對于網(wǎng)站建設(shè)都非常重要,創(chuàng)新互聯(lián)通過對建站技術(shù)性的掌握、對創(chuàng)意設(shè)計的研究為客戶提供一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進步。
#!/usr/bin/env python3
# coding: utf-8
import smtplib
import time
import sys
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 發(fā)送郵件
def SendMail(mail_list, subject, content):
from_mail = '這里填入發(fā)件人郵箱'
temp_msg = '武松申請了車費報銷'
msg = MIMEText('
' + temp_msg + '
同意,不同意', 'html','utf-8') msg['Subject'] = subject msg['From'] = from_mail msg['To'] = mail_list msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z') smtp_server = '這里填入郵件服務(wù)器的地址' s = smtplib.SMTP(smtp_server) s.login("填入登錄郵件服務(wù)器的郵箱", "填入郵箱密碼") s.sendmail(from_mail, mail_list.split(","), msg.as_string()) s.quit() # 發(fā)送郵件帶附件 def SendMailWithAttachment(mail_list, subject, content, attachment): from_mail = '這里填入發(fā)件人郵箱' msg = MIMEMultipart() msg['Subject'] = subject msg['From'] = from_mail msg['To'] = mail_list msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z') msg.attach(MIMEText(content, 'html', 'utf-8')) att = MIMEText(open(attachment, 'rb').read(), 'base64', 'utf8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="%s"' % attachment msg.attach(att) smtp_server = '這里填入郵件服務(wù)器的地址' s = smtplib.SMTP(smtp_server) s.login("填入登錄郵件服務(wù)器的郵箱", "填入郵箱密碼") s.sendmail(from_mail, mail_list.split(","), msg.as_string()) s.quit() if __name__ == '__main__': if len(sys.argv) < 4: print('Usage:', sys.argv[0], ' mail_list subject content [attachment]') sys.exit(1) content = "" if len(sys.argv) == 4: SendMail(sys.argv[1], sys.argv[2], content) else: SendMailWithAttachment(sys.argv[1], sys.argv[2], content, sys.argv[4])
你需要修改的地方是:
from_mail:發(fā)件人信息
temp_msg:發(fā)送的內(nèi)容
smtp_server:郵件服務(wù)器地址
s.login:郵件服務(wù)器登錄名和密碼
命令行發(fā)送:
python3 sendmail.py '772654204@qq.com' 'python send mail' 'leran is fun'
顯示效果: