Created
January 6, 2011 06:17
-
-
Save tokibito/767601 to your computer and use it in GitHub Desktop.
メール送信
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
# coding: utf-8 | |
from optparse import OptionParser | |
import smtplib | |
from email.MIMEText import MIMEText | |
from email.Utils import formatdate | |
ENCODING = 'iso-2022-jp' | |
class GMailConnection(object): | |
def __init__(self, username, password): | |
self.username = username | |
self.password = password | |
self.open() | |
def open(self): | |
self.smtp = smtplib.SMTP('smtp.gmail.com', 587) | |
self.smtp.ehlo() | |
self.smtp.starttls() | |
self.smtp.ehlo() | |
self.smtp.login(self.username, self.password) | |
def close(self): | |
self.smtp.close() | |
def sendmail(self, *args, **kwargs): | |
self.smtp.sendmail(*args, **kwargs) | |
def sendmail(connection, from_addr, to_addr, subject, body, cc=None, bcc=None): | |
msg = MIMEText(body.encode(ENCODING), 'plain', ENCODING) | |
msg['Subject'] = subject | |
msg['From'] = from_addr | |
msg['To'] = ', '.join(to_addr) | |
if cc: | |
msg['Cc'] = cc | |
msg['Date'] = formatdate() | |
connection.sendmail(from_addr, to_addr + (cc or []) + (bcc or []), msg.as_string()) | |
def main(): | |
parser = OptionParser() | |
options, args = parser.parse_args() | |
conn = GMailConnection(args[0], args[1]) | |
sendmail(conn, args[0], ['[email protected]'], u'テスト送信', u'テスト送信の内容', bcc=['[email protected]']) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment