{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Gmailの送信"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ライブラリなどのインポート"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"import smtplib\n",
"import ssl\n",
"from email.mime.text import MIMEText"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys, codecs\n",
"sys.stdout = codecs.getwriter(\"utf-8\")(sys.stdout)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Gmailの送信"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gmail_account = \"kinocode.contact@gmail.com\"\n",
"gmail_password = \"\" \n",
"send_name = \"キノコード\"\n",
"mail_to = \"kino.code.project@gmail.com\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"today_date = datetime.date.today()\n",
"delivery_date = today_date + datetime.timedelta(days=7)\n",
"print(today_date,delivery_date)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"subject = \"{0}様、{1}分の発注書をお送りします。\".format(send_name,today_date)\n",
"body = \"表題の発注書をお送りいたします。
添付ファイルをご確認ください。
本発注の納期は{0}となります。
□■株式会社キノコード■□\".format(delivery_date)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(subject)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(body)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"msg = MIMEText(body, \"html\")\n",
"print(msg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"msg[\"Subject\"] = subject\n",
"msg[\"To\"] = mail_to\n",
"msg[\"From\"] = gmail_account\n",
"print(msg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"server = smtplib.SMTP_SSL(\"smtp.gmail.com\", 465,context=ssl.create_default_context())\n",
"server.login(gmail_account, gmail_password)\n",
"server.send_message(msg)\n",
"server.close()\n",
"print('送信完了')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 添付資料付きGmail送信"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from email.mime.multipart import MIMEMultipart\n",
"from email.mime.base import MIMEBase\n",
"from email import encoders"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"subject = \"{0}様、{1}分の発注書をお送りします。\".format(send_name,today_date)\n",
"body = \"表題の発注書をお送りいたします。
添付ファイルをご確認ください。
本発注の納期は{0}となります。
□■株式会社キノコード■□\".format(delivery_date)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"msg = MIMEMultipart()\n",
"\n",
"msg['Subject'] = subject\n",
"msg['To'] = mail_to\n",
"msg['From'] = gmail_account\n",
"msg_body = MIMEText(body, \"html\")\n",
"\n",
"msg.attach(msg_body)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"filename = \"order_a.pdf\"\n",
"file = open(filename, \"rb\")\n",
"\n",
"attachment_file = MIMEBase('application', 'pdf')\n",
"attachment_file.set_payload((file).read())\n",
"file.close()\n",
"\n",
"encoders.encode_base64(attachment_file)\n",
"attachment_file.add_header('Content-Disposition', \"attachment\", filename=filename)\n",
"msg.attach(attachment_file)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"server = smtplib.SMTP_SSL(\"smtp.gmail.com\", 465, context=ssl.create_default_context())\n",
"server.login(gmail_account, gmail_password)\n",
"server.send_message(msg)\n",
"print('送信完了')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 複数の宛先に一斉送信をする"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_excel('mailing_list.xlsx')\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for send_name, mail_to,filename in zip(df['宛名'], df['メールアドレス'], df['添付ファイル']):\n",
" print(send_name, mail_to,filename)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def gmail_send(send_name, mail_to,filename):\n",
" \n",
" subject = \"{0}様、{1}分の発注書をお送りします。\".format(send_name,today_date)\n",
" body = '''表題の発注書をお送りいたします。
\n",
" 添付ファイルをご確認ください。
\n",
" 本発注の納期は{0}となります。
\n",
" □■株式会社キノコード■□'''.format(delivery_date)\n",
" \n",
" msg = MIMEMultipart()\n",
"\n",
" msg['Subject'] = subject\n",
" msg['To'] = mail_to\n",
" msg['From'] = gmail_account\n",
" msg_body = MIMEText(body, \"html\")\n",
"\n",
" msg.attach(msg_body)\n",
"\n",
" filename = filename\n",
" file = open(filename, \"rb\")\n",
"\n",
" attachment_file = MIMEBase('application', 'pdf')\n",
" attachment_file.set_payload((file).read())\n",
" file.close()\n",
"\n",
" encoders.encode_base64(attachment_file)\n",
" attachment_file.add_header('Content-Disposition', \"attachment\", filename=filename)\n",
" msg.attach(attachment_file)\n",
" \n",
" server = smtplib.SMTP_SSL(\"smtp.gmail.com\", 465, context=ssl.create_default_context())\n",
" server.login(gmail_account, gmail_password)\n",
" server.send_message(msg) \n",
" server.close()\n",
" print(send_name,'様:送信完了')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for send_name, mail_to,filename in zip(df['宛名'], df['メールアドレス'], df['添付ファイル']):\n",
" gmail_send(send_name, mail_to,filename)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}