Hello,

I'm not a programmer, so please bear with me. I'm using the following script with Python 2.6 to send emails with attachments from the command line.

#!/usr/bin/python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
import sys

GMAIL_USER = 'username@gmail.com'
GMAIL_PASSWD = "password"
RECIPIENT = "backup@gmail.com"
SUBJECT = "Document backup: " + sys.argv[2]
MESSAGE = "A backup version of an OpenOffice.org document is attached."

def mail(to, subject, text, attach):
   msg = MIMEMultipart()

   msg['From'] = GMAIL_USER
   msg['To'] = to
   msg['Subject'] = subject

   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(GMAIL_USER, GMAIL_PASSWD)
   mailServer.sendmail(GMAIL_USER, to, msg.as_string())
   mailServer.close()

mail(RECIPIENT, SUBJECT, MESSAGE, sys.argv[1])

The problem is that this script doesn't work with Python 3.0. I'd be grateful if someone could help me to fix the script, so it works with Python 3.0.

Thank you!

Kind regards,
Dmitri

Any important reason you want to use Python30?
There have been quite a few changes beyond the import lines.

Python30 import lines are ...

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders as Encoders
import os
import sys

Thanks, vegaseat,

I fixed the import lines, but the script still doesn't work. Any suggestions?

Thank you!

Kind regards,
Dmitri

the script still doesn't work

What doesn't work? If you are getting error messages please paste the traceback here.

Sorry, my bad! Here is the traceback (I replaced sys.argv[1] in the script with the README.TXT for testing purposes) :

Traceback (most recent call last):
  File "C:\Python30\gmail_backup.py", line 41, in <module>
    mail(RECIPIENT, SUBJECT, MESSAGE, "README.TXT")
  File "C:\Python30\gmail_backup.py", line 37, in mail
    mailServer.login(GMAIL_USER, GMAIL_PASSWD)
  File "C:\Python30\lib\smtplib.py", line 580, in login
    AUTH_PLAIN + " " + encode_plain(user, password))
  File "C:\Python30\lib\smtplib.py", line 545, in encode_plain
    return encode_base64("\0%s\0%s" % (user, password))
  File "C:\Python30\lib\email\base64mime.py", line 96, in body_encode
    enc = b2a_base64(s[i:i + max_unencoded]).decode("ascii")
TypeError: b2a_base64() argument 1 must be bytes or buffer, not str

Thanks!

Any important reason you want to use Python30?

Python30 has made some significant changes, including the way it reads binary files. Before the data read was simply a string of characters representing the bytes, now it is a sequence of type <class 'bytes'> containing the actual values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.