LoginSignup
10
12

More than 5 years have passed since last update.

Gmailに添付ファイルをつけて、メールを送信する

Last updated at Posted at 2016-07-09

仕事の合間に、勉強がてらに、PythonでGmialからメール送信。

http://d.hatena.ne.jp/kenpy/20101123/1290499799
参考にしたのはこちら。というかほぼ写経です。

#!/Usr/bin/env python                                                                                  
# -*- coding: utf-8 -*-  

import os.path
import datetime
import smtplib

from email import Encoders
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

def create_message(from_addr, to_addr, subject, body, mine, attach_file):
    """
    Mailのメッセージを構築する
    """
    msg = MIMEMultipart()
    msg["Subject"] = subject
    msg["From"] = from_addr
    msg["To"] = to_addr


    body = MIMEText(body)
    msg.attach(body)

    #添付ファイルのMIMEタイプを指定する
    attachment = MIMEBase(mine['type'],mine['subtype'])

    file = open(attach_file['path'])
    attachment.set_payload(file.read())
    file.close()
    Encoders.encode_base64(attachment)
    msg.attach(attachment)
    attachment.add_header("Content-Dispositon","attachment",filename=attach_file['name'])

    return msg

def sendGmail(from_addr, to_addr, msg):
    """
    mailを送信する
    """
    smtp = smtplib.SMTP_SSL(host, port)
    smtp.ehlo()
    smtp.login(username, password)
    smtp.sendmail(from_addr, to_addr, msg.as_string())
    smtp.quit()


if __name__ == '__main__':

    #gmail用に追加
    host, port = 'smtp.gmail.com', XXX
    username, password = 'hogehoge@gmail.com', 'hogehoge'

#実際に使用されているアドレスだと困るので、コメントアウトしてます。

    from_addr = #"hogehoge@gmail.com”
    to_addr = #"hogehoge@gmail.com"
    subject = "ファイル添付"
    body = "test body"
    mine={'type':'text','subtype':'comma-separated-values'}

    attach_file={'name':'test.csv','path':'/tmp/test.csv'}
    msg = create_message(from_addr, to_addr, subject, body, mine, attach_file)
    sendGmail(from_addr, to_addr, msg)
10
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
12