Mercurial > kallithea
annotate rhodecode/lib/smtp_mailer.py @ 1639:95c3e33ef32e
merged upto rev 019026a8cf67
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 02 Nov 2011 22:19:47 +0200 |
parents | 752b0a7b7679 8e77c75bd65a |
children |
rev | line source |
---|---|
981
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
1 # -*- coding: utf-8 -*- |
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
2 """ |
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
3 rhodecode.lib.smtp_mailer |
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~~ |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1169
diff
changeset
|
5 |
981
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
6 Simple smtp mailer used in RhodeCode |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1169
diff
changeset
|
7 |
984
ccd323bfa121
Added limit option for revision ranges
Marcin Kuzminski <marcin@python-works.com>
parents:
981
diff
changeset
|
8 :created_on: Sep 13, 2010 |
1532
2afe9320d5e6
updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents:
1417
diff
changeset
|
9 :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> |
2afe9320d5e6
updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents:
1417
diff
changeset
|
10 :license: GPLv3, see COPYING for more details. |
981
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
11 """ |
1532
2afe9320d5e6
updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents:
1417
diff
changeset
|
12 # This program is free software: you can redistribute it and/or modify |
2afe9320d5e6
updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents:
1417
diff
changeset
|
13 # it under the terms of the GNU General Public License as published by |
2afe9320d5e6
updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents:
1417
diff
changeset
|
14 # the Free Software Foundation, either version 3 of the License, or |
2afe9320d5e6
updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents:
1417
diff
changeset
|
15 # (at your option) any later version. |
2afe9320d5e6
updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents:
1417
diff
changeset
|
16 # |
2afe9320d5e6
updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents:
1417
diff
changeset
|
17 # This program is distributed in the hope that it will be useful, |
2afe9320d5e6
updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents:
1417
diff
changeset
|
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
2afe9320d5e6
updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents:
1417
diff
changeset
|
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2afe9320d5e6
updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents:
1417
diff
changeset
|
20 # GNU General Public License for more details. |
2afe9320d5e6
updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents:
1417
diff
changeset
|
21 # |
2afe9320d5e6
updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents:
1417
diff
changeset
|
22 # You should have received a copy of the GNU General Public License |
2afe9320d5e6
updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents:
1417
diff
changeset
|
23 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
981
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
24 |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
25 import logging |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
26 import smtplib |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
27 import mimetypes |
981
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
28 from socket import sslerror |
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
29 |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
30 from email.mime.multipart import MIMEMultipart |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
31 from email.mime.image import MIMEImage |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
32 from email.mime.audio import MIMEAudio |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 from email.mime.base import MIMEBase |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
34 from email.mime.text import MIMEText |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
35 from email.utils import formatdate |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
36 from email import encoders |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 |
1307 | 38 |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
39 class SmtpMailer(object): |
981
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
40 """SMTP mailer class |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1169
diff
changeset
|
41 |
1583
8e77c75bd65a
made auth type optional in constructor
Marcin Kuzminski <marcin@python-works.com>
parents:
1581
diff
changeset
|
42 mailer = SmtpMailer(mail_from, user, passwd, mail_server, smtp_auth |
1307 | 43 mail_port, ssl, tls) |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1169
diff
changeset
|
44 mailer.send(recipients, subject, body, attachment_files) |
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1169
diff
changeset
|
45 |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
46 :param recipients might be a list of string or single string |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1169
diff
changeset
|
47 :param attachment_files is a dict of {filename:location} |
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1169
diff
changeset
|
48 it tries to guess the mimetype and attach the file |
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1169
diff
changeset
|
49 |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 """ |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 |
1583
8e77c75bd65a
made auth type optional in constructor
Marcin Kuzminski <marcin@python-works.com>
parents:
1581
diff
changeset
|
52 def __init__(self, mail_from, user, passwd, mail_server, smtp_auth=None, |
8e77c75bd65a
made auth type optional in constructor
Marcin Kuzminski <marcin@python-works.com>
parents:
1581
diff
changeset
|
53 mail_port=None, ssl=False, tls=False, debug=False): |
689
ecc566f8b69f
fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents:
604
diff
changeset
|
54 |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
55 self.mail_from = mail_from |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
56 self.mail_server = mail_server |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
57 self.mail_port = mail_port |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
58 self.user = user |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
59 self.passwd = passwd |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 self.ssl = ssl |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
61 self.tls = tls |
1169
f6dca275c5a8
control mailer debug with the .ini file
Marcin Kuzminski <marcin@python-works.com>
parents:
1029
diff
changeset
|
62 self.debug = debug |
1581
67377fd685be
applied smth_auth options update patch
Les Peabody <lpeabody@ccom.unh.edu>
parents:
1532
diff
changeset
|
63 self.auth = smtp_auth |
689
ecc566f8b69f
fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents:
604
diff
changeset
|
64 |
1029
3bf8898ce66d
removed immutable default argument from mailer
Marcin Kuzminski <marcin@python-works.com>
parents:
984
diff
changeset
|
65 def send(self, recipients=[], subject='', body='', attachment_files=None): |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
66 |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
67 if isinstance(recipients, basestring): |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
68 recipients = [recipients] |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
69 if self.ssl: |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
70 smtp_serv = smtplib.SMTP_SSL(self.mail_server, self.mail_port) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
71 else: |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
72 smtp_serv = smtplib.SMTP(self.mail_server, self.mail_port) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
73 |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
74 if self.tls: |
981
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
75 smtp_serv.ehlo() |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 smtp_serv.starttls() |
689
ecc566f8b69f
fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents:
604
diff
changeset
|
77 |
ecc566f8b69f
fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents:
604
diff
changeset
|
78 if self.debug: |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
79 smtp_serv.set_debuglevel(1) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
80 |
952
beabd452f08b
fixed ehlo command for mailing.
Marcin Kuzminski <marcin@python-works.com>
parents:
689
diff
changeset
|
81 smtp_serv.ehlo() |
1581
67377fd685be
applied smth_auth options update patch
Les Peabody <lpeabody@ccom.unh.edu>
parents:
1532
diff
changeset
|
82 if self.auth: |
67377fd685be
applied smth_auth options update patch
Les Peabody <lpeabody@ccom.unh.edu>
parents:
1532
diff
changeset
|
83 smtp_serv.esmtp_features["auth"] = self.auth |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
84 |
1583
8e77c75bd65a
made auth type optional in constructor
Marcin Kuzminski <marcin@python-works.com>
parents:
1581
diff
changeset
|
85 # if server requires authorization you must provide login and password |
8e77c75bd65a
made auth type optional in constructor
Marcin Kuzminski <marcin@python-works.com>
parents:
1581
diff
changeset
|
86 # but only if we have them |
981
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
87 if self.user and self.passwd: |
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
88 smtp_serv.login(self.user, self.passwd) |
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
89 |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
90 date_ = formatdate(localtime=True) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
91 msg = MIMEMultipart() |
1417
5875955def39
fixes #223 improve password reset form
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
92 msg.set_type('multipart/alternative') |
5875955def39
fixes #223 improve password reset form
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
93 msg.preamble = 'You will not see this in a MIME-aware mail reader.\n' |
5875955def39
fixes #223 improve password reset form
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
94 |
5875955def39
fixes #223 improve password reset form
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
95 text_msg = MIMEText(body) |
5875955def39
fixes #223 improve password reset form
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
96 text_msg.set_type('text/plain') |
5875955def39
fixes #223 improve password reset form
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
97 text_msg.set_param('charset', 'UTF-8') |
5875955def39
fixes #223 improve password reset form
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
98 |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
99 msg['From'] = self.mail_from |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
100 msg['To'] = ','.join(recipients) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
101 msg['Date'] = date_ |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
102 msg['Subject'] = subject |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
103 |
1417
5875955def39
fixes #223 improve password reset form
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
104 msg.attach(text_msg) |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
105 |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
106 if attachment_files: |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
107 self.__atach_files(msg, attachment_files) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
108 |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
109 smtp_serv.sendmail(self.mail_from, recipients, msg.as_string()) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
110 logging.info('MAIL SEND TO: %s' % recipients) |
981
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
111 |
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
112 try: |
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
113 smtp_serv.quit() |
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
114 except sslerror: |
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
115 # sslerror is raised in tls connections on closing sometimes |
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
116 pass |
c45c910714e4
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents:
952
diff
changeset
|
117 |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
118 def __atach_files(self, msg, attachment_files): |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 if isinstance(attachment_files, dict): |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 for f_name, msg_file in attachment_files.items(): |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
121 ctype, encoding = mimetypes.guess_type(f_name) |
1307 | 122 logging.info("guessing file %s type based on %s", ctype, |
123 f_name) | |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
124 if ctype is None or encoding is not None: |
1307 | 125 # No guess could be made, or the file is encoded |
126 # (compressed), so use a generic bag-of-bits type. | |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
127 ctype = 'application/octet-stream' |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
128 maintype, subtype = ctype.split('/', 1) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
129 if maintype == 'text': |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
130 # Note: we should handle calculating the charset |
689
ecc566f8b69f
fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents:
604
diff
changeset
|
131 file_part = MIMEText(self.get_content(msg_file), |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
132 _subtype=subtype) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
133 elif maintype == 'image': |
689
ecc566f8b69f
fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents:
604
diff
changeset
|
134 file_part = MIMEImage(self.get_content(msg_file), |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
135 _subtype=subtype) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
136 elif maintype == 'audio': |
689
ecc566f8b69f
fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents:
604
diff
changeset
|
137 file_part = MIMEAudio(self.get_content(msg_file), |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
138 _subtype=subtype) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
139 else: |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
140 file_part = MIMEBase(maintype, subtype) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
141 file_part.set_payload(self.get_content(msg_file)) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
142 # Encode the payload using Base64 |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
143 encoders.encode_base64(msg) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
144 # Set the filename parameter |
689
ecc566f8b69f
fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents:
604
diff
changeset
|
145 file_part.add_header('Content-Disposition', 'attachment', |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
146 filename=f_name) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
147 file_part.add_header('Content-Type', ctype, name=f_name) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
148 msg.attach(file_part) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
149 else: |
689
ecc566f8b69f
fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents:
604
diff
changeset
|
150 raise Exception('Attachment files should be' |
ecc566f8b69f
fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents:
604
diff
changeset
|
151 'a dict in format {"filename":"filepath"}') |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
152 |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
153 def get_content(self, msg_file): |
1029
3bf8898ce66d
removed immutable default argument from mailer
Marcin Kuzminski <marcin@python-works.com>
parents:
984
diff
changeset
|
154 """Get content based on type, if content is a string do open first |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
155 else just read because it's a probably open file object |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1169
diff
changeset
|
156 |
604
5cc96df705b9
fixed @repo into :repo for docs
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
157 :param msg_file: |
1029
3bf8898ce66d
removed immutable default argument from mailer
Marcin Kuzminski <marcin@python-works.com>
parents:
984
diff
changeset
|
158 """ |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
159 if isinstance(msg_file, str): |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
160 return open(msg_file, "rb").read() |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
161 else: |
1583
8e77c75bd65a
made auth type optional in constructor
Marcin Kuzminski <marcin@python-works.com>
parents:
1581
diff
changeset
|
162 # just for safe seek to 0 |
474
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
163 msg_file.seek(0) |
a3d9d24acbec
Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
164 return msg_file.read() |
1583
8e77c75bd65a
made auth type optional in constructor
Marcin Kuzminski <marcin@python-works.com>
parents:
1581
diff
changeset
|
165 |