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
Related
I just setup a Google workspace. One of the accounts has an alias: info#domain.com. I’d like to send all emails from that alias. I know I have to authenticate with the actual account email address through EMAIL_HOST_USER (which I have done), but how do I force Django AllAuth to send emails from the alias email? Is this something that can be achieved by overriding AllAuth views and using send_mail()? Any help would be appreciated.
You can use send_email() for that all you need to do is pass from_email argruament in send_email() function.
like this:
send_mail(
'Subject here',
'Here is the message.',
'from#example.com',
['to#example.com'],
fail_silently=False,
)
You can go through the documentation here: https://docs.djangoproject.com/en/4.1/topics/email/#send-mail
sending an email from one email is working fine, but if we want send
an email to user from multiple emails(i have 3 emails from that i need
to choose any one and receiver get email whichever i chose ) so please
help me I'm new to Django and Python
This is how to send multiple email in django.
from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
'from#example.com',
['to#example.com', 'anotherTo#example.com'],
fail_silently=False,
)
Here's the django documentation. https://docs.djangoproject.com/en/2.2/topics/email/
I hope this help. Also beginner in django :)
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 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 at shantiyoga.ca/contact
With the view
def contact(request):
template = 'contact.html'
form = ContactForm(request.POST or None)
if form.is_valid(): # All validation rules pass
subject = form.cleaned_data['subject']
message = "%s\n\n%s" % (form.cleaned_data['message'], form.cleaned_data['sender'])
cc_myself = form.cleaned_data['cc_myself']
recipients = ['contact#shantiyoga.ca']
sender = form.cleaned_data['sender']
if cc_myself:
recipients.append(sender)
headers = {'Reply-To': form.cleaned_data['sender']}
from django.core.mail import send_mail
send_mail(subject,message,sender,recipients,headers)
return redirect('/thanks/')
return render_to_response(template, {'form': form, 'current':'contact'}, context_instance=RequestContext(request))
Which works fairly well. I'm not terribly sophisticated with Django and my Python skills are not quite up to snuff, so please bear with me if I step through this in a basic fashion.
I would like to clarify that there is no way for the form recipient (contact#shantiyoga.ca) to receive the contact form from the value of the email field (user entered). It will always be sent by the authenticated email in my settings.py, which at this point is my personal email?
A user fills out the contact form and hits submit, an email is sent to contact#shantiyoga.ca from my personal email, and if the user decides to cc themself, a copy of the email is sent to them, also from my personal email.
This is not ideal, should I create an email like contactform#shantiyoga.ca for my settings.py to send the email from?
Also, the headers = {'Reply-To': form.cleaned_data['sender']} does not appear to be doing anything and I can't seem to find documentation describing its proper usage, has anyone had success using this technique?
Thank you for your time,
Noah
I would like to clarify that there is no way for the form recipient (contact#shantiyoga.ca) to receive the contact form from the value of the email field (user entered). It will always be sent by the authenticated email in my settings.py, which at this point is my personal email?
You're setting the sender of the email to sender but using EMAIL_HOST, correct? If this is the case, be careful that your SMTP account will let you send email from users other than your domain. Normally, providing you have an authenticated account on that server, you'll be able to set the From: field to whatever you like.
So in short, this email will hit your inbox appearing to be from the sender variable. So when you hit reply, you'll be able to email them back, which is I think what you want. However, it will be sent using the SMTP server whose authentication details you provide. There is a disconnect between being able to send email (SMTP) and being able to receive it, which I think has got you confused.
I've never tried it, but to add extra headers I believe you need to use the EmailMessage object. According to the docs, you would:
e = EmailMessage(headers={'Reply-To':...
e.send()
Be careful doing this; specifically, be careful to strip out newlines in the reply to field. I do not know if the default clean methods would do this.
Finally, there isn't much wrong with your django/python at all. The only thing I'd say, by way of awareness, is not to use this:
return redirect('/thanks/')
but instead:
return HttpResponseRedirect(reverse('myapp.views.method',args=tuple,kwargs=dict))
This gives you the ability to not hardcode urls into your application. The reverse method will look them up from urls.py for you, so you can move your application around/change urls and it will still work.
Here is signature of send_mail from django documentation:
send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None)
As you can see it does not have headers argument.
You will need to use EmailMessage object directly.
It would also be nice to remove email-formatting from view. I would write everything something more like this:
from django.core.mail.message import EmailMessage
from django.conf import settings
class ContactsEmailMessage(EmailMessage):
def __init__(self, sender, subject, message, cc_myself):
super(ContactEmailMessage, self).__init__(
subject=subject,
body=self._get_message(self, sender, message),
from=settings.DEFAULT_FROM_EMAIL,
to=settings.CONTACT_RECIPIENTS,
headers=self._get_headers(sender),
cc=(sender, ) if cc_myself else None
)
def _format_message(self, sender, message):
return "%s\n\n%s" % (sender, message)
def _get_headers(self, sender):
return {
'reply-to': sender
}
Now you can write clean and easy to read view:
from myproject.mail.message import ContactsEmailMessage, BadHeaderError
from django.core.exceptions import SuspiciousOperation
def contact(request):
...
form = ContactForm(request.POST or None)
if form.is_valid(): # All validation rules pass
try:
message = ContactsEmailMessage(**form.cleaned_data)
message.send()
except BadHeaderError:
# django will raise this error if user will try to pass suspicious new-line
# characters to "sender" or other fields. This is safe-guard from
# header injections
raise SuspiciousOperation()
return redirect('/thanks/')
...