Created
May 14, 2013 04:04
-
-
Save peterldowns/5573613 to your computer and use it in GitHub Desktop.
Short function for sending messages using a Gmail account.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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