-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsendmail.py
executable file
·102 lines (93 loc) · 3.51 KB
/
sendmail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Send email with attachment
"""
import smtplib
import os
import subprocess
import argparse
from email import encoders, utils
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import mimetypes
TEMP_DIR = "/tmp"
# localhostでSMTPサーバが稼働している場合
USE_TLS = False
SMTP_HOST = '127.0.0.1'
SMTP_PORT = 25
SMTP_USER = ''
SMTP_PASSWORD = ''
# gmailを利用する場合
#USE_TLS = True
#SMTP_HOST = 'smtp.gmail.com'
#SMTP_PORT = 587
#SMTP_USER = '[email protected]'
#SMTP_PASSWORD = 'password'
def attach_file(filename):
"添付ファイルのMIMEパートを作成"
data = open(filename, 'rb').read()
mimetype, mimeencoding = mimetypes.guess_type(filename)
if mimeencoding or (mimetype is None):
mimetype = 'application/octet-stream'
maintype, subtype = mimetype.split('/')
if maintype == 'text':
retval = MIMEText(data, _subtype=subtype)
else:
retval = MIMEBase(maintype, subtype)
retval.set_payload(data)
encoders.encode_base64(retval)
retval.add_header('Content-Disposition', 'inline', filename=os.path.basename(filename))
return retval
def tif2pdf(tif_file):
"TIFFファイルをPDFファイルに変換"
basename, _ = os.path.splitext(os.path.basename(tif_file))
pdf_file = os.path.join(TEMP_DIR, basename + '.pdf')
command = ['convert', tif_file, pdf_file]
proc = subprocess.Popen(command)
proc.communicate()
return pdf_file
def create_message(fromaddr, toaddr, ccaddr, subject, text, attachments):
"マルチパートMIMEメッセージを組み立てる"
msg = MIMEMultipart()
msg['To'] = toaddr
msg['From'] = fromaddr
if ccaddr:
msg['Cc'] = ccaddr
msg['Subject'] = subject
msg['Date'] = utils.formatdate(localtime=True)
msg['Message-ID'] = utils.make_msgid()
msg.attach(MIMEText(text, 'plain', 'utf-8'))
for attachment in attachments:
if not os.access(attachment, os.R_OK):
continue
mimetype, _ = mimetypes.guess_type(attachment)
if mimetype == "image/tiff":
attachment = tif2pdf(attachment)
msg.attach(attach_file(attachment))
return msg.as_string()
def sendmail(toaddr, fromaddr, ccaddr='', subject='', body='', attachment=[]):
"Eメールを送信する"
smtp = smtplib.SMTP(host=SMTP_HOST, port=SMTP_PORT)
if USE_TLS:
smtp.starttls()
smtp.login(SMTP_USER, SMTP_PASSWORD)
message = create_message(fromaddr, toaddr, ccaddr, subject,
body.decode('string-escape'), attachment)
smtp.sendmail(fromaddr, [toaddr, ccaddr], message)
smtp.close()
def main():
"コマンドライン解析"
par = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
par.add_argument('toaddr', help='destination address')
par.add_argument('-f', '--from', default='[email protected]', help='sender address', dest='fromaddr')
par.add_argument('-c', '--cc', default='', help='carbon copy address', dest='ccaddr')
par.add_argument('-s', '--subject', default='', help='subject of the email')
par.add_argument('-b', '--body', default='', help='content of the email')
par.add_argument('-a', '--attachment', nargs='*', default=[], help='attachment files')
par.add_argument('--version', action='version', version='%(prog)s 0.2')
args = vars(par.parse_args())
sendmail(**args)
if __name__ == '__main__':
main()