Search code examples
djangoemaildjango-signalsdjango-email

recipient_list send mail just to the first address in the list


I´m using signals to send mails to the users depending of some actions. In one of my signals I need to send the same mail to multiple users, my signal use post_save so for the parameter [recipient_list] I use this instance.email_list email_list is where I store the list of email addresses to send the mail, they are stored in this format user1@mail.com, user2@mail.com, user3@mail.com.

To send the emails I use EMAIL_HOST = 'smtp.gmail.com' the problem is that the only user who recive the email is the first one in the list. So I 'log in' in the gmail account to check the send emails section and actually in the "to" area of the email shows that was send to all the users in the list but the emails never arrive just for the first email address in the list.

I read that if google detect that the account sends a lot of messages could be blocked but only send like 5 emails at the same time and when I'm in the account never shows me some alert or something.

So the problem is how I send the emails or maybe some bad configuration of the gmail account?

Any help is really appreciated.

Sorry for my bad grammar.

EDIT: Here's my code.

forms.py

class MyForm(forms.Form):
    userslist = forms.ModelChoiceField(queryset = User.objects.filter(here goes my condition to show the users), empty_label='List of users', label='Users', required=False)
    emailaddress = forms.CharField(max_length=1000, label='Send to:', required=False)
    comment = forms.CharField(widget=CKEditorUploadingWidget(), label="Comment:")

That form display a list of users to select the email address in the field emailaddress store the values. This is my Ajax to bring the email address:

views.py

class mails(TemplateView):
    def get(self, request, *args, **kwargs):
        id_user = request.GET['id']
        us = User.objects.filter(id = id_user)
        data = serializers.serialize('json', us, fields=('email'))
        return HttpResponse(data, content_type='application/json')

And here's the <script> I use to populate the emailaddres field:

<script>
    $('#id_userlist').on('change', concatenate);
    function concatenate() {
        var id = $(this).val();
        $.ajax({
            data: { 'id': id },
            url: '/the_url_to_get_data/',
            type: 'get',
            success: function (data) {
                var mail = ""
                for (var i = 0; i < data.length; i++) {
                    mail += data[i].fields.email;
                }
                var orig = $('#id_emailaddress').val();
                $('#id_emailaddress').val(orig + mail + ',');
            }
        })
    }
</script>

The signal I use to send the mail is this:

@receiver(post_save, sender=ModelOfMyForm, dispatch_uid='mails_signal')
def mails_signal(sender, instance, **kwargs):
    if kwargs.get('created', False):
        if instance.emailaddress:
            #Here goes the code for the subject, plane_message,
            #from_email and template_message.
            send_mail(subject, plane_message, from_email, [instance.emailaddress], fail_silently=False, html_message=template_message)

So if I select 4 users the info is save in this way in the database:

data info

Then I 'log in' in the account to check the 'Sent Mail' section and check the detail of the mail and shows that was send to the 4 users but the only user who recibe the mail was the first in the list.

enter image description here


Solution

  • Your problem is that you are passing a comma-separated string of email addresses inside instance.emailaddress (first@gmail.com, second@hotmail.com, third@hotmail.com etc). Django expects a Python list of addresses, not a comma separated string. It will just ignore everything after the first comma.

    Change your code as follows and it will work:

    def mails_signal(sender, instance, **kwargs):
        if kwargs.get('created', False):
            if instance.emailaddress:
                #Here goes the code for the subject, plane_message,
                #from_email and template_message.
                recipients = [r.strip() for r in instance.emailaddress.split(',')]
                send_mail(subject, plane_message, from_email, recipients, fail_silently=False, html_message=template_message)