2013-07-02 53 views
4

因此,我正在按照python發送電子郵件的教程,問題在於它是爲Python 2而不是python 3編寫的(這是我的)。所以這裏是我想要得到答案什麼是在Python 3中的電子郵件模塊?我想要得到的是具體的模塊是:python 3中的電子郵件模塊

from email.MIMEMultipart import MIMEMultipart 
from email.MIMEText import MIMETex 

我也有一個feelling,當我得到了這個模塊會出現錯誤(沒有到達那裏又因爲 的模塊上方給錯誤

import smtp 

這裏是腳本:

from email.MIMEMultipart import MIMEMultipart 
from email.MIMEText import MIMETex 

fromaddr = ("[email protected]") 
toaddr = ("[email protected]") 
msg = MIMEMultipart 
msg['From'] = fromaddr 
msg['To'] = toaddr 
msg['Subject'] = ("test") 

body = ("This is a test sending email through python") 
msg.attach(MIMEText(body, ('plain'))) 

import smptlib 
server = smptlib.SMPT('mail.mchsi.com, 456') 
server.login("[email protected]", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX") 
text = msg.as_string() 
sender.sendmail(fromaddr, toaddr, text) 
+2

那些位於'email.mime':http://docs.python.org/3.3/library/email.mime.html#module-email.mime – michaelmeyer

+0

@ doukremt是的,這解決了我的問題,但現在得到這個錯誤Traceback(最近調用最後): 文件「C:\ Python33 \ emailtest.py」,第9行,在 msg ['From'] = fromaddr TypeError:'類型「對象不支持項目分配 –

+0

@ raspberry.pi您將'msg'分配給'MIMEMultipart'類,它是類。你忘了括號:msg = MIMEMultipart()' – andersschuller

回答

3
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 

順便說這裏再在你的代碼的一些錯誤:

fromaddr = "[email protected]" # redundant parentheses 
toaddr = "[email protected]" # redundant parentheses 
msg = MIMEMultipart() # not redundant this time :) 
msg['From'] = fromaddr 
msg['To'] = toaddr 
msg['Subject'] = "test" # redundant parentheses 

body = "This is a test sending email through python" # redundant parentheses 
msg.attach(MIMEText(body, 'plain')) # redundant parentheses 

import smtplib # SMTP! NOT SMPT!! 
server = smtplib.SMTP('mail.mchsi.com', 456) # `port` is an integer 
server.login("[email protected]", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX") # on most SMTP servers you should remove domain name(`@mchsi.com`) here 
text = msg.as_string() 
server.sendmail(fromaddr, toaddr, text) # it's not `sender` 
相關問題