Skip to content

Instantly share code, notes, and snippets.

@noexpect
Last active December 27, 2015 08:48
Show Gist options
  • Save noexpect/7298623 to your computer and use it in GitHub Desktop.
Save noexpect/7298623 to your computer and use it in GitHub Desktop.
pythonでGmail経由でメール送信。
import smtplib
from email.mime.text import MIMEText
class sendGmail:
username, password = '[email protected]', 'password'
def __init__(self, to, sub, body):
host, port = 'smtp.gmail.com', 465
msg = MIMEText(body)
msg['Subject'] = sub
msg['From'] = self.username
msg['To'] = to
smtp = smtplib.SMTP_SSL(host, port)
smtp.ehlo()
smtp.login(self.username, self.password)
smtp.mail(self.username)
smtp.rcpt(to)
smtp.data(msg.as_string())
smtp.quit()
if __name__ == '__main__':
to = '[email protected]'
sub = 'python smtplib'
body = 'hello world'
sendGmail(to, sub, body)
@noexpect
Copy link
Author

noexpect commented Nov 4, 2013

ここらを参考に。

つ「Gmailからpython送信。 以下を参考に「PythonのsmtplibでGmailからメールを送信する - yattの日記」http://d.hatena.ne.jp/yatt/20110730/1312009712」https://gist.github.com/noexpect/7298623

つ「メール送信 - python入門から応用までの学習サイト」http://www.python-izm.com/contents/application/mail_send.shtml

@noexpect
Copy link
Author

noexpect commented Nov 4, 2013

冗長だったし、拡張する予定ないのでコンストラクタに処理突っ込んだ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment