Django EmailMultiAlternatives sending to multiple "to" addresses - python

Is there a way in Django EmailMultiAlternatives that you can send to multiple "to" addresses, without having to send it again?
Have tried separating the email int the tuple by commas, but only the first recipient receives the emails.

A list of email addresses should work, as the example in the Django docs shows.
to_emails = ['alice#example.com', 'bob#example.com']
msg = EmailMultiAlternatives(subject, text_content, from_email, to_emails)
You might want to use bcc instead of to_emails to prevent users from seeing the other recipients of the email.
msg = EmailMultiAlternatives(subject, text_content, from_email, bcc=to_emails)

I'm using a for loop. I'm also using this to filter which users emails are sent to.
for user in users:
if user.is_active and user != request.user and user.last_login != None:
message = EmailMultiAlternatives(
subject = "your subject here",
from_email = settings.EMAIL_HOST_USER,
to = [user.email],
)
context = {'request':request, 'user':user, 'event':event}
html_template = get_template("notifications/new_event_email.html").render(context)
message.attach_alternative(html_template, "text/html")
message.send()
It might not be the best way, but it's working for me.

Related

How can I send a message to all posts in Django Allauth?

I am trying to send an email to all registered users in Django Allauth, but when I try to get a list of all users' emails and send it via the send_mail() method, I get an error :
too many values to unpack (expected 2)
When I manually specify the mail in the recipient_list list, then everything works. But I need it to be automatically sent to the emails of all users.
Tried to do through :
def email(request):
cleaned_data = super().clean()
title = cleaned_data.get('articles_title')
message = cleaned_data.get('articles_text')
recipient_list = User.objects.get('email')
email_from = 'mymail'
send_mail(title, message[:50], email_from, recipient_list)
return title, message
or iterate through the for loop:
def email(request):
cleaned_data = super().clean()
title = cleaned_data.get('articles_title')
message = cleaned_data.get('articles_text')
mails = User.objects.get('email')
recipient_list = []
for i in mails:
recipient_list.append(i)
email_from = 'mymail'
send_mail(title, message[:50], email_from, recipient_list)
return title, message
But nothing helps, does someone know some alternative method?

Send Django Email User Register Confirmation with with custom header name

I want to send Django email confirmation email, it shows the admin added email address (that we configure in settings.py) as the header of the newly arrived email, but I want to change it to something else my personal Header Title, like The Google Team, I want to replace my email as others showing in the picture.
https://prnt.sc/1mo2xeo
You can use "YOUR NAME <abc#domain.com>" as the email address you send from_email.
from django.core.mail import EmailMessage
email = EmailMessage()
email.subject = 'subject'
email.body = 'message'
email.from_email = 'YOUR NAME <abc#domain.com>'
email.to = ['to#domain.com']
email.send()

How to Link Sengrid to a Contact Form in Flask to Forward Email

I am writing a web application and am trying to use sendgrid to handle email delivery services.
I am writing the application in Flask.
Right now I have a contact form, and my problem is that the email only gets delivered if I send the e-mail from my pre-approved e-mail address with sendgrid. Obviously this is not good since everyone else who fills out the e-mail form will not have it go through.
Here's the code that I have:
ROUTE
app.config['MAIL_SERVER'] = 'smtp.sendgrid.net'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = 'apikey'
app.config['MAIL_PASSWORD'] = os.environ.get('SENDGRID_API_KEY')
app.config['MAIL_DEFAULT_SENDER'] = os.environ.get('MAIL_DEFAULT_SENDER')
#app.route('/contact', methods=['GET', 'POST'])
def contactpage():
if request.method == 'POST':
print("Message sent")
print(request.form.to_dict())
m = message(request.form.to_dict())
m.send()
title = "Jonathan Bechtel contact form"
description = "Contact Jonathan Bechtel with questions about teaching or working with him"
return render_template("contact.html",
title=title,
description=description)
Here's my code for actually sending the e-mail:
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
class message():
def __init__(self, message_data):
for key in message_data:
print(key, message_data[key])
setattr(self, key, message_data[key])
def send(self):
message = Mail(
from_email = self.email,
to_emails = 'jonathanbechtel#gmail.com',
subject = 'Sample Email Message',
html_content = f'<strong>From: {self.email}</strong><br><strong>Reason: {self.reason}</strong><br><strong>Message:</strong>{self.message}')
try:
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sg.send(message)
except Exception as e:
print(e)
If I set the from address in my contact form to my own the e-mail works fine. However, if I use any other one the message does not go through and generates a 403 status code.
I think this means that I'm just not using the correct part of the API but am not sure where to start.
Thank you.
Twilio SendGrid developer evangelist here.
SendGrid does not allow you to send emails from just any email address. I can see that in this case you are just trying to create a contact form that only sends emails to your email address so being able to send from any email address might be useful. But consider a form that allowed users to set the to and the from address and you can see how that might get abused.
You can read more about sender identity and SendGrid here.
In the meantime, for your use-case here is what I would suggest.
Set the from email to your pre-approved email address, include the user's email address in the body of the email, as you are doing already. Then add the user's email as a reply-to email as well, that way you can respond to the email and it will be sent straight to the user.
I believe you can set the reply-to with the mail object's reply_to method:
def send(self):
message = Mail(
from_email = APPROVED_SENDGRID_EMAIL,
to_emails = 'jonathanbechtel#gmail.com',
subject = 'Sample Email Message',
html_content = f'<strong>From: {self.email}</strong><br><strong>Reason: {self.reason}</strong><br><strong>Message:</strong>{self.message}')
# Set the reply-to email
message.reply_to(self.email)
try:
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sg.send(message)
except Exception as e:
print(e)
See the examples in the helper library for more detail.

Send E-mail with python

I need to always send the same file attached, but my signal is sending the full path of my server with my file.
e.g
/home/user/Sites/path_my_project/static/downloads/file.pdf
On my signal I get the file like this:
file_annex = ('path_my_file / myfile.pdf')
and sending the signal as follows:
EmailMessage message = (
to = str (to),
subject = subject,
template = template,
context = context,
reply_to = _from,
attachments = file_annex
)
this works, but sends with of full path,i not understand why.
I want only send the file.
According to the docs attachments must be: either email.MIMEBase.MIMEBase instances, or (filename, content, mimetype) triples.
So you could try something like:
file_annex = ('myfile.pdf', open('path/to/myfile.pdf').read(), 'application/pdf')
Then you must pass file_annex as a single item in a list:
message = EmailMessage(
...
attachments = [file_annex],
...
)
I prefer to use EmailMultiAlternatives to send emails with Django, it's easier for me, if you want to try it, just do:
from django.core.mail import EmailMultiAlternatives
subject = subject
from_email = 'from#example.com'
to = str(to)
text_content = 'This is an important message.' # This is text message
html_content = template # This is HTML message
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.attach_file('path_my_file / myfile.pdf')
msg.send()
Don't forget the brackets [ ] around to because this allways have to be a list, even it's only 1 e-mail address (it could be more)
There is text_content and html_content in case the receiver can't render html email
And you need in your settings.py your e-mail configuration:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'any_mail#gmail.com'
EMAIL_HOST_PASSWORD = 'email_pass'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
This configuration is good to send mails FROM gmail, if you use other email service, maybe you need to change some values.

Error: SMTPRecipientsRefused 553, '5.7.1 #while working on contact form in django

im trying to make a contact form in django 1.3, python 2.6.
Whats the reason of following error?
error:
SMTPRecipientsRefused at /contact/
{'test#test.megiteam.pl': (553, '5.7.1 <randomacc#hotmail.com>: Sender address
rejected: not owned by user test#test.megiteam.pl')}
my settings.py:
EMAIL_HOST = 'test.megiteam.pl'
EMAIL_HOST_USER = 'test#test.megiteam.pl'
EMAIL_HOST_PASSWORD = '###'
DEFAULT_FROM_EMAIL = 'test#test.megiteam.pl'
SERVER_EMAIL = 'test#test.megiteam.pl'
EMAIL_USE_TLS = True
edit: If any1 else was following djangobook, this is the part causing it:
send_mail(
request.POST['subject'],
request.POST['message'],
request.POST.get('email', 'noreply#example.com'), #get rid of 'email'
['siteowner#example.com'],
The explanation is in the error message. Your email host is rejecting the email because of the sender address randomacc#hotmail.com that you have taken from the contact form.
Instead, you should use your own email address as the sender address. You can use the reply_to option so that replies go to your user.
email = EmailMessage(
'Subject',
'Body goes here',
'test#test.megiteam.pl',
['to#example.com',],
reply_to='randomacc#hotmail.com',
)
email.send()
On Django 1.7 and earlier, there isn't a reply_to argument, but you can manually set a Reply-To header:
email = EmailMessage(
'Subject',
'Body goes here',
'test#test.megiteam.pl',
['to#example.com',],
headers = {'Reply-To': 'randomacc#hotmail.com'},
)
email.send()
Edit:
In the comments you asked how to include the sender's address in the message body. The message and from_email are just strings, so you can combine them however you want before you send the email.
Note that you shouldn't get the from_email argument from your cleaned_data. You know that the from_address should be test#test.megiteam.pl, so use that, or maybe import DEFAULT_FROM_EMAIL from your settings.
Note that if you create a message using EmailMessage as in my example above, and set the reply to header, then your email client should do the right thing when you hit the reply button. The example below uses send_mail to keep it similar to the code you linked to.
from django.conf import settings
...
if form.is_valid():
cd = form.cleaned_data
message = cd['message']
# construct the message body from the form's cleaned data
body = """\
from: %s
message: %s""" % (cd['email'], cd['message'])
send_mail(
cd['subject'],
body,
settings.DEFAULT_FROM_EMAIL, # use your email address, not the one from the form
['test#test.megiteam.pl'],
)

Categories