-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmailto
More file actions
executable file
·190 lines (151 loc) · 4.84 KB
/
mailto
File metadata and controls
executable file
·190 lines (151 loc) · 4.84 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/python
#
# $Id: mailto,v 1.6 2011/01/07 09:46:02 gurubert Exp $
#
#
# This is a mailto backend for CUPS (www.cups.org)
#
# (C) 2011 Robert Sander <robert.sander@epigenomics.com>
#
# Released under GPL
#
# NO WARRANTY AT ALL
#
import sys, os, re, shutil, 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))
# added by jm
def get_pdf_content(path):
pdf_content = os.popen("pdftotext -layout \"%s\" -" % path ).read().rstrip()
return pdf_content
# get's rid of smbprn.00000237
# in smbprn.00000237 Microsoft Word - document2012-10-02.docx
# NOT NEEDED
#def trim_smbprn(fname):
# trimmed = re.sub(r'smbprn\.\d+\s', '', fname)
# return trimmed
argc = len(sys.argv)
syslog.openlog('mailto', syslog.LOG_PID, syslog.LOG_LPR)
if argc == 1:
print "network mailto \"Unknown\" \"SMTP\""
syslog.syslog(syslog.LOG_INFO, "network mailto \"Unknown\" \"SMTP\"")
syslog.closelog()
sys.exit(0)
writelog("INFO: mailto 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(".mailto")
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)
# this gets the formatted text to put in the body of the email
contents = get_pdf_content(infilename)
# make a backup copy of last infile for debugging
shutil.copyfile(infilename, "/tmp/mailto_last_printed.pdf")
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)
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("%s\n\nYou printed %s with jobid %s\n%s" % (contents, 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))
msg.attach(att)
os.popen("%s -t -f%s" % (sendmail, mailfrom), "w").write(msg.as_string())
writelog("INFO: sent email to %s\n" % mailto)
infile.close()
if argc == 6:
os.unlink(infilename)
writelog("INFO: Ready to print.\n")
syslog.closelog()
sys.exit(0)