From f09eac8ea2261c1e6f0539f380122567178dc9a2 Mon Sep 17 00:00:00 2001 From: nikhila05 Date: Tue, 17 Jul 2018 16:03:20 +0530 Subject: [PATCH 1/2] added pluggable email backends for aws ses, sendgrid, mandrill, mailgun, can send mail to active/inactive email ids with respective email senders (#54) --- crm/helper.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ crm/settings.py | 16 +++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 crm/helper.py diff --git a/crm/helper.py b/crm/helper.py new file mode 100644 index 00000000..4b2cf605 --- /dev/null +++ b/crm/helper.py @@ -0,0 +1,62 @@ +import boto.ses +import sendgrid +import requests +import mandrill +from django.core.mail import EmailMultiAlternatives +from django.conf import settings + + +def send_mail(mto, mfrom, msubject, mbody, user_active): + if mfrom: + mfrom = mfrom + else: + mfrom = settings.DEFAULT_FROM_EMAIL + if user_active: + mail_sender = settings.MAIL_SENDER + else: + mail_sender = settings.INACTIVE_MAIL_SENDER + if mail_sender == 'AMAZON': + # conn=SESConnection(settings.AM_ACCESS_KEY, settings.AM_PASS_KEY) + conn = boto.ses.connect_to_region( + settings.AWS_REGION, + aws_access_key_id=settings.AM_ACCESS_KEY, + aws_secret_access_key=settings.AM_PASS_KEY + ) + response = conn.send_email(mfrom, msubject, mbody, mto, format='html') + elif mail_sender == 'MAILGUN': + response = requests.post( + settings.MGUN_API_URL, + auth=('api', settings.MGUN_API_KEY), + data={ + 'from': mfrom, + 'to': mto, + 'subject': msubject, + 'html': mbody, + }) + elif mail_sender == 'SENDGRID': + sg = sendgrid.SendGridClient(settings.SG_USER, settings.SG_PWD) + sending_msg = sendgrid.Mail() + sending_msg.set_subject(msubject) + sending_msg.set_html(mbody) + sending_msg.set_text(msubject) + sending_msg.set_from(mfrom) + sending_msg.add_to(mto) + response = sg.send(sending_msg) + elif mail_sender == 'MANDRILL': + api_key = settings.MANDRILL_API_KEY + mandrill_client = mandrill.Mandrill(api_key) + + message = { + "html": mbody, + "subject": msubject, + "from_email": mfrom, + "from_name": "Django CRM", + "to": [{'email': i, 'type': 'to'} for i in mto] + } + response = mandrill_client.messages.send(message=message) + + else: + msg = EmailMultiAlternatives(msubject, mbody, mfrom, [mto]) + msg.attach_alternative(mbody, "text/html") + response = msg.send() + return response diff --git a/crm/settings.py b/crm/settings.py index d0a6bab1..41b02fe9 100644 --- a/crm/settings.py +++ b/crm/settings.py @@ -171,3 +171,19 @@ COMPRESS_PRECOMPILERS = ( COMPRESS_OFFLINE_CONTEXT = { 'STATIC_URL': 'STATIC_URL', } + +DEFAULT_FROM_EMAIL = 'no-reply@django-crm.micropyramid.com' +MAIL_SENDER = 'AMAZON' +INACTIVE_MAIL_SENDER = 'MANDRILL' + +AM_ACCESS_KEY = os.getenv('AM_ACCESS_KEY', '') +AM_PASS_KEY = os.getenv('AM_PASS_KEY', '') +AWS_REGION = os.getenv('AWS_REGION', '') + +MGUN_API_URL = os.getenv('MGUN_API_URL', '') +MGUN_API_KEY = os.getenv('MGUN_API_KEY', '') + +SG_USER = os.getenv('SG_USER', '') +SG_PWD = os.getenv('SG_PWD', '') + +MANDRILL_API_KEY = os.getenv('MANDRILL_API_KEY', '') From e78cc567efeeea6c38864dd73f9234e0aea75e05 Mon Sep 17 00:00:00 2001 From: The Codacy Badger Date: Wed, 18 Jul 2018 09:44:44 +0000 Subject: [PATCH 2/2] Add Codacy badge --- README.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.rst b/README.rst index 191ad8b1..538787f5 100644 --- a/README.rst +++ b/README.rst @@ -1,6 +1,10 @@ Django-CRM ========== +.. image:: https://api.codacy.com/project/badge/Grade/b11da5f09dd542479fd3bd53944595d2 + :alt: Codacy Badge + :target: https://app.codacy.com/app/ashwin/Django-CRM?utm_source=github.com&utm_medium=referral&utm_content=MicroPyramid/Django-CRM&utm_campaign=badger + Django CRM is opensourse CRM developed on django framework. It has all the basic features of CRM to start with. We welcome code contributions and feature requests via github.