How can I properly send batch/bulk/mass emails using MailGun in Django using SMTP protocol?
What I've tried so far?
- I am using
django.core.mail.backends.smtp.EmailBackend
as myEMAIL_BACKEND
and this is the code snippet that I have tried to send the emails.
from django.core.mail import EmailMultiAlternatives
import json
to_emails = [
"mail_1@example.com",
"mail_2@example.com",
"mail_3@example.com",
"mail_4@example.com",
"jerinpetergeorge@gmail.com",
]
mail = EmailMultiAlternatives(
subject="Hey - %recipient.name%",
body="Hey %recipient.name%,\n\nThis is just a batch email test!!!",
from_email="JPG <me@somehost.com>",
to=to_emails,
)
recipient_variables = {
address: {"name": address} for address in to_emails
}
mail.extra_headers["X-Mailgun-Recipient-Variables"] = json.dumps(recipient_variables)
response = mail.send()
print(response)
and I've got the mail as below,
As we can see, the to
attribute is filled with all email addresses, which is not what I am expecting.
So, how can I tell the Mailgun/Django to parse my variables properly in order to make the emails looks more personal?
Notes
- I prefer to use SMTP protocol
- I've tried the REST APIs of Mailgun and it was a success (but, I prefer SMTP)
- I found django-anymail and seems it has the feature. But, It also uses the APIs (correct me if I am wrong)
Update-1
- Updated the
to
argument toto="%recipient%"
But, got-
TypeError: "to" argument must be a list or tuple
-
- Updated the
to
argument toto=["%recipient%"]
But, got-
smtplib.SMTPRecipientsRefused: {'=?utf-8?q?=25recipient=25?=': (501, b'Invalid command or cannot parse to address')}
-
from How to use Mailgun's recipient-variables with Django SMTP mail backend?
No comments:
Post a Comment