jazzband / django-newsletter

An email newsletter application for the Django web application framework, including an extended admin interface, web (un)subscription, dynamic e-mail templates, an archive and HTML email support.
GNU Affero General Public License v3.0
849 stars 206 forks source link

How to send emails using Zoho email? #231

Closed rubenalvesbcn closed 7 years ago

rubenalvesbcn commented 7 years ago

Hi,

I'm trying to make the Django Newsletter works with the Zoho Mail, but I wasn't able to make it work after two days trying all the time.

The closest that I reached was following the link: https://stackoverflow.com/questions/18335697/send-email-through-zoho-smtp

But according to some proposals there I should modify the code of Django Newsltter, what is not an option now.

Does anybody was able to send email using Zoho Mail?

dokterbob commented 7 years ago

This is a general Django issue and has no relation to this package whatsoever. Please close this issue.

Thanks (and good luck!)

Ruben Alves notifications@github.com schreef op 25 juni 2017 16:57:43 WEST:

Hi,

I'm trying to make the Django Newsletter works with the Zoho Mail, but I wasn't able to make it work after two days trying all the time.

The closest that I reached was following the link: https://stackoverflow.com/questions/18335697/send-email-through-zoho-smtp

But according to some proposals there I should modify the code of Django Newsltter, what is not an option now.

Does anybody was able to send email using Zoho Mail?

-- Verstuurd vanaf mijn Android apparaat met K-9 Mail. Excuseer mijn beknoptheid.

rubenalvesbcn commented 7 years ago

I was able to solve it analyzing it deeper after looking https://forums.zoho.com/topic/zoho-mail-servers-reject-python-smtp-module-communications.

On the Submission model, on the submit method I saw the following:

message = EmailMultiAlternatives(
    subject, text,
    from_email=self.newsletter.get_sender(),
    to=[subscription.get_recipient()],
    headers=extra_headers,
)

The important part on the code is from_email=self.newsletter.get_sender()

I saw that the email that I have added as the Sender Email of the Newsletter on the admin page was different of the one defined on settings.EMAIL_HOST_USER and settings.DEFAULT_FROM_EMAIL. By the way, these two variables must have the same value.

So, if I add on the Newsletter the email defined on settings.EMAIL_HOST_USER it works.

But once the field is editable on the admin page, it's prone to errors, so what I did was add the default value settings.EMAIL_HOST_USER to this field and make it read only on the admin (my admin.py file) using the following code:

from django.contrib import admin
from django.conf import settings
from django.contrib.admin import widgets
from django.forms import ModelForm
from newsletter.models import Newsletter
from newsletter.admin import NewsletterAdmin

admin.site.unregister(Newsletter)

class ReadyOnlyField(widgets.AdminTextInputWidget):

    ATTRS = {'readonly': True, 'style': 'background-color: #eee'}

    def __init__(self, attrs=ATTRS):
        super(ReadyOnlyField, self).__init__(attrs=attrs)

class CustomNewsletterAdminForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(CustomNewsletterAdminForm, self).__init__(*args, **kwargs)
        self.fields['email'].widget = ReadyOnlyField()
        self.fields['sender'].widget = ReadyOnlyField()

class CustomNewsletterAdmin(NewsletterAdmin):
    form = CustomNewsletterAdminForm

    list_display = (
        'title', 'email', 'sender', 'admin_subscriptions', 'admin_messages',
        'admin_submissions'
    )

    def get_changeform_initial_data(self, request):
        return {
            'email': settings.EMAIL_HOST_USER,
            'sender': 'Alô Linux'
        }

admin.site.register(Newsletter, CustomNewsletterAdmin)