3

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 '18 at 09: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 '18 at 09:49
  • Can you try change `"Contact me" + '',` to `'youremailaddress@domain.com',` where the address you provide is your O365 address? – Robert Dyjas Jul 26 '18 at 11:27
  • Yeah it is working perfectly thanks alot for the help – Pradeep Kumar Nalluri Jul 26 '18 at 11:52
  • You're welcome. I'll add an answer in a second so you can accept it. – Robert Dyjas Jul 26 '18 at 11:53

1 Answers1

2

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.

Robert Dyjas
  • 4,979
  • 3
  • 19
  • 34