Created
November 1, 2016 07:30
-
-
Save knmkr/ca70b703e48e5eb2123465562a334bb1 to your computer and use it in GitHub Desktop.
Send mail via gmail
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import argparse | |
import smtplib | |
FROM = '[email protected]' | |
def _main(): | |
parser = argparse.ArgumentParser(description='') | |
parser.add_argument('--to', nargs='+') | |
parser.add_argument('--subject') | |
parser.add_argument('--body-txt', type=file) | |
args = parser.parse_args() | |
domain = 'smtp.gmail.com' | |
port = 465 | |
user = '[email protected]' | |
password = 'password' | |
try: | |
server_ssl = smtplib.SMTP_SSL(domain, port) | |
server_ssl.ehlo() | |
server_ssl.login(user, password) | |
to = args.to | |
subject = args.subject or '' | |
text = ''.join(args.body_txt.readlines()).strip() | |
message = '''From: %s\nTo: %s\nSubject: %s\n\n%s | |
''' % (FROM, ', '.join(to), subject, text) | |
server_ssl.sendmail(FROM, to, message) | |
server_ssl.close() | |
print message | |
print 'Successfully sent the mail' | |
except: | |
print 'Failed to send mail' | |
if __name__ == '__main__': | |
_main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment