25 |
25 |
from email.mime.audio import MIMEAudio
|
26 |
26 |
from email.mime.base import MIMEBase
|
27 |
27 |
from email.mime.image import MIMEImage
|
28 |
|
from email.mime.multipart import MIMEMultipart
|
29 |
28 |
from email.mime.nonmultipart import MIMENonMultipart
|
30 |
29 |
from email.mime.text import MIMEText
|
31 |
30 |
from email.utils import formataddr
|
... | ... | |
39 |
38 |
except ImportError:
|
40 |
39 |
docutils = None
|
41 |
40 |
|
|
41 |
from django.core.mail import EmailMessage, EmailMultiAlternatives
|
42 |
42 |
from django.template.loader import render_to_string
|
43 |
43 |
from django.utils.safestring import mark_safe
|
44 |
44 |
from quixote import get_publisher, get_request, get_response
|
... | ... | |
266 |
266 |
context['content'] = mark_safe(html_body)
|
267 |
267 |
html_body = render_to_string('qommon/email_body.html', context)
|
268 |
268 |
|
269 |
|
if text_body and html_body:
|
270 |
|
msg_body = MIMEMultipart(_charset=encoding, _subtype='alternative')
|
271 |
|
msg_body.attach(MIMEText(text_body, _charset=encoding))
|
272 |
|
msg_body.attach(MIMEText(html_body, _subtype='html', _charset=encoding))
|
273 |
|
elif text_body:
|
274 |
|
msg_body = MIMEText(text_body, _charset=encoding)
|
275 |
|
else:
|
276 |
|
msg_body = MIMEText(html_body, _subtype='html', _charset=encoding)
|
|
269 |
to_emails = []
|
|
270 |
bcc_emails = bcc or []
|
|
271 |
if email_rcpt:
|
|
272 |
if isinstance(email_rcpt, str):
|
|
273 |
email_rcpt = [email_rcpt]
|
|
274 |
if hide_recipients:
|
|
275 |
bcc_emails += email_rcpt[:]
|
|
276 |
else:
|
|
277 |
to_emails += email_rcpt[:]
|
|
278 |
if not ignore_mail_redirection:
|
|
279 |
mail_redirection = get_cfg('debug', {}).get('mail_redirection')
|
|
280 |
if mail_redirection:
|
|
281 |
to_emails, bcc_emails = [mail_redirection], []
|
|
282 |
if os.environ.get('QOMMON_MAIL_REDIRECTION'):
|
|
283 |
# if QOMMON_MAIL_REDIRECTION is set in the environment, send all emails
|
|
284 |
# to that address instead of the real recipients.
|
|
285 |
to_emails, bcc_emails = [os.environ.get('QOMMON_MAIL_REDIRECTION')], []
|
|
286 |
|
|
287 |
if not email_from:
|
|
288 |
email_from = emails_cfg.get('from')
|
|
289 |
if not email_from:
|
|
290 |
email_from = '%s@%s' % (pwd.getpwuid(os.getuid())[0], socket.getfqdn())
|
|
291 |
|
|
292 |
reply_to = None
|
|
293 |
if emails_cfg.get('reply_to'):
|
|
294 |
reply_to = emails_cfg.get('reply_to')
|
|
295 |
if replyto:
|
|
296 |
reply_to = replyto
|
277 |
297 |
|
278 |
298 |
attachments_parts = []
|
279 |
299 |
for attachment in attachments or []:
|
... | ... | |
282 |
302 |
if attachment:
|
283 |
303 |
attachments_parts.append(attachment)
|
284 |
304 |
|
285 |
|
if not attachments_parts:
|
286 |
|
msg = msg_body
|
287 |
|
else:
|
288 |
|
msg = MIMEMultipart(_charset=encoding, _subtype='mixed')
|
289 |
|
msg.attach(msg_body)
|
290 |
|
for attachment in attachments_parts:
|
291 |
|
msg.attach(attachment)
|
292 |
|
|
293 |
|
msg['Subject'] = Header(subject, encoding)
|
294 |
|
if hide_recipients or email_rcpt is None:
|
295 |
|
msg['To'] = 'Undisclosed recipients:;'
|
296 |
|
else:
|
297 |
|
if isinstance(email_rcpt, list):
|
298 |
|
msg['To'] = ', '.join(email_rcpt)
|
299 |
|
else:
|
300 |
|
msg['To'] = email_rcpt
|
301 |
|
|
302 |
|
if not email_from:
|
303 |
|
email_from = emails_cfg.get('from')
|
304 |
|
if not email_from:
|
305 |
|
email_from = '%s@%s' % (pwd.getpwuid(os.getuid())[0], socket.getfqdn())
|
|
305 |
email_msg_kwargs = {
|
|
306 |
'subject': subject,
|
|
307 |
'to': to_emails,
|
|
308 |
'bcc': bcc_emails,
|
|
309 |
'from_email': email_from,
|
|
310 |
'reply_to': reply_to,
|
|
311 |
'attachments': attachments_parts,
|
|
312 |
'headers': {
|
|
313 |
'X-Qommon-Id': os.path.basename(get_publisher().app_dir),
|
|
314 |
},
|
|
315 |
}
|
|
316 |
if extra_headers:
|
|
317 |
for key, value in extra_headers.items():
|
|
318 |
email_msg_kwargs['headers'][key] = value
|
306 |
319 |
|
307 |
320 |
sender_name = get_publisher().get_site_option(
|
308 |
321 |
'email_sender_name', 'variables'
|
309 |
322 |
) or get_publisher().get_site_option('global_title', 'variables')
|
310 |
323 |
if sender_name:
|
311 |
|
msg['From'] = formataddr((str(Header(sender_name, encoding)), email_from))
|
312 |
|
else:
|
313 |
|
msg['From'] = email_from
|
|
324 |
email_msg_kwargs['headers']['From'] = formataddr((str(Header(sender_name, encoding)), email_from))
|
314 |
325 |
|
315 |
|
if emails_cfg.get('reply_to'):
|
316 |
|
msg['Reply-To'] = emails_cfg.get('reply_to')
|
317 |
|
if replyto:
|
318 |
|
msg['Reply-To'] = replyto
|
319 |
|
|
320 |
|
msg['X-Qommon-Id'] = os.path.basename(get_publisher().app_dir)
|
321 |
|
if extra_headers:
|
322 |
|
for key, value in extra_headers.items():
|
323 |
|
msg[key] = value
|
|
326 |
if hide_recipients or not to_emails:
|
|
327 |
email_msg_kwargs['headers']['To'] = 'Undisclosed recipients:;'
|
324 |
328 |
|
325 |
|
if isinstance(email_rcpt, list):
|
326 |
|
rcpts = email_rcpt[:]
|
|
329 |
if text_body and html_body:
|
|
330 |
email_msg = EmailMultiAlternatives(body=text_body, **email_msg_kwargs)
|
|
331 |
email_msg.attach_alternative(html_body, 'text/html')
|
327 |
332 |
else:
|
328 |
|
rcpts = [email_rcpt]
|
329 |
|
if bcc:
|
330 |
|
rcpts += bcc
|
331 |
|
|
332 |
|
rcpts = [x for x in rcpts if x]
|
333 |
|
if len(rcpts) == 0:
|
334 |
|
return
|
335 |
|
|
336 |
|
if not ignore_mail_redirection:
|
337 |
|
mail_redirection = get_cfg('debug', {}).get('mail_redirection')
|
338 |
|
if mail_redirection:
|
339 |
|
rcpts = [mail_redirection]
|
340 |
|
if os.environ.get('QOMMON_MAIL_REDIRECTION'):
|
341 |
|
# if QOMMON_MAIL_REDIRECTION is set in the environment, send all emails
|
342 |
|
# to that address instead of the real recipients.
|
343 |
|
rcpts = [os.environ.get('QOMMON_MAIL_REDIRECTION')]
|
|
333 |
email_msg = EmailMessage(body=html_body or text_body, **email_msg_kwargs)
|
|
334 |
if html_body:
|
|
335 |
email_msg.content_subtype = 'html'
|
344 |
336 |
|
345 |
|
email_to_send = EmailToSend(email_from, rcpts, msg, smtp_timeout)
|
|
337 |
email_to_send = EmailToSend(email_msg, smtp_timeout)
|
346 |
338 |
if not fire_and_forget:
|
347 |
339 |
email_to_send()
|
348 |
340 |
else:
|
... | ... | |
384 |
376 |
|
385 |
377 |
|
386 |
378 |
class EmailToSend:
|
387 |
|
def __init__(self, msg_from, rcpts, msg, smtp_timeout):
|
388 |
|
self.msg_from = msg_from
|
389 |
|
self.rcpts = rcpts
|
390 |
|
self.msg = msg
|
|
379 |
def __init__(self, email_msg, smtp_timeout):
|
|
380 |
self.email_msg = email_msg
|
391 |
381 |
self.smtp_timeout = smtp_timeout
|
392 |
382 |
|
393 |
383 |
def __call__(self, job=None):
|
... | ... | |
395 |
385 |
|
396 |
386 |
s = create_smtp_server(emails_cfg, self.smtp_timeout)
|
397 |
387 |
try:
|
398 |
|
s.send_message(self.msg, self.msg_from, self.rcpts)
|
|
388 |
s.send_message(self.email_msg.message(), self.email_msg.from_email, self.email_msg.recipients())
|
399 |
389 |
except (smtplib.SMTPRecipientsRefused, smtplib.SMTPNotSupportedError, smtplib.SMTPDataError):
|
400 |
390 |
pass
|
401 |
391 |
s.quit()
|
402 |
|
-
|