相关文章推荐
想发财的野马  ·  C# ...·  6 月前    · 
想发财的野马  ·  Error: Unable to ...·  7 月前    · 
想发财的野马  ·  golang ...·  7 月前    · 
想发财的野马  ·  python zmq socket timeout·  10 月前    · 
文雅的沙滩裤  ·  WKWebView拦截请求资源 ·  1小时前    · 
独立的眼镜  ·  如何连接Babelfish for RDS ...·  2 小时前    · 
发财的蛋挞  ·  Microsoft Azure Data ...·  2 小时前    · 
冷冷的投影仪  ·  Secure an ASP.NET ...·  3 小时前    · 
不羁的生姜  ·  PSPSDK 开发的时候出现 ...·  3 小时前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

SMTPSenderRefused at / (501, b'5.1.7 Invalid address [MAXPR01MB0396.INDPRD01.PROD.OUTLOOK.COM]', '=?utf-8?q?Your?=')

Ask Question

I am unable to send mail on django application using office365 settings my settings for the email service.

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.office365.com'
EMAIL_HOST_USER = "***********"
EMAIL_HOST_PASSWORD = '***********'
EMAIL_PORT = 587

My models.py

from django.db import models
from django import forms
from django.core.validators import RegexValidator
class ContactModel(models.Model):
    contact_name = models.CharField("Your Name ",max_length=20)
    contact_email = models.EmailField("Your Email ")
    content = models.TextField("Message ",max_length=1500)
    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$',
                                 message="Please Enter a valid Contact number(in +999999) format'. Up to 15 digits allowed Minimum 9 digits.")
    phone_number = models.CharField("Contact Number(in +9999 format)",validators=[phone_regex], max_length=17, blank=True)  # validators should be a list
    needacallback = models.BooleanField(" I want an Informational Phone Call")

My views.py

from django.shortcuts import render,render_to_response
from . import forms
from django.core.mail import EmailMessage
from django.shortcuts import redirect
from django.template.loader import get_template
from django.contrib.sites.shortcuts import get_current_site
from django.template.loader import render_to_string
from django.http import HttpResponse
from django.views.decorators.csrf import ensure_csrf_cookie
    def home(request):
        form = forms.ContactForm(request.POST or None, request.FILES or None)
        if request.method == 'POST':
            # form = form_class(data=request.POST)
            if form.is_valid():
                contactform = form.save(commit=False)
                contact_name = request.POST.get(
                    'contact_name'
                    , '')
                contact_email = request.POST.get(
                    'contact_email'
                    , '')
                needacallback = request.POST.get(
                    'needacallback'
                    , '')
                phone_number = request.POST.get(
                    'phone_number'
                    , '')
                form_content = request.POST.get('content', '')
                # Email the profile with the
                # contact information
                template = get_template('contact_template.html')
                context = {
                    'contact_name': contact_name,
                    'contact_email': contact_email,
                    'form_content': form_content,
                    'phone_number': phone_number,
                    'needacallback': needacallback,
                content = template.render(context)
                if needacallback:
                    subject = "This person requested for a call"
                else:
                    subject = "New Message from " + contact_name
                email = EmailMessage(
                    subject,
                    content,
                    "Contact me" + '',
                    ['myemail@gmail.com'],
                    headers={'Reply-To': contact_email}
                email.send()
                contactform.save()
                return redirect('thanks')
        return render(request, 'index.html', {
            'form': form,
    def thanks(request):
        return render(request, 'thanks.html')

The error I am facing is

SMTPSenderRefused at / (501, b'5.1.7 Invalid address [MAXPR01MB0396.INDPRD01.PROD.OUTLOOK.COM]', '=?utf-8?q?Your?=')

Template is a normal contact form which is formed by form.as_p()

What code are you using? Take a look at how to provide [mvce] and please add it to your question. What error do you receive? Settings are more or less correct. – Robert Dyjas Jul 26, 2018 at 9:34 Can I some how enable the connection on office settings because the same setup is working for my gmail in the case of gmail I got a mail saying you need to provide access but I have searched all of the office365 settings and cant really find anything useful – Pradeep Kumar Nalluri Jul 26, 2018 at 9:49 Can you try change "Contact me" + '', to 'youremailaddress@domain.com', where the address you provide is your O365 address? – Robert Dyjas Jul 26, 2018 at 11:27

As the error points out, you have incorrect sender address. According to the docs it's 3rd argument as in this example:

from django.core.mail import send_mail
send_mail(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,

Also this answer might be useful if you want to add the sender name to the script.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章