python 3中的电子邮件模块

时间:2013-07-02 17:37:09

标签: python email

所以我按照教程在python中发送电子邮件,问题是它是为python 2而不是python 3编写的(这就是我所拥有的)。所以这就是我想要的答案是什么是python 3中的电子邮件模块?我想要的具体模块是:

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

我还有一种感觉,当我接触到这个模块时会出现错误(还没有到达那里因为 上面的模块给出了错误

import smtp

这是脚本:

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

fromaddr = ("XXXXX@mchsi.com")
toaddr = ("XXXX@mchsi.com")
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("XXXXX@mchsi.com", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
text = msg.as_string()
sender.sendmail(fromaddr, toaddr, text)

1 个答案:

答案 0 :(得分:9)

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

顺便说一下,你的代码中存在一些错误:

fromaddr = "XXXXX@mchsi.com" # redundant parentheses
toaddr = "XXXX@mchsi.com" # 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("XXXXX@mchsi.com", "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`