Im using django_yubin to send mails via its mail queue.
However, there are a few instances where I want to send the mail immediately without putting it on the queue.
For example, when a user registers or resets their password. Or certain admin emails.
I tried using Django's built in email system by
from django.core.mail import send_mail as send_mail_core
and then using the send_mail_core() function.
This didnt work - looks like the send_mail in django.core.mail gets overridden by yubin
Thanks for your help
Why would you even try to use send_mail_core if you can select the mail backend inside of send_mail function
def send_mail(subject, message, from_email, recipient_list,
fail_silently=False, auth_user=None, auth_password=None,
connection=None, html_message=None):
connection: The optional email backend to use to send the mail. If
unspecified, an instance of the default backend will be used. See the
documentation on Email backends for more details.
Related
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 to send bulk email in django, the email template will be will be customized and the some data in the template will be coming from db. i twas using django notification but it can only send email to the registered users. I have to send emails to the non-registered users. there will be five email template the user can select any one and the email has to be sent.
For ex. An invitation to the event to the group of non-registered users. user will enter email ids, and will do a bulk send. which django package can i use to achieve the same.
You can use django's default sending multiple email system. From here: https://docs.djangoproject.com/en/dev/topics/email/#sending-multiple-emails
You can try like this:
from django.core import mail
connection = mail.get_connection()
connection.open()
reciever_list= ['aa#bb.cc', 'dd#ee.ff'] #extend this list according to your requirement
email1 = mail.EmailMessage('Hello', 'Body goes here', 'from#example.com',
reciever_list, connection=connection)
email1.send()
connection.close()
For bulk email reference, you can check this so answer: How does one send an email to 10,000 users in Django?
Edit
From this stackoverflow answer, you can send emails with template. If you use django 1.7, html_message can be added as perameter of send_mail(). Details here.
By the way, for mass email handling, django has send_mass_mail() method.
I am trying to send account activation link in my Django project, but it cannot work. So I try the very basic send_mail() function in shell and see if it is sending.
in the settin.py
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend', # necessary for django.auth
'survey.modelbackend.EmailBackend' # custom backend to authenticate using the email field
)
#settings for the email
EMAIL_HOST = 'localhost'
EMAIL_PORT = 25
DEFAULT_FROM_EMAIL = 'webmaster#localhost'
(I am using all the default values in the settings for email)
in the shell I typed
>>> from django.core.mail import send_mail
>>> send_mail('Subject here', 'Here is the message.', 'from#example.com',
... ['myemailaddress#gmail.com'], fail_silently=False)
and it return 1 after the above line, but I cannot get the email in my own email
Can anyone help with this problem and explain why the email is not sent to my email?
Thank you very much.
I'm going to go ahead and say that the mail is probably being sent, but it is hitting your spam filter.
Unless you have something other than django.core.mail.backends.smtp.EmailBackend set as your settings.EMAIL_BACKEND, you will only get a return of True (or 1) if the backend has successfully connected to the server and told the server to send the message. If fail_silently=False, that means that send_mail will either return True or raise an exception. This means that the error either lies in your SMTP or it is being sent directly to "spam".
But, in the off chance that you have already checked spam, there are ways to make an SMTP server fail silently. Check out this article for Sendmail (one of the most common *nix applications), or this one for Mercury (one of the SMTP servers available for Windows). If you're using IIS, this site looks like it addresses some potential issues.
Hy, I've tried asking and this can successfully send email without any setting in the settings.py
first, add these two imports
import smtplib
from email.mime.text import MIMEText as text
then the code to send email could be like this
def send_email(sender,receiver,message,subject):
sender = sender
receivers = receiver
m = text(message)
m['Subject'] = subject
m['From'] = sender
m['To'] = receiver
# message = message
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, str(m))
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
This works just as expected.
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