Send E-mail with python - 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.

Related

Can i use the views.py file to actually send emails?

I am trying to create a contact form in Django that actually sends emails for real. Can i put all the email configs in the views.py file itself? I want to do that because i want only the legitimate owners of emails to actually send emails. I do not people to send me emails using their friends email.
Yes obviously you can, but make sure you have your email credentials stored in your settings.py file safely
What is ideal is to save your email credentials as environment variables
In your settings.py File
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = "from#example.com"
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# If you are using any other smtp host.
# Search documentation for other smtp host name and port number
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = "from#example.com"
EMAIL_HOST_PASSWORD = "SUPER_SECRET_PASSWORD"
In your view that you want to use to send email
views.py
from django.core.mail import EmailMessage
def email_send_view(request):
if request.method == "POST":
# Get email information via post request
to_email = request.POST.get("to", "")
subject = request.POST.get("subject", "")
message = request.POST.get("message", "")
if to_email and message:
email = EmailMessage(subject=subject,body=body,to=[to])
email.send()
# Complete your view
# ...
return redirect("REDIRECT_VIEW")
If you want to send html template or image using email Email Template via your django app
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from YourProject.settings import EMAIL_HOST_USER
def email_send_view(request):
if request.method == "POST":
# Get email information via post request
to_email = request.POST.get("to", "")
subject = request.POST.get("subject", "")
message = request.POST.get("message", "")
if to_email and message:
# Gets HTML from template
# Make sure in this case you have
# html template saved in your template directory
html_message = render_to_string('template.html',
{'context': 'Special Message'})
# Creates HTML Email
email = EmailMultiAlternatives(subject,
from_email=EMAIL_HOST_USER,
to=[to])
# Send Email
email.attach_alternative(html_message, "text/html")
email.send()
# Complete your view
# ...
return redirect("REDIRECT_VIEW")

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.

Django smtplib.SMTPSenderRefused: (530, b'5.5.1 Authentication Required. ...)

I've been trying to let users send email to my personal account via form. At first it was showing me
socket.gaierror: [Errno 11004] getaddrinfo failed
error which I solved by installing and configuring hMailServer in my device. Now when I try to send mail by my friend's email it send email by my own account instead. When I removed MAIL_HOST_USER = 'my_email' from settings file, it shows
smtplib.SMTPSenderRefused
error as mentioned in title. Any suggestions or related links will be appreciated.
My code is:
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_PORT = 25
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'my_email'
EMAIL_HOST_PASSWORD = 'my_password'
EMAIL_USE_TLS = True
views.py
class contact(View):
def post(self, request):
form = ContactForm(request.POST)
if form.is_valid():
if send_mail(subject = form.cleaned_data['title'], message = form.cleaned_data['content'], from_email = form.cleaned_data['contact_email'], recipient_list = ['my_email'], fail_silently=False):
return render(request, 'index.html', {'form': form, 'message': 'Message delivered successfully', 'error':'', 'email_sent': 1 })
else:
return render(request, 'index.html', {'form': ContactForm(), 'error': form.errors})
Gmail, like more or less any other email provider or correctly configured mail server, does not allow you to set an arbitrary From address. An email server that allows this would be abused by spammers and quickly find itself on a spam blacklist.
What you can do is set my_email as sender and add a Reply-To header with the email address provided through the form.
You have to create an EmailMessage instance and call send():
from django.core.mail import EmailMessage
...
email = EmailMessage(
subject=form.cleaned_data['title'],
message=form.cleaned_data['content'],
from_email='my_email',
recipient_list=['my_email'],
reply_to=form.cleaned_data['contact_email']
)
sent = email.send(fail_silently=False)
if sent:
return render(request, 'index.html', {'form': form, 'message': 'Message delivered successfully', 'error':'', 'email_sent': 1 })
Note that Gmail has some limits on the number of emails you can send this way. Make sure you don't accidentally block your account.
N.B., you can configure gmail to relay messages on behalf of any sending address in a google apps organization (powered by gmail), but as Daniel Hepper noted, this is not the default for a personal gmail address. That said, I would wager that the OP's problem is likely that the port setting is incorrect. In gmail, the port setting should be 465 or 587 for TLS-using SMTP.

Django EmailMultiAlternatives sending to multiple "to" addresses

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.

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