How to send django email via proxy with authentication - python

I am trying to send an email from a work PC that is behind a proxy.
In settings.py my code looks like this:
#email setup
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'username#gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
In views.py:
from django.core.mail import send_mail
def home(request):
context = RequestContext(request)
send_mail('test email', 'hello world', 'sender#gmail.com', ['receiver#email.com'], fail_silently=False)
return render_to_response('project/home.html', context)
This gives the error:
[Errno 10061] No connection could be made because the target machine actively refused it
How can I implement authentication with the proxy?

Related

Django email backend and smtp configuration

I'm trying to use my Zoho account within my django project, in order to receive emails via contact forms.
I also followed this guide: https://www.zoho.com/mail/help/zoho-smtp.html
In the 'settings.py' file I wrote:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtppro.zoho.eu'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '<domain name email>'
EMAIL_HOST_PASSWORD = '<password>'
and in views.py:
def home(request):
allTemplates = AllTemplates.objects.all()
if request.method == 'POST':
form = forms.ContactForm(request.POST)
if form.is_valid():
body = {
'name': form.cleaned_data['name'],
'surname': form.cleaned_data['surname'],
'from_email': form.cleaned_data['from_email'],
'message': form.cleaned_data['message'],
}
mail_body = "\n".join(body.values())
try:
send_mail("Generic contact", mail_body, '<domain name email>',
['<domain name email>'], fail_silently=False)
except BadHeaderError:
return HttpResponse('Ops, qualcosa è andato storto')
form = forms.ContactForm
context = {'form': form, 'allTemplates': allTemplates,
'allTemplates_length': len(allTemplates)}
return render(request, 'home.html', context)
N.B. in 'send_email' I entered my email address twice to test
I also tried to use ssl
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtppro.zoho.eu'
EMAIL_PORT = 465
EMAIL_USE_SSL = True
EMAIL_HOST_USER = '<domain name email>'
EMAIL_HOST_PASSWORD = '<password>'
but nothing, I don't receive any email.
Is there anyone who has already gone through it or who can direct me towards some document or guide to study?
Thank you very much in advance.
I use
EMAIL_PORT = 587
EMAIL_USE_TLS = True
my biggest pain was that all emails went to spam folder and I did not realize that for 2 hours.
test with local output to terminal:
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
test with local mail server to make sure the email is correctly created:
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST='localhost'
EMAIL_PORT=1025
and start local test mail server parallel to runserver in terminal window:
python -m smtpd -n -c DebuggingServer localhost:1025

Django SMTP [Errno 111] Connection refused

I am trying to send email from a web app using django and a sendgrid SMTP server. It works fine locally but when I try to use it on pythonanywhere I get
error: [Errno 111] Connection refused
The code from the relevant view is
def contact(request):
if request.method == 'POST':
form = EmailForm(request.POST)
if form.is_valid():
mail = form.cleaned_data
send_mail(
'Automated Enquiry',
'A user is interested in part ' + mail['part'] + '.Their message: ' + mail['msg'],
mail['email'],
['myemail#gmail.com'],
fail_silently = False,
)
return render(request, 'manager/error.html', {'msg': "Thanks, we will be in touch as soon as possible"})
else:
part = request.GET.get('part', '')
form = EmailForm()
return render(request, 'manager/contact.html', {'form': form, 'part':part})
And in my setting.py:
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'myuser'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
It didn't work from the console either.
Any help would be greatly appreciated.
free users on PythonAnywhere have restricted Internet access to a whitelist of sites, and only the HTTP/HTTPS protocols are allowed, so you cannot make SMTP connections to sendgrid from a free account.
you can use gmail from a free account, and pythonanywhere have instructions here.
or you can switch to using the sandgrid HTTP api: https://sendgrid.com/docs/Integrate/Frameworks/django.html

Using Django to send Gmail

This is very much potentially a duplicate question, but none of the other obvious duplicates have resolved the issue for me:
This is an inherited project.
My settings.py includes:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'my_username#gmail.com'
EMAIL_HOST_PASSWORD = 'my_password'
EMAIL_PORT = '587'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'my_username#gmail.com'
DEFAULT_FEEDBACK_EMAIL = 'my_username#gmail.com'
SERVER_EMAIL = 'my_username#gmail.com'
ACCOUNT_EMAIL_VERIFICATION = 'none'
The code that I'm trying to run is:
subject = 'Subject'
template = get_template('accounts/email-templates/email-activation.html').render(Context(ctx))
email = EmailMessage(subject, template, to=[send_to])
email.content_subtype = "html"
try:
email.send()
My error when trying repeatedly with python manage.py shell is:
gaierror: [Errno 8] nodename nor servname provided, or not known
My dns seems fine, sudo killall -HUP mDNSResponder and dscacheutil -flushcache have been run without success, but I'm hardly an expert on dns settings.
My hosts file is:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Advice appreciated!
You might need to generate an app password in Gmail or change the settings to allow less secure access. I don't think Gmail will allow you to just use your regular password in - what Google would consider - an insecure manner.
I had similar problem working with Gmail to send mails.
Try only the following codes to send emails. Hope it helps:
In settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "sender_email#gmail.com"
EMAIL_HOST_PASSWORD = "email_password"
EMAIL_PORT = 0
EMAIL_USE_TLS = True
Codes to send emails:
# Imports for sending emails:
from django.conf import settings
from django.core.mail import EmailMultiAlternatives, send_mail
emailFrom = [settings.EMAIL_HOST_USER]
emailTo = email_here
text_content = 'Your content here'
email_subject = "Subject here"
msg = EmailMultiAlternatives(email_subject, text_content, emailFrom, [emailTo], )
msg.send()
Also change the settings in the 'sender gmail account' to create a passage for the mail to be send from the sender to the receiver. Hope this helps.

Django Mailer not finding server

I'm trying to send an email through Django's wrapper.
Here are my relevant settings.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
The Email, which I am trying to isolate in the most basic form of a view:
from django.core.mail import send_mail
def index(request):
subject = 'Subject'
message = 'message'
from_email = settings.DEFAULT_FROM_EMAIL
send_mail(subject, message, from_email, ['email#example.com'])
return render(request, "index.html")
All of the emails and password are legitimate. When I execute the code, I get thrown an error message:
SMTPAuthenticationError at /....*Link to sign into my account*
Please log in via your web browser and\n5.7.14 then try again
I did that but continue to get the same message. The password I give in the app is correct. Is there anything I need to configure in my gmail account?
Change EMAIL_HOST = 'smtp.gmail.com ' toEMAIL_HOST = 'smtp.gmail.com' and I bet your problem will disappear :)
EDIT #1
You are running into authentication issues as EMAIL_USE_TLS is True and Gmail only requires TLS connections for SMTP on port 587. Change to EMAIL_PORT = 587 and you should bypass the issue.
EDIT #2
The error you see can be fixed through your Gmail settings. See -
Django SMTPAuthenticationError

in send mail from is not working in django

i am trying to send the mail in django. mail is going properly but mail is going by EMAIL_HOST_USER. Want to send the mail using from i.e. from some other email address.
settings.py
EMAIL_HOST ='smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'you#everycrave.me'
EMAIL_HOST_PASSWORD = '*********'
EMAIL_USE_TLS = True
in view:
text="hi this is test mail"
send_mail('Codeville Signup', text.decode(), 'gaurav#everycrave.me', ['manish#everycrave.me', 'jagat#everycrave.me'], fail_silently=False)
i want to send the mail from "gaurav#everycrave.me" but mail is getting sent by "you#everycrave.me"
How can i overcome this problem. And i dont want to change EMAIL_HOST_USER mail address.
Guide me through this
You can refer EmailBackend for sending email through multiple SMTP in Django this question or
in your view you have to write this code, from where you are sending the email.
from django.core.mail import get_connection, send_mail
from django.core.mail.message import EmailMessage
#TODO: Insert clever settings mechanism
my_host = 'smtp.gmail.com'
my_port = 587
my_username = 'your email address'
my_password = 'password'
my_use_tls = True
connection = get_connection(host=my_host,
port=my_port,
username=my_username,
password=my_password,
user_tls=my_use_tls)
EmailMessage('Test subject', 'test message', 'from_email', ['to'], connection = connection).send(fail_silently=False)
Check this.

Categories

Resources