Last active
December 27, 2015 08:48
-
-
Save noexpect/7298623 to your computer and use it in GitHub Desktop.
pythonで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
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) |
冗長だったし、拡張する予定ないのでコンストラクタに処理突っ込んだ
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ここらを参考に。
つ「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