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 jack#gmail.com and jane#gmail.com, jack should see jack#gmail.com and jane should see jane#gmail.com in the 'To' field.
Is there a way to achieve this? Or do I have to use loop over each recipient with send_mail?
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', 'from#example.com', ['first#example.com', 'other#example.com'])
message2 = ('Another Subject', 'Here is another message', 'from#example.com', ['second#test.com'])
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.
Try This:
from django.core.mail import send_mass_mail
subject = 'test subject'
message = 'test message'
from_email = 'from#from.com'
recipient_list = ['a#a.com', 'b#b.com', 'c#c.com']
messages = [(subject, message, from_email, [recipient]) for recipient in recipient_list]
send_mass_mail(messages)
Related
I'm solving problem, that of course I'm sending an email like sender email, which I authenticated in settings.py, but what if I want to send and email as request.user.email alias?
send_mail("Hello", "hello", settings.EMAIL_HOST_USER, [settings.EMAIL_RECEIVER])
is this some reason how to do that?
Thanks a lot
settings.EMAIL_HOST_USER Is used for authentication.
It should be like this:
send_mail("Hello", "hello", “sender#email.com”, [“recipient#email.com”], fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=None, connection=None, html_message=None)
To add an email alias you just need to add that field to your user model and reference that instead of “sender#email.com”
Sendgrid allows to specify unique arguments when sending emails. These can be used for the event webhook integration to identify emails doc.
I have an existing code piece in django that uses django.core.mail.EmailMultiAlternatives to send emails via SendGrid. I'd like to specify the above mentioned unique arguments if possible. So far I was trying to use the custom_args field
email.custom_args = {'test_arg': 'value'}
but that didn't seem to work.
I saw that there's a django-sendgrid module, but if possible I'd prefer not having to re-write the existing code base.
i don't use the SendGrid, but looks like the Unique Arguments is email headers, and by the doc: emailmessage, you can add headers for example:
from django.core.mail import EmailMultiAlternatives
subject, from_email, to = 'hello', 'EXAMPLE#FROM.com', 'EXAMPLE#TO.NET'
text_content = 'This is an important message.'
msg = EmailMultiAlternatives(
subject,
text_content,
from_email,
[to], headers={"customerAccountNumber": "55555", },
)
msg.send()
Have you tried using the SendGrid Python Library?
The term custom_args is specific to Web API v3 Mail Send, so adding it to your SMTP message won't work. The Web v3 API call is faster and more full-featured than the SMTP transaction, but is a newer generation of call, and has some updated vocabulary.
If you need to send via SMTP, you'll need to use the term unique_args within the X-SMTPAPI header specifically. That will allow those key:values to be attached to all Events related to the messages generated in that send.
I'm using Python and Django to configure sending messages through SendGrid: https://docs.sendgrid.com/for-developers/sending-email/django
As others have mentioned, the best way to match event webhooks with previously sent emails is to add a unique identifier when sending the email. To do so, you need to use the X-SMTPAPI header with a unique_args key/value. It's important to configure this header correctly, otherwise the email will be rejected. Two helpful links:
Building an X-SMTPAPI Header
Unique Arguments
Based on this documentation, here's what my implementation looks like:
MESSAGE_ID_KEY = 'jv_message_id'
message = EmailMultiAlternatives(
subject=subject,
body=plain_content,
from_email=from_email or EMAIL_ADDRESS_SEND,
to=to_emails,
cc=cc_email,
headers={
'X-SMTPAPI': json.dumps({'unique_args': {MESSAGE_ID_KEY: '<internal message id>'}})
}
)
Then when the webhook is sent, I get the message ID like this:
def post(self, request):
event_data = request.data
if not event_data:
logger.error('Sendgrid webhook delivered an unparseable request')
return Response(status=status.HTTP_200_OK)
for event in event_data:
event_type = event['event']
if not (message_id := event.get(MESSAGE_ID_KEY)):
logger.warning('Sendgrid webhook delivered an event without a unique message ID')
continue
...
I want to send a survey to my customers so I want each email subject, body contain customer's name.
Is it possible to send all emails at once or I have to send email multiple times, each time for a specific customer throughout SMTP?
You can use send_mass_mail()
message1 = ('Subject here', 'Here is the message', 'from#example.com',
['first#example.com', 'other#example.com'])
message2 = ('Another Subject', 'Here is another message',
'from#example.com', ['second#test.com'])
send_mass_mail((message1, message2), fail_silently=False)
https://docs.djangoproject.com/en/1.8/topics/email/#send-mass-mail
SMTP accepts a single message, to possibly multiple recipients. Assuming you previously used bcc, that could be a single message.
If you're customizing the subject, or body of the email; there will have to be multiple mail() calls and multiple SMTP calls
I have a functionality in a Django app to send an email to all the registered users, I'm currently doing it with 'EmailMessage' and it works perfectly but everybody gets to see every other recipient's email which is unwanted.
Is there a way to hide recipients using the Django mailing functions?
Thank you.
when you instantiate EmailMessage class you can provide bcc attribute such as the example.
Here is the EmailMessage class
class EmailMessage(object):
"""
A container for email information.
"""
content_subtype = 'plain'
mixed_subtype = 'mixed'
encoding = None # None => use settings default
def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
connection=None, attachments=None, headers=None, cc=None):
so if you provide bcc recipient with the attribute name. you can set the target email as bcc recipient.
message = EmailMessage('hello', 'body', bcc=['user#email.com',])
message.send()
http://en.wikipedia.org/wiki/Blind_carbon_copy
https://docs.djangoproject.com/en/1.7/topics/email/
Definitely BCC each address, this should hide them for any recipient. By the looks of the docs you will need to create your own EmailMessage rather than using the predefined wrappers.
You can try this code format if you want to send Mass Email using send_mass_mail() function.
from django.core.mail import send_mass_mail
subject = 'test subject'
message = 'test message'
from_email = 'from#from.com'
recipient_list = ['a#a.com', 'b#b.com', 'c#c.com']
messages = [(subject, message, from_email, [recipient]) for recipient in recipient_list]
send_mass_mail(messages)
I have a contact form on a website (a general form: name, email, subject, message) in which mails are sent using google apps smtp to the admins.
Currently if an administrator wants to reply to the mail directly selecting the reply option, the person's reply's To field will be filled by the sender's address automatically.
What I wan't to ask is, Is there any standardized way to pass on some additional info with the mail which would define any reply to the mail should go to this address instead of the sender's?
It does seems that there is a little chance for this option as it may lead to some problems due to spammers (They may define a custom reply field in their mail and a general user might not look where they are replying).
So as an alternative what I thought is to find a way to create a filter with sender's account which figures out the reply email address from the format and forwards the mail (Doesn't seems like a good solution and I have no idea how to achieve this).
I have tagged django, though this is not directly related with this, as I will finally implement this through django.
There are in fact standardized headers to specify response headers: http://cr.yp.to/immhf/response.html.
As far as implementing this in Django is concerned, the documentation contains an example:
from django.core.mail import EmailMessage
email = EmailMessage(
'Hello', # email subject
'Body goes here', # email body
'from#example.com', # sender address
['to1#example.com', 'to2#example.com'],
['bcc#example.com'],
headers={'Reply-To': 'another#example.com'},
)
This solved my problem.
Reply-To is a standard SMTP header.
I can't find a good reference for it at the moment, but it is mentioned in the Wikipedia article on Email.
Edit: Found it: RFC 5322, section 3.6.2
The RFC says you can specify multiple emails and that is what I was looking for. Came up with this:
from django.core.mail import EmailMessage
headers = {'Reply-To': 'email#one.com;email#two.com'}
msg = EmailMessage(subject, html_content, EMAIL_HOST_USER, email_list, headers=headers)
msg.content_subtype = "html"
msg.send()
Works like a charm. Note: EMAIL_HOST_USER is imported from your settings file as per Django doc email setup. More on this here, search for 'reply-to': https://docs.djangoproject.com/en/dev/topics/email/
Here is also how reply-to can be used
from django.core.mail import EmailMessage
email = EmailMessage(
'Hello',
'Body goes here',
'from#example.com',
['to1#example.com', 'to2#example.com'],
['bcc#example.com'],
reply_to=['another#example.com'],
headers={'Message-ID': 'foo'},
)
Read more at docs docs.djangoproject