Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from time import sleep
import signal
import sys
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.Header import Header
from email.Utils import formatdate
import smtplib
import os
from_addr="example_Tx@gmail.com" #送信メールアドレス
to_addr ="example_Rx@gmail.com" #受信メールアドレス
subject = u'スマート冷蔵庫からのメールです' #メールの件名
body = u'\n%s\n' % (u'冷蔵庫の中の写真になります') #メールの本文の文章
jpegs = ['mail_test.jpg'] #添付する画像
server='smtp.gmail.com'
port=587
encoding='utf-8'
msg = MIMEMultipart()
mt = MIMEText(body.encode(encoding), 'plain', encoding)
if jpegs:
for fn in jpegs:
img = open(fn, 'rb').read()
mj = MIMEImage(img, 'jpeg', filename=fn)
mj.add_header("Content-Disposition", "attachment", filename=fn)
msg.attach(mj)
msg.attach(mt)
else:
msg = mt
msg['Subject'] = Header(subject, encoding)
msg['From'] = from_addr
msg['To'] = to_addr
msg['Date'] = formatdate()
#送信する側のメールアドレスとgoogleアカウントのパスワード
_user = "example_Tx@gmail.com"
_pass = "password"
smtp = smtplib.SMTP(server, port)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(_user, _pass)
smtp.sendmail(from_addr, [to_addr], msg.as_string())
smtp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment