简体   繁体   中英

Sending an Attachment with e-mail using Python

I'm trying send an attachment with an e-mail using following code. But it gives an error. Without the attachment it works perfectly. What is the problem with this code?

"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"

看起来你错过了前一行的右括号,试试这个:

msg.attach(MIMEText(file("text.txt").read()))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM