This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mailprinter
173 lines (138 loc) · 4.41 KB
/
mailprinter
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/python
#
# $Id: mailprinter,v 0.1 2016/08/27 09:46:02 ilmaisin Exp $
#
#
# This is a mailto backend for CUPS (www.cups.org)
#
# (C) 2016 Iiro Laiho <[email protected]>
# (C) 2011 Robert Sander <[email protected]>
#
# Released under GPL
#
# NO WARRANTY AT ALL
#
import sys, os, tempfile, mimetypes, syslog
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
log = "\nLog:\n"
file = "/usr/bin/file"
sendmail = "/usr/sbin/sendmail"
def writelog(message):
global log
syslog.syslog(syslog.LOG_INFO, message)
sys.stderr.write(message)
if log:
log += message
def guess_type(datafile):
type, enc = mimetypes.guess_type(datafile)
if (not type) or (type == 'application/octet-stream'):
enc = None
try:
type = os.popen("%s -b --mime-type %s 2> /dev/null" % (file, datafile)).read().rstrip()
if type == "" or type == "data":
type = "application/octet-stream"
except:
type = "application/octet-stream"
return (type, enc)
def guess_extension(mimetype):
return(mimetypes.guess_extension(mimetype))
argc = len(sys.argv)
syslog.openlog('mailprinter', syslog.LOG_PID, syslog.LOG_LPR)
if argc == 1:
print "network mailprinter:/ \"ilmaisin cupsmailprinter PDF converter\" \"Send as email\""
syslog.syslog(syslog.LOG_INFO, "network mailprinter:/ \"cupsmailprinter PDF converter\" \"Send as email\"")
syslog.closelog()
sys.exit(0)
writelog("INFO: mailprinter argv[%s] = '%s'\n" % (argc, "', '".join(sys.argv[1:])))
if argc < 6 or argc > 7:
writelog("ERROR: %s job-id user title copies options [file]\n" % sys.argv[0])
sys.exit(1)
jobid = sys.argv[1]
user = sys.argv[2]
title = sys.argv[3]
if title[:7] == "smbprn.":
title = title[16:]
opts = sys.argv[5].split(" ")
if argc == 7:
writelog("INFO: file is %s\n" % sys.argv[6])
infilename = sys.argv[6]
else:
infilename = tempfile.mktemp(".mailprinter")
try:
infile = open(infilename, "w")
except:
writelog("ERROR: unable to create tmp file %s\n" % infilename)
sys.exit(1)
writelog("INFO: file is stdin\n")
try:
infile.write(sys.stdin.read())
except:
writelog("ERROR: unable to copy into tmpfile\n")
sys.exit(1)
infile.close()
writelog("INFO: copied stdin to %s\n" % infilename)
infile = open(infilename, "rb")
mailto = None
mailfrom = None
for opt in opts:
if opt[:7] == "mailto=":
mailto = opt[7:]
writelog("INFO: mailto %s\n" % mailto)
if opt[:9] == "mailfrom=":
mailfrom = opt[9:]
writelog("INFO: mailfrom %s\n" % mailfrom)
if not mailfrom:
if user:
mailfrom = user
else:
mailfrom = "lp"
if not mailto:
if user:
mailto = user
else:
mailto = mailfrom
msg = MIMEMultipart()
# Essential lines to put into the header of a MIME mail.
msg["From"] = mailfrom
msg["To"] = mailto
msg["Subject"] = Header("%s printed as PDF" % title, "utf-8")
if mailto != user and mailfrom != user:
msg["X-CUPS-mailto-started-by"] = user
msg.preamble = "This is an automatically generated email.\n"
type, enc = guess_type(infilename)
ext = guess_extension(type)
if not ext:
ext = ""
maintype, subtype = type.split('/', 1)
writelog("INFO: type='%s' enc='%s' ext='%s'\n" % (type, enc, ext))
txt = MIMEText("You printed %s with jobid %s\n%s" % (title, jobid, log))
msg.attach(txt)
if maintype == 'application':
from email.mime.application import MIMEApplication
att = MIMEApplication(infile.read(), _subtype=subtype)
elif maintype == 'text':
att = MIMEText(infile.read(), _subtype=subtype)
elif maintype == 'image':
from email.mime.image import MIMEImage
att = MIMEImage(infile.read(), _subtype=subtype)
elif maintype == 'audio':
from email.mime.audio import MIMEAudio
att = MIMEAudio(infile.read(), _subtype=subtype)
else:
from email import encoders
from email.mime.base import MIMEBase
att = MIMEBase(maintype, subtype)
att.set_payload(infile.read())
encoders.encode_base64(att)
att.add_header('Content-Disposition', 'attachment', filename="%s%s" % (title, ext))
infile.close()
msg.attach(att)
os.popen("%s -t -f%s" % (sendmail, mailfrom), "w").write(msg.as_string())
writelog("INFO: sent email to %s\n" % mailto)
if argc == 6:
os.unlink(infilename)
writelog("INFO: Ready to print.\n")
syslog.closelog()
sys.exit(0)