0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python3: メールの送信 (添付ファイル付き)

Last updated at Posted at 2021-04-17

こちらのプログラムを改造して、添付ファイルを送るようにしました。
Python3: Starttls でメールの送信
attachements/linux_logo.gif というファイルを添付するサンプルです。

coreserver_attache.py
# ! /usr/bin/python3
# -*- coding: utf-8 -*-
#
#	coreserver_attache.py
#
#						Apr/17/2021	
# --------------------------------------------------------------------
import	os
import	sys
import	socket
from dotenv import load_dotenv
import	email
from email.mime.text import MIMEText
from email.utils import formatdate
from email.message import EmailMessage
import imghdr
# --------------------------------------------------------------------
from send_starttls import send_starttls_proc
# --------------------------------------------------------------------
def attach_image_proc(message,img_data,filename):
    try:
        message.add_attachment(img_data,maintype='image',
            subtype=imghdr.what(None,img_data),filename=filename)
    except Exception as ee:
        sys.stderr.write("*** error *** in message.add_attachment ***\n")
        sys.stderr.write(str(ee) + "\n")
#
    return message
# --------------------------------------------------------------------
def create_message_proc(mail,file_attach):
	message = EmailMessage()
	contents = 'Hello, Good Afternoon! on Apr/17/2021 PM 20:21\n'
	contents += 'Apr/17/2021 PM 20:03\n'
	contents += 'こんにちは。\n'
	contents += 'python3\n'
	contents += ' ' + socket.gethostname ()

	message.set_content(contents)
	message['Subject'] = 'Hello Apr/17/2021 PM 20:21'
	message['From'] = mail['from']
	message['To'] = mail['to']
	message['Date'] = formatdate ()
#
	fp = open(file_attach,'rb')
	img_data = fp.read()
	fp.close()
#
	for suffix in [".jpg",".png",".bmp","gif"]:
		if (file_attach.endswith(suffix)):
			message = attach_image_proc(message,img_data,file_attach)
#
	return message
# --------------------------------------------------------------------
sys.stderr.write ("*** 開始 ***\n")
dotenv_path = '.env'
load_dotenv(dotenv_path)
mail = {}
mail['server'] = os.environ.get("SERVER")
mail['port'] = os.environ.get("PORT")
mail['usr'] = os.environ.get("USR")
mail['password'] = os.environ.get("PASSWORD")
mail['from'] = os.environ.get("FROM")
mail['to'] = os.environ.get("TO")

file_attache = "./attachements/linux_logo.gif"

message = create_message_proc(mail,file_attache)

send_starttls_proc (mail,message)
#
sys.stderr.write ("*** 終了 ***\n")
# --------------------------------------------------------------------

次のファイルはこちらです。
Python3: Starttls でメールの送信

send_starttls.py
.env
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?