python3發送郵件
❶ 用python代碼給夜曲編程發送一封郵件,並附上你的成績單圖片
為了用Python代碼給夜曲編程發送一封郵件,需了解並運用smtplib和email兩個Python內置模塊。首先,smtplib模塊扮演著關鍵角色,負責實現郵件的連接、登錄與發送過程。通過調用相應函數,我們能夠實現郵件的順利投遞。
在使用smtplib時,我們通常需要先創建一個SMTP對象,然後使用它來連接郵件伺服器,接著進行登錄操作。登錄成功後,就可以利用該對象發送郵件了。登錄細節包括郵件伺服器的地址、埠號以及可能需要的賬號和密碼。
另一方面,email模塊則負責郵件內容的整合,包括收發件人、正文和附件等信息。藉助email.mime和email.utils等子模塊,可以方便地構建郵件的頭部信息,定義郵件主題、發件人、收件人等。
構建郵件正文和附件同樣依賴於email模塊。通過email.mime.multipart.MIMEMultipart實例,可以創建一個包含正文和附件的郵件。正文可以是純文本、HTML格式或附件鏈接等。而附件需要使用email.mime.base.MIMEBase或相關子類進行封裝,然後添加到郵件中。
在代碼實現中,我們需要指定發件人和收件人地址,設置郵件主題,編寫郵件正文,選擇是否添加附件,並確保郵件格式正確。通過精心構造的代碼,郵件將按照設定的參數發送到指定郵箱。
使用Python發送郵件的過程涉及多個步驟,包括伺服器連接、登錄驗證、郵件內容構建和最終的郵件發送。smtplib和email模塊提供了強大的工具,使得這一過程變得相對簡單。在完成郵件發送後,別忘了檢查郵件是否成功送達,並確認其內容是否符合預期。
❷ python,使用smtp發送郵件,求實例
from smtplib import SMTP
from RuckusAutoTest.models import TestCase
from email.MIMEMultipart import MIMEMultipart
from email.mime.application import MIMEApplication
def sendFildByMail(config):
print 'Preparing...',
message = MIMEMultipart( )
message['from'] = config['from']
message['to'] = config['to']
message['Reply-To'] = config['from']
message['Subject'] = config['subject']
message['Date'] = time.ctime(time.time())
message['X-Priority'] = '3'
message['X-MSMail-Priority'] = 'Normal'
message['X-Mailer'] = 'Microsoft Outlook Express 6.00.2900.2180'
message['X-MimeOLE'] = 'Proced By Microsoft MimeOLE V6.00.2900.2180'
f=open(config['file'], 'rb')
file = MIMEApplication(f.read())
f.close()
file.add_header('Content-Disposition', 'attachment', filename= os.path.basename(config['file']))
message.attach(file)
print 'OK'
print 'Logging...',
smtp = SMTP(config['server'])#, config['port'])
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(config['username'], config['password'])
print 'OK'
print 'Sending...',
smtp.sendmail (config['from'], [config['from'], config['to']], message.as_string())
print 'OK'
smtp.close()
time.sleep(1)
sendFildByMail({
'from': '[email protected]',
'to': '[email protected]',
'subject': ''This is an email test!',
'server': '123.125.50.132',
'username': 'username',
'password': 'password'})
可以注冊一個163郵箱試一下,是可以發郵件的。
在sendFildByMail這個函數里填上正確的參數,from是從哪個郵箱發送,也就是剛注冊的163郵箱,to是發送到哪個郵箱,可以填另一個郵箱來檢查是否能接收郵件,server不要改,這是163的地址;username和password那裡填寫163郵箱的用戶名和密碼。
希望能幫到你,有疑問請追問!
❸ 用Python發送郵件,可以群發,帶有多個附件
'''''
函數說明:Send_email_text()函數實現發送帶有附件的郵件,可以群發,附件格式包括:xlsx,pdf,txt,jpg,mp3等
參數說明:
1.subject:郵件主題
2.content:郵件正文
3.filepath:附件的地址,輸入格式為["","",...]
4.receive_email:收件人地址,輸入格式為["","",...]
'''
defSend_email_text(subject,content,filepath,receive_email):
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.
sender="發送方郵箱"
passwd="填入發送方密碼"
receivers=receive_email#收件人郵箱
msgRoot=MIMEMultipart()
msgRoot['Subject']=subject
msgRoot['From']=sender
iflen(receivers)>1:
msgRoot['To']=','.join(receivers)#群發郵件
else:
msgRoot['To']=receivers[0]
part=MIMEText(content)
msgRoot.attach(part)
##添加附件部分
forpathinfilepath:
if".jpg"inpath:
#jpg類型附件
jpg_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=jpg_name)
msgRoot.attach(part)
if".pdf"inpath:
#pdf類型附件
pdf_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=pdf_name)
msgRoot.attach(part)
if".xlsx"inpath:
#xlsx類型附件
xlsx_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=xlsx_name)
msgRoot.attach(part)
if".txt"inpath:
#txt類型附件
txt_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=txt_name)
msgRoot.attach(part)
if".mp3"inpath:
#mp3類型附件
mp3_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=mp3_name)
msgRoot.attach(part)
try:
s=smtplib.SMTP()
s.connect("smtp.mail.aliyun.com")#這里我使用的是阿里雲郵箱,也可以使用163郵箱:smtp.163.com
s.login(sender,passwd)
s.sendmail(sender,receivers,msgRoot.as_string())
print("郵件發送成功")
exceptsmtplib.SMTPExceptionase:
print("Error,發送失敗")
finally:
s.quit()