Skip to content
Snippets Groups Projects
email.py 1.32 KiB
Newer Older
Piotr Gawron's avatar
Piotr Gawron committed
# coding=utf-8

from django.core import mail
from django.core.mail.backends.smtp import EmailBackend

from web.models import ConfigurationItem
from web.models.constants import EMAIL_HOST_CONFIGURATION_TYPE, EMAIL_PASSWORD_CONFIGURATION_TYPE, \
    EMAIL_PORT_CONFIGURATION_TYPE, EMAIL_USER_CONFIGURATION_TYPE, EMAIL_CONFIGURATION_TYPE


class EmailSender(object):
    def __init__(self):
        host = ConfigurationItem.objects.get(type=EMAIL_HOST_CONFIGURATION_TYPE).value
        port = ConfigurationItem.objects.get(type=EMAIL_PORT_CONFIGURATION_TYPE).value
        user = ConfigurationItem.objects.get(type=EMAIL_USER_CONFIGURATION_TYPE).value
        password = ConfigurationItem.objects.get(type=EMAIL_PASSWORD_CONFIGURATION_TYPE).value
        self.backend = EmailBackend(host=host, port=port, username=user, password=password, use_tls=False,
                                    use_ssl=False)
        # self.connection = mail.get_connection(backend=self.backend)
        self.connection = mail.get_connection()

    def send_email(self, subject, body, to):
        email_from = ConfigurationItem.objects.get(type=EMAIL_CONFIGURATION_TYPE).value
        print email_from
        print subject
        print body
        print to
        mail.EmailMessage(subject, body, email_from, [to], connection=self.connection).send(fail_silently=False)