annotate rhodecode/lib/smtp_mailer.py @ 984:ccd323bfa121 beta

Added limit option for revision ranges
author Marcin Kuzminski <marcin@python-works.com>
date Mon, 31 Jan 2011 17:41:12 +0100
parents c45c910714e4
children 3bf8898ce66d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 ~~~~~~~~~~~~~~~~~~~~~~~~~
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
5
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
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
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
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
9 :copyright: (c) 2011 by marcink.
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
10 :license: LICENSE_NAME, see LICENSE_FILE for more details.
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 """
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
12
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 import logging
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14 import smtplib
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 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
16 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
17
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 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
19 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
20 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
21 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
22 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
23 from email.utils import formatdate
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 from email import encoders
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26 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
27 """SMTP mailer class
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29 mailer = SmtpMailer(mail_from, user, passwd, mail_server, mail_port, ssl, tls)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 mailer.send(recipients, subject, body, attachment_files)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32 :param recipients might be a list of string or single string
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 :param attachment_files is a dict of {filename:location}
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
34 it tries to guess the mimetype and attach the file
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
35
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36 """
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38 def __init__(self, mail_from, user, passwd, mail_server,
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 mail_port=None, ssl=False, tls=False):
689
ecc566f8b69f fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 604
diff changeset
40
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41 self.mail_from = mail_from
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42 self.mail_server = mail_server
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43 self.mail_port = mail_port
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44 self.user = user
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
45 self.passwd = passwd
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
46 self.ssl = ssl
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47 self.tls = tls
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 self.debug = False
689
ecc566f8b69f fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 604
diff changeset
49
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 def send(self, recipients=[], subject='', body='', attachment_files={}):
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 if isinstance(recipients, basestring):
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 recipients = [recipients]
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 if self.ssl:
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 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
56 else:
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 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
58
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 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
60 smtp_serv.ehlo()
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 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
62
ecc566f8b69f fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 604
diff changeset
63 if self.debug:
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 smtp_serv.set_debuglevel(1)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65
952
beabd452f08b fixed ehlo command for mailing.
Marcin Kuzminski <marcin@python-works.com>
parents: 689
diff changeset
66 smtp_serv.ehlo()
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 #if server requires authorization you must provide login and password
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
69 #but only if we have them
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
70 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
71 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
72
474
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 date_ = formatdate(localtime=True)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 msg = MIMEMultipart()
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76 msg['From'] = self.mail_from
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
77 msg['To'] = ','.join(recipients)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 msg['Date'] = date_
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79 msg['Subject'] = subject
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 msg.preamble = 'You will not see this in a MIME-aware mail reader.\n'
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82 msg.attach(MIMEText(body))
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84 if attachment_files:
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85 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
86
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 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
88 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
89
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
90 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
91 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
92 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
93 # 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
94 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
95
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98 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
99 if isinstance(attachment_files, dict):
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100 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
101 ctype, encoding = mimetypes.guess_type(f_name)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 logging.info("guessing file %s type based on %s" , ctype, f_name)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 if ctype is None or encoding is not None:
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104 # No guess could be made, or the file is encoded (compressed), so
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 # use a generic bag-of-bits type.
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
106 ctype = 'application/octet-stream'
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
107 maintype, subtype = ctype.split('/', 1)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 if maintype == 'text':
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 # 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
110 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
111 _subtype=subtype)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112 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
113 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
114 _subtype=subtype)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115 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
116 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
117 _subtype=subtype)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 else:
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 file_part = MIMEBase(maintype, subtype)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 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
121 # Encode the payload using Base64
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 encoders.encode_base64(msg)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123 # 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
124 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
125 filename=f_name)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126 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
127 msg.attach(file_part)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 else:
689
ecc566f8b69f fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 604
diff changeset
129 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
130 '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
131
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132 def get_content(self, msg_file):
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 '''
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134 Get content based on type, if content is a string do open first
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135 else just read because it's a probably open file object
604
5cc96df705b9 fixed @repo into :repo for docs
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
136 :param msg_file:
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 '''
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138 if isinstance(msg_file, str):
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139 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
140 else:
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141 #just for safe seek to 0
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
142 msg_file.seek(0)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143 return msg_file.read()