def send(request, to, subject, body, behalf=None, cc='', bcc=''): reply_to = {} if hasattr(settings, 'REPLY_TO'): reply_to = {'Reply-To': settings.REPLY_TO, 'Return-Path': settings.REPLY_TO} if behalf is None and hasattr(settings, 'EMAIL_FROM'): behalf = settings.EMAIL_FROM cc = _combine_with_settings(cc, 'EMAIL_CC') bcc = _combine_with_settings(bcc, 'EMAIL_BCC') e = EmailMultiAlternatives( subject=subject, from_email=behalf, to=to.split(';'), cc=cc, bcc=bcc, headers=reply_to ) e.attach_alternative(markdown2.markdown( body, extras=["link-patterns", "tables", "code-friendly"], link_patterns=registry.link_patterns(request), safe_mode=True ), 'text/html') e.content_subtype = 'html' e.send()
def send(request, to, subject, body, behalf=None, cc='', bcc=''): reply_to = {} if hasattr(settings, 'REPLY_TO'): reply_to = { 'Reply-To': settings.REPLY_TO, 'Return-Path': settings.REPLY_TO } if behalf is None and hasattr(settings, 'EMAIL_FROM'): behalf = settings.EMAIL_FROM cc = _combine_with_settings(cc, 'EMAIL_CC') bcc = _combine_with_settings(bcc, 'EMAIL_BCC') e = EmailMultiAlternatives(subject=subject, from_email=behalf, to=to.split(';'), cc=cc, bcc=bcc, headers=reply_to) e.attach_alternative( markdown2.markdown(body, extras=["link-patterns", "tables", "code-friendly"], link_patterns=registry.link_patterns(request), safe_mode=True), 'text/html') e.content_subtype = 'html' e.send()
def send(self, event, users, instance, paths): from_address = settings.ASYNC_EMAIL_FROM reply_to = {} if hasattr(settings, 'ASYNC_EMAIL_REPLY_TO'): reply_to = { 'Reply-To': settings.ASYNC_EMAIL_REPLY_TO, 'Return-Path': settings.ASYNC_EMAIL_REPLY_TO } messages = [] for user, templates in users.items(): if not self.enabled(event, user, paths) or not user.email: continue template = self._get_template(templates) if template is None: continue params = self.prepare(template, instance) e = mail.EmailMultiAlternatives(subject=params['subject'], body=params['description'], from_email=from_address, to=[ user.email, ], headers=reply_to) e.attach_alternative( markdown2.markdown( params['description'], extras=["link-patterns"], link_patterns=link_registry.link_patterns(request), safe_mode=True), 'text/html') messages.append(e) if len(messages): connection = mail.get_connection() connection.send_messages(messages)
def send(self, observation, users, instance, paths): if not self._ensure_connection(): print("Cannot contact the XMPP server") return for user, templates in users.items(): jid = self._get_jid(user) if not self.enabled(observation, user, paths) or jid is None: continue template = self._get_template(templates) if template is None: continue params = self.prepare(template, instance) message = xmpp.protocol.Message( jid, body=params['short_description'].encode('utf-8'), subject=params['subject'].encode('utf-8'), typ='chat') html = xmpp.Node('html', {'xmlns': 'http://jabber.org/protocol/xhtml-im'}) text = u"<body xmlns='http://www.w3.org/1999/xhtml'>" + markdown2.markdown( params['short_description'], extras=["link-patterns"], link_patterns=link_registry.link_patterns(request), safe_mode=True) + u"</body>" html.addChild(node=xmpp.simplexml.XML2Node(text.encode('utf-8'))) message.addChild(node=html) self.client.send(message) self.client.disconnected()
def send_email(request): if request.method == 'POST': try: from django.core.mail import EmailMultiAlternatives behalf = request.POST['behalf'] to = request.POST['to'] cc = request.POST['cc'] bcc = request.POST['bcc'] subject = request.POST['subject'] body = request.POST['body'] if hasattr(settings, 'REPLY_TO'): reply_to = {'Reply-To': settings.REPLY_TO, 'Return-Path': settings.REPLY_TO} else: reply_to = {} e = EmailMultiAlternatives( subject=subject, from_email=behalf, to=to.split(';'), cc=cc.split(';'), bcc=bcc.split(';'), headers=reply_to ) e.attach_alternative(markdown2.markdown(body, extras=["link-patterns", "tables"], link_patterns=registry.link_patterns(request), safe_mode=True), 'text/html') e.content_subtype = 'html' e.send() return HttpResponse(dumps({'status': 'ok'}), content_type="application/json") except Exception, e: return HttpResponse(dumps({'status': 'ko', 'error': str(e)}), content_type="application/json")
def prepare_email_message(to, subject, body, behalf=None, cc=None, bcc=None, request=None): reply_to = {} if hasattr(settings, 'REPLY_TO'): reply_to = {'Reply-To': settings.REPLY_TO, 'Return-Path': settings.REPLY_TO} if behalf is None and hasattr(settings, 'EMAIL_FROM'): behalf = settings.EMAIL_FROM if not isinstance(to, (tuple, list)): to = to.split(';') email_message = EmailMultiAlternatives( subject=subject, body=body, from_email=behalf, to=to, cc=cc, bcc=bcc, headers=reply_to ) email_message.attach_alternative(markdown2.markdown( body, extras=["link-patterns", "tables", "code-friendly"], link_patterns=registry.link_patterns(request), safe_mode=True ), 'text/html') return email_message
def send(self, event, users, instance, paths): if not self._ensure_connection(): print("Cannot contact the XMPP server") return for user, templates in users.items(): jid = self._get_jid(user) if not self.enabled(event, user, paths) or jid is None: continue template = self._get_template(templates) if template is None: continue params = self.prepare(template, instance) message = xmpp.protocol.Message(jid, body=params['short_description'].encode('utf-8'), subject=params['subject'].encode('utf-8'), typ='chat') html = xmpp.Node('html', {'xmlns': 'http://jabber.org/protocol/xhtml-im'}) text = u"<body xmlns='http://www.w3.org/1999/xhtml'>" + markdown2.markdown(params['short_description'], extras=["link-patterns"], link_patterns=link_registry.link_patterns( request), safe_mode=True) + u"</body>" html.addChild(node=xmpp.simplexml.XML2Node(text.encode('utf-8'))) message.addChild(node=html) self.client.send(message) self.client.disconnected()