Example #1
0
def _send_mail_by_ses(messages):
    if not settings.EMAIL_HOST:
        return

    if len(messages) == 0:
        return

    is_invitate=False
    sender = Header(unicode(settings.APP_SHORT_NAME), 'utf-8')
    sender.append('<%s>' % unicode(settings.DEFAULT_FROM_EMAIL))
    sender = u'%s <%s>' % (unicode(settings.APP_SHORT_NAME), unicode(settings.DEFAULT_FROM_EMAIL))

    try:
        index = 0
        connection = None
        for recipient, subject, html, text, media in messages:

            if sender is None:
                sender = str(settings.DEFAULT_FROM_EMAIL)
                
            if hasattr(messages[0][0],"type") and messages[0][0].type == "invitation":
                is_invitate=True

            print ">>>",recipient.email," ",index
            index += 1

            msg = EmailMultiAlternatives(subject, from_email=sender,to=[recipient.email])
            msg.preamble = 'This is a multi-part message from %s.' % unicode(settings.APP_SHORT_NAME).encode('utf8')

            msgAlternative = MIMEMultipart('alternative')
            msg.attach(msgAlternative)

            msgAlternative.attach(MIMEText(text.encode('utf-8'), _charset='utf-8'))
            msgAlternative.attach(MIMEText(html.encode('utf-8'), 'html', _charset='utf-8'))
            for alias, location in media.items():
                fp = open(location, 'rb')
                msgImage = MIMEImage(fp.read())
                fp.close()
                msgImage.add_header('Content-ID', '<'+alias+'>')
                msg.attach(msgImage)

            try:
                msg.send()
            except Exception, e:
                logging.error("Couldn't send mail using the sendmail method: %s" % e)
    except Exception, e:
        logging.error('Email sending has failed: %s' % e)