-4

So I'm trying to send a .txt file as an attachment and I can't find the right code to work. Here is my code:

import pythoncom
import win32gui
import win32console
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


fromaddr = 'zover1@gmail.com'
toaddrs = 'zover2@gmail.com'
msg = "contrl text file"
username = 'zover1@gmail.com'
password= 'xxxxxxxxxxxx'
server = smtplib.SMTP('smtp.gmail.com:587')
f = file("d:/control.txt")
attachment = MIMEText(f.read())
attachment.add_header('Content-Disposition', 'attachment', filename="d:/control.txt")
msg.attach(attachment)
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

And when I run the module I get this error:

Traceback (most recent call last):
  File "C:\Python278\emailonlytester.pyw", line 19, in <module>
msg.attach(attachment)
AttributeError: 'str' object has no attribute 'attach'

Any help would be much appreciated.

ρss
  • 5,115
  • 8
  • 43
  • 73
Zover Lul
  • 1
  • 1
  • 2

2 Answers2

1

You can try this to send an attached file with python:

msg = MIMEMultipart()
msg['From'] = 'your adress'
msg['To'] = 'someone'
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = 'a random subject'
msg.attach(MIMEText("some text"))
file = 'd:/control.txt'
attachment = MIMEBase('application', 'octet-stream')
attachment.set_payload(open(file,'rb').read())
encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
msg.attach(attachment)

This is the part for the creation of the Email, not the sending.

Oscar
  • 231
  • 4
  • 17
  • I pasted the above code but I'm getting an error like this: Traceback (most recent call last): File "C:\Python278\emailonlytester.pyw", line 26, in server.ehlo() NameError: name 'server' is not defined don't see how its not defined, i have smtplib imported. It worked when I wasn't sending an attachment – Zover Lul Dec 23 '14 at 14:18
  • The code I provided does not contain any reference to `server`, can you show me how you implemented this piece of code in yours ? – Oscar Dec 23 '14 at 15:00
  • file = 'd:/control.txt' attachment = MIMEBase('application', 'octet-stream') attachment.set_payload(open(file,'rb').read()) attachment.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file)) msg.attach(attachment) username = 'zover.lul@gmail.com' password= 'battlefield34' msg = "contrl text file" server.ehlo() server.starttls() server.login(username, password) server.sendmail(fromaddr, toaddrs, msg) server.quit() – Zover Lul Dec 23 '14 at 18:58
  • Well from what you gave me here, there is no server define, if this is all the code you are using, no wonder it is not working. You should try to reuse some old parts of the code you gave us in the first place. – Oscar Dec 24 '14 at 09:06
0

The error is clearly stating the reason. msg is a string in your case. You may want to do the following instead:

msg = MIMEMultipart()

Docs are here.

ρss
  • 5,115
  • 8
  • 43
  • 73