Jack
Jack

Reputation: 437

Django send_mass_email without showing all recipients' Emails

Using send_mass_mail will result in all the emails being shown in the 'To' field.

I came across this solution but it hides all user Emails. And it looks weird in the inbox because the 'To' field is empty.

What I want is to use send_mass_mail showing only the receiving recipient's Email in the 'To' field. E.g. If I send Emails to [email protected] and [email protected], jack should see [email protected] and jane should see [email protected] in the 'To' field.

Is there a way to achieve this? Or do I have to use loop over each recipient with send_mail?

Upvotes: 1

Views: 1171

Answers (2)

Amar Kumar
Amar Kumar

Reputation: 2646

Try This:

from django.core.mail import send_mass_mail

subject = 'test subject'
message = 'test message'
from_email = '[email protected]'
recipient_list = ['[email protected]', '[email protected]', '[email protected]']

messages = [(subject, message, from_email, [recipient]) for recipient in recipient_list]

send_mass_mail(messages)

Upvotes: 1

lukewarm
lukewarm

Reputation: 857

You can send separate emails directly to different recipients using send_mass_mail, you just need to get the arguments structured properly:

message1 = ('Subject here', 'Here is the message', '[email protected]', ['[email protected]', '[email protected]'])
message2 = ('Another Subject', 'Here is another message', '[email protected]', ['[email protected]'])
send_mass_mail((message1, message2), fail_silently=False)

This is not too different from iterating over the recipients yourself and sending each email separately, except for one key difference; using send_mass_email will result in all emails being sent via a single connection to your mail server. Doing it iteratively in your code will result in connections being opened and closed for each email and as a result will be slower due to that overhead.

Upvotes: 1

Related Questions