0

This is my code and it work's but i would like to send .txt files along with it more than one about 4. How would i go about doing this? Thank you!

import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)

server.login("Email@gamil.com", "password")

msg = "\n"
server.sendmail("From", "To", msg)
K.Church
  • 11
  • 2
  • see also: http://stackoverflow.com/questions/11921188/how-to-send-email-with-pdf-attachment-in-python – Paul May 29 '14 at 06:57

1 Answers1

0

I use this for sending mails with attachments using gmail:

def send_mail(send_from, send_to, files=[]):

        msg = MIMEMultipart()
        msg['From'] = send_from
        msg['To'] = send_to
        msg['Date'] = formatdate(localtime=True)
        if len(string_files)==1:
            msg['Subject'] = 'Sending file: %s' % files[0]
        else:
            msg['Subject'] = 'Sending file: %s and others' % files[0]
        text='This message has followng attachments'
        for filename in string_files:
            text+='\n%s' % filename
        msg.attach( MIMEText(text) )

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

        smtp = smtplib.SMTP('smtp.gmail.com:587')
        smtp.starttls()

        # if you store password in a keyring (like gnome-keyring)
        import keyring
        gmail_password = keyring.get_password('gmail', 'personal')
        # or just gmail_password='password'
        smtp.login('yourgmailaddress', gmail_password)
        smtp.sendmail(send_from, send_to, msg.as_string())
        smtp.close()
yemu
  • 26,249
  • 10
  • 32
  • 29