I'm trying to send attachments via email with python but I'm getting this error:
msg.attach(msgImage) AttributeError: 'str' object has no attribute 'attach'
Here is the code:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.mime.image import MIMEImage
def send_email():
fromaddr = '[email protected]'
toaddrs = 'Toemail'
global msg
subject = 'RESPOSTA'
message = 'Subject: %s\n\n%s' % (subject, msg)
username = '[email protected]'
password = 'xxxxxxxx'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
fp = open ('C:\Python27\Scripts\pares.txt', 'rb')
msgImage = MIMEImage (fp.read(), _subtype='txt')
fp.close()
msg.attach(msgImage)
server.sendmail(fromaddr, toaddrs, message, msg.as_string())
server.quit()
msg = 'Email test, please, see the attachments'
send_email()
Anyone has a hint of what is the problem?
msg?msgis a string, but I still don't understand the problem, when the script reads the txt file, is it no able to "attach" the content as a string?