Exemplo n.º 1
0
 def send_mail(self):
     name, email, msg = self.data['name'], self.data['email'], self.data['msg']
     msg = ("Mensagem de '%s (%s)':\n----------------\n"% (name, email)) + msg
     contacts = getattr(settings, 'CONTACTS', settings.ADMINS)
     to = [x[1] for x in contacts]
     email = EmailMessage(subject="Delos - Contato pelo site", body=msg, to=to, headers={'reply-to': email})
     email.encoding = 'utf-8'
     email.send(fail_silently=True)
Exemplo n.º 2
0
def send_mail(subject, recipient_persons, template_path=None, context_dict={}, 
              fail_silently=False, content_type='html', cc_persons=None, 
              template_string=None, reply_to=None, from_person=None, bcc=False):
    if template_path:
        temp = loader.get_template(template_path)
    elif template_string:
        temp = Template(template_string)
    context_dict['site'] = Site.objects.get_current()
    context = Context(context_dict)
    msg = temp.render(context).encode('utf-8')
    try:
        recipient_list = ["%s <%s>" % (p.name, p.get_email()) for p in recipient_persons]
    except:
        recipient_list = ["%s <%s>" % (recipient_persons.name, recipient_persons.get_email())]
    if cc_persons:
        cc_list = ["%s <%s>" % (p.name, p.get_email()) for p in cc_persons]
    else:
        cc_list = None
    
    headers = {}
    if reply_to:
        headers['reply-to'] = "%s <%s>" % (reply_to.name, reply_to.get_email())
    
    if from_person:
        from_email = "%s <%s>" % (from_person.name, from_person.get_email())
        headers['from'] = from_email
    else:
        from_email = settings.EMAIL_FROM
    
    if not bcc:
        email = EmailMessage(subject=subject, body=msg, from_email=from_email,
                to=recipient_list, cc=cc_list, headers=headers)
    else:
        email = EmailMessage(subject=subject, body=msg, from_email=from_email,
                bcc=recipient_list, cc=cc_list, headers=headers)
    email.content_subtype = content_type
    email.encoding = 'utf-8'
    
    email.send(fail_silently=fail_silently)