1
0

More than 3 years have passed since last update.

メール を指定した時間で定型分を送信するプログラム by Python

Posted at

モジュールをインポートする。

import datetime
import smtplib
import ssl
from email.mime.text import MIMEText
import sys, codecs

sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
gmail_account = "" #→自分のメールアドレスを入れてください。
gmail_password = "" #→自分のメールアドレスパスワードを入れてください。
gmail to = "" #→送信したいメールアドレスを入れてください。
send_name = "ぷーやん"

メールを送る期間を決める。

today_date = datetime.date.today()
delivery_date = today_date + datetime.timedelta(days=7)
print(today_date,delivery_date)

メールデータ(MIME)の作成

subject = "{0}様、{1}分の発注書をお送りします。". format(send_name, today_date)
body = "表題の発注書をお送りします。
添付ファイルをご確認ください。
本発注の納期は{0}となります。

株式会社ぷーやん".format(delivery_date)

print(subject) #確認用

print(body) #確認用

msg = MIMEText(body, "html")
print(msg)

msg["Subject"] = subject
msg["To"] = mail_to
msg["From "] = gmail_account
print(msg)

server = smtplib.SMTP_SSL("smtp.gmail.com", 465,context=ssl.create_default_context())
server.login(gmail_account, gmail_password)
server.send_message(msg)
server.close()
print('送信完了')

1
0
1

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
1
0