2016-01-18 30 views
2

我是Python的新手,因此在使用python腳本發送郵件時遇到了一些困難。我使用mime和smtplib。出於某種原因,我無法發送郵件並且發送郵件沒有任何機構,附上代碼。 任何想法?如何在Python中使用Mimemultipart發送郵件

import smtplib 
from email.mime.text import MIMEText 
from email.mime.base import MIMEBase 
from email.mime.multipart import MIMEMultipart 

hostname='hello' 

sender = '[email protected]' 
receivers = ['[email protected]'] 

msg = MIMEMultipart() 
msg['From'] = sender 
msg['To'] = receivers 
msg['Subject'] = "Upgrade started for"+hostname+"." 


try: 
    smtpObj = smtplib.SMTP('mail.xxx.com',25) 
    smtpObj.sendmail(sender, receivers, msg.as_string()) 
    print "Successfully sent email" 
except: 
     print "Error: unable to send email" 

回答

1

我有這個選項的工作原理是postfix

import smtplib 
from email.mime.text import MIMEText 
s = smtplib.SMTP() 
s.connect('localhost') # connect to the SMTP server 
doc = "<html><h1>Test title</h1></html>" 
while True: 
    msg = MIMEText(doc, _subtype='html', _charset='utf-8') 
    s.sendmail('[email protected]', ['[email protected]'], msg.as_string()) 
s.quit() 
+0

這工作!謝謝 – Vholla