Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send_mass_emails with EmailMultiAlternatives

Tags:

I try to build a newsletter application and want to send 50 emails with one connection. send_mass_mail() looks perfect but I can't figure out how I call this in combination with EmailMultiAlternatives.

This is my code that sends only one email with one connection:

html_content = render_to_string('newsletter.html', {'newsletter': n,})                text_content = "..."                       msg = EmailMultiAlternatives("subject", text_content, "from@bla", ["to@bla"])                                       msg.attach_alternative(html_content, "text/html")                                                                                                                                                                                msg.send()   

a working example with the code above and send_mass_mail would be great, thanks!

like image 503
gustavgans Avatar asked Sep 28 '11 13:09

gustavgans


People also ask

What is EmailMultiAlternatives?

EmailMultiAlternatives is a subclass of EmailMessage. EmailMessage has a 'connection' parameter allowing you to specify a single connection to use when sending the message to all recipients - i.e. the same functionality as provided by send_mass_mail().

How to send mail with django?

In most cases, you can send email using django.core.mail.send_mail() . The subject , message , from_email and recipient_list parameters are required.

How to send email using django rest framework?

Inside of the “send_mail.py”, create a function that takes in the following arguments. def send_mail(html,text='Email_body',subject='Hello word',from_email='',to_emails=[]): The next step would be to make sure that the “to_emails” argument is always a “list of emails” and not a string or any other data type.


1 Answers

That is send_mass_mail() rewritten to use EmailMultiAlternatives:

from django.core.mail import get_connection, EmailMultiAlternatives  def send_mass_html_mail(datatuple, fail_silently=False, user=None, password=None,                          connection=None):     """     Given a datatuple of (subject, text_content, html_content, from_email,     recipient_list), sends each message to each recipient list. Returns the     number of emails sent.      If from_email is None, the DEFAULT_FROM_EMAIL setting is used.     If auth_user and auth_password are set, they're used to log in.     If auth_user is None, the EMAIL_HOST_USER setting is used.     If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.      """     connection = connection or get_connection(         username=user, password=password, fail_silently=fail_silently)     messages = []     for subject, text, html, from_email, recipient in datatuple:         message = EmailMultiAlternatives(subject, text, from_email, recipient)         message.attach_alternative(html, 'text/html')         messages.append(message)     return connection.send_messages(messages) 
like image 191
semente Avatar answered Sep 23 '22 03:09

semente