我正在尝试使用以下代码发送带有电子邮件的附件。但它给出了一个错误。没有附件,它可以完美地工作。这段代码有什么问题?
"mail5.py", line 14
smtpObj = smtplib.SMTP('domain', 25)
^
SyntaxError: invalid syntax
#!/usr/bin/python
import smtplib
sender = 'a@a.com'
receivers = ['b@b.com']
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
msg = MIMEMultipart()
msg.attach(MIMEText(file("text.txt").read())
smtpObj = smtplib.SMTP('domain', 25)
smtpObj.sendmail(sender, receivers, msg.as_string())
print "Successfully sent email"
答案 0 :(得分:2)
看起来你错过了前一行的右括号,试试这个:
msg.attach(MIMEText(file("text.txt").read()))