Send email with python

This is a simple snippets for sending an email with a gmail smtp server.

If you want to send emails from localhost, you will have to first enable an smtp server.

So, the simplest is to use gmail smtp server:

import smtplib

# define the method
def sendemail(from_addr, to_addr_list, cc_addr_list,
              subject, message,
              login, password,
              smtpserver='smtp.gmail.com:587'):
    header  = 'From: %s\n' % from_addr
    header += 'To: %s\n' % ','.join(to_addr_list)
    header += 'Cc: %s\n' % ','.join(cc_addr_list)
    header += 'Subject: %s\n\n' % subject
    message = header + message

    server = smtplib.SMTP(smtpserver)
    server.starttls()
    server.login(login,password)
    problems = server.sendmail(from_addr, to_addr_list, message)
    server.quit()

#execute the method
sendemail('me@domain.com', ['you@yourdomain.com'], ['andyou@yourdomain.com'],
          'test subject', 'test message', 'mylogin@gmail.com', 'mypassword')

Source:

Comments

Comments powered by Disqus