Created
December 16, 2014 15:06
-
-
Save yatt/e001013140652e0c5b27 to your computer and use it in GitHub Desktop.
Python2.7でGMailから自身にメールを送信、日本語もOK.
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
#! /usr/bin/python2.7 | |
# coding: utf-8 | |
""" | |
stdinの内容をGMailで | |
自分自身に送る | |
""" | |
import smtplib | |
host, port = 'smtp.gmail.com', 465 | |
username, password = 'XXXXXXXXXXXXXXXXXXXXX@gmail.com', 'YYYYYYYYYYYYYYYYYYYYY' | |
import sys | |
import os | |
content = sys.stdin.read() | |
# unicodization | |
for enc in ['utf-8', 'cp932', 'euc-jp']: | |
try: | |
content = unicode(content, enc) | |
except: pass | |
subject = content.splitlines()[0].strip() | |
body = content | |
# build mail message | |
message = u"""From: %s | |
To: %s | |
Subject: %s | |
%s | |
""" % (username, username, subject, body) | |
# normalize newline character to CRLF | |
message = message.replace('\r\n', '\n') | |
message = message.replace('\r', '\n') | |
message = message.replace('\n', '\r\n') | |
message = message.encode('utf-8') | |
smtp = smtplib.SMTP_SSL(host, port) | |
smtp.ehlo() | |
smtp.login(username, password) | |
smtp.sendmail(username, username, message) | |
smtp.quit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment