Skip to content

Instantly share code, notes, and snippets.

@yatt
Created December 16, 2014 15:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yatt/e001013140652e0c5b27 to your computer and use it in GitHub Desktop.
Save yatt/e001013140652e0c5b27 to your computer and use it in GitHub Desktop.
Python2.7でGMailから自身にメールを送信、日本語もOK.
#! /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