##// END OF EJS Templates
fixed ehlo command for mailing....
marcink -
r952:beabd452 beta
parent child Browse files
Show More
@@ -1,118 +1,118 b''
1 1 import logging
2 2 import smtplib
3 3 import mimetypes
4 4 from email.mime.multipart import MIMEMultipart
5 5 from email.mime.image import MIMEImage
6 6 from email.mime.audio import MIMEAudio
7 7 from email.mime.base import MIMEBase
8 8 from email.mime.text import MIMEText
9 9 from email.utils import formatdate
10 10 from email import encoders
11 11
12 12 class SmtpMailer(object):
13 13 """simple smtp mailer class
14 14
15 15 mailer = SmtpMailer(mail_from, user, passwd, mail_server, mail_port, ssl, tls)
16 16 mailer.send(recipients, subject, body, attachment_files)
17 17
18 18 :param recipients might be a list of string or single string
19 19 :param attachment_files is a dict of {filename:location}
20 20 it tries to guess the mimetype and attach the file
21 21 """
22 22
23 23 def __init__(self, mail_from, user, passwd, mail_server,
24 24 mail_port=None, ssl=False, tls=False):
25 25
26 26 self.mail_from = mail_from
27 27 self.mail_server = mail_server
28 28 self.mail_port = mail_port
29 29 self.user = user
30 30 self.passwd = passwd
31 31 self.ssl = ssl
32 32 self.tls = tls
33 33 self.debug = False
34 34
35 35 def send(self, recipients=[], subject='', body='', attachment_files={}):
36 36
37 37 if isinstance(recipients, basestring):
38 38 recipients = [recipients]
39 39 if self.ssl:
40 40 smtp_serv = smtplib.SMTP_SSL(self.mail_server, self.mail_port)
41 41 else:
42 42 smtp_serv = smtplib.SMTP(self.mail_server, self.mail_port)
43 43
44 44 if self.tls:
45 45 smtp_serv.starttls()
46 46
47 47 if self.debug:
48 48 smtp_serv.set_debuglevel(1)
49 49
50 smtp_serv.ehlo("rhodecode mailer")
50 smtp_serv.ehlo()
51 51
52 52 #if server requires authorization you must provide login and password
53 53 smtp_serv.login(self.user, self.passwd)
54 54
55 55 date_ = formatdate(localtime=True)
56 56 msg = MIMEMultipart()
57 57 msg['From'] = self.mail_from
58 58 msg['To'] = ','.join(recipients)
59 59 msg['Date'] = date_
60 60 msg['Subject'] = subject
61 61 msg.preamble = 'You will not see this in a MIME-aware mail reader.\n'
62 62
63 63 msg.attach(MIMEText(body))
64 64
65 65 if attachment_files:
66 66 self.__atach_files(msg, attachment_files)
67 67
68 68 smtp_serv.sendmail(self.mail_from, recipients, msg.as_string())
69 69 logging.info('MAIL SEND TO: %s' % recipients)
70 70 smtp_serv.quit()
71 71
72 72
73 73 def __atach_files(self, msg, attachment_files):
74 74 if isinstance(attachment_files, dict):
75 75 for f_name, msg_file in attachment_files.items():
76 76 ctype, encoding = mimetypes.guess_type(f_name)
77 77 logging.info("guessing file %s type based on %s" , ctype, f_name)
78 78 if ctype is None or encoding is not None:
79 79 # No guess could be made, or the file is encoded (compressed), so
80 80 # use a generic bag-of-bits type.
81 81 ctype = 'application/octet-stream'
82 82 maintype, subtype = ctype.split('/', 1)
83 83 if maintype == 'text':
84 84 # Note: we should handle calculating the charset
85 85 file_part = MIMEText(self.get_content(msg_file),
86 86 _subtype=subtype)
87 87 elif maintype == 'image':
88 88 file_part = MIMEImage(self.get_content(msg_file),
89 89 _subtype=subtype)
90 90 elif maintype == 'audio':
91 91 file_part = MIMEAudio(self.get_content(msg_file),
92 92 _subtype=subtype)
93 93 else:
94 94 file_part = MIMEBase(maintype, subtype)
95 95 file_part.set_payload(self.get_content(msg_file))
96 96 # Encode the payload using Base64
97 97 encoders.encode_base64(msg)
98 98 # Set the filename parameter
99 99 file_part.add_header('Content-Disposition', 'attachment',
100 100 filename=f_name)
101 101 file_part.add_header('Content-Type', ctype, name=f_name)
102 102 msg.attach(file_part)
103 103 else:
104 104 raise Exception('Attachment files should be'
105 105 'a dict in format {"filename":"filepath"}')
106 106
107 107 def get_content(self, msg_file):
108 108 '''
109 109 Get content based on type, if content is a string do open first
110 110 else just read because it's a probably open file object
111 111 :param msg_file:
112 112 '''
113 113 if isinstance(msg_file, str):
114 114 return open(msg_file, "rb").read()
115 115 else:
116 116 #just for safe seek to 0
117 117 msg_file.seek(0)
118 118 return msg_file.read()
General Comments 0
You need to be logged in to leave comments. Login now