Skip to content

Instantly share code, notes, and snippets.

@peterldowns
Created May 14, 2013 04:04
Show Gist options
  • Save peterldowns/5573613 to your computer and use it in GitHub Desktop.
Save peterldowns/5573613 to your computer and use it in GitHub Desktop.
Short function for sending messages using a Gmail account.
def gmail(recipients, subject, message, sender_username, sender_password):
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username, password)
if not (isinstance(recipients, list) or isinstance(recipients, tuple)):
recipients = [recipients]
gmail_sender = '{0}@gmail.com'.format(username)
message_formatstr = 'To: {0}\nFrom: {1}\nSubject: {2}\n\n{3}\n\n'
for recipient in recipients:
server.sendmail(gmail_sender,
recipient,
message_formatstr.format(recipient, gmail_sender, subject, message))
server.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment