9

所以我正在按照教程在 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)
4

1 回答 1

12
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`
于 2015-08-10T11:07:23.003 回答