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 protected]", "password")

msg = "\n"
server.sendmail("From", "To", msg)
1

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.