How to send Email in Django using office365 - python

I am working in a project where I need to send the email to the user who is filling the Email in the form. I am able to send email by using my Gmail Account details but while using outlook.365. This is the image with error that I am getting.
My requirement is :
Once users come in the registration form and fill the details as Name, Email, Mobile
Whatever Email is put there in the form, send the email with link to create new password.
Here is my code:
settings.py:
# Using outlook365:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = 'support#academic.com'
EMAIL_HOST = 'smtp.outlook.office365.com' # (also tried : smtp.office365.com and
outlook.office365.com)
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = 'My_Password'
# Using my gmail account:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = 'admin#gmail.com'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = 'My_Password'
views.py:
subject = "Academic - Create New Password"
message = "Hi %s! Please create your new Password here : http://127.0.0.1:8000/api/create-password" % firstname
send_mail(
subject, # Subject of the email
message, # Body or Message of the email
'support#academic.com', # from#gmail.com (admin#gmail.com for gmail account)
[email], # to#gmail.com # email that is filled in the form
)
Please guide me that what extra setting or configuration I need to send email using Outlook365.

Please put this in settings.py file.
SERVER_EMAIL = EMAIL_HOST_USER

Your settings look correct. Use EMAIL_HOST = "smtp.office365.com". Verify that you are using the correct credentials. Check that your outlook account is set up properly to send emails from support#academic.com.

Related

Dynamically Sending Email

I have email verification working in Django, it sucessfully sends an email to inbox when I click the submit button at the bottom of the signup page, but the only problem is that it does not yet have the function where it sends an email to the address of the user who just signed up, instead, it only sends the email to the email address hard coded in the views .py file .
Signup page:
[![Signup page][1]][1]
views.py
from django.core.mail import EmailMessage
email = EmailMessage(
'Test', #email subject
'Hello,'
' This is the Robo Depot team. We wanted to let you know your
account is confirmed.'
' Thank You for joining the Robo Depot userbase.', #email body
'*********', #not sure what this does
['******', '******',
'***********'], #recipients
reply_to=['hignuff#gmail.com'],
headers={'Message-ID': 'foo'},
)
email.send(fail_silently=False)
Settings.py
# Email
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_PORT = 587
EMAIL_HOST_USER = '**********'
EMAIL_HOST_PASSWORD = '*****'
register.html
<button id="signup-btn" type="submit" class="mt-3 btn btn-
primary" EmailMessage/cart/view.py">
Submit
</button>
I don't even know where to begin here. I think I need a get function to pull the value from the email address form field, but I have no clue how I would even get that. Anyone have any idea?
Where is the form data sent to?
that would be the action attribute in the <form> element.
say it's /api/register
you can set up a handler there to get the email address from the form and then call the emailing function with the address as a parameter.

Django didnt receive email

i am trying to send email from the website that i create. It does not display any error but there are no email that I received.
here is my setting code
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'my email's pswrd'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
Here is the function
from django.core.mail import send_mail
def sentmes(request):
if request.method == "POST":
name = request.POST.get('name')
email = request.POST.get('email')
subject = request.POST.get('subject')
message = request.POST.get('message')
data = {
'name': name,
'email': email,
'subject': subject,
'message': message
}
mail = '''
New message: {}
From: {}
'''.format(data['message'], data['email'])
send_mail(data['subject'], mail, ' ', ['myemail#gmail.com'], fail_silently=False)
return render(request, ' ')
this is the output that i get
[14/Jun/2021 06:52:15] "POST / HTTP/1.1" 200 18516
Where is my mistake. Hope someone can help. Thank you.
You should have a closer look at the user's guide.
https://docs.djangoproject.com/fr/3.2/topics/email/
A good check is to use the console to have a feedback, while testing.
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
and to test your parameters via the shell.
If it works, switch to smtp,
It it works, then implement your function.
As you use the smtp backend, make sure that all the parameters are the right ones.

How to send email to official account from logged in user in django?

I am trying to send email from the user who is logged in to an account which I have given bilalkhangood4#gmail.com. But I have typed it manually. There could be more accounts means more users from which I want to send. But how could I send email from just those users who are logged in? Basically it is an APP where a user will click on withdraw button to request and send mail to official admin account.
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'bilalkhangood4#gmail.com'
EMAIL_HOST_PASSWORD = ''
EMAIL_PORT = 587
ACCOUNT_EMAIL_VERIFICATION = 'none'
EMAIL_USE_SSL = False
views.py
from django.contrib.auth.models import User
def ViewSendEmail(request):
send_mail('Hello from Bilal Khan',
'Hello there, This is an automated message.',
'bilalkhangood6#gmail.com', #FROM
['bilalkhangood4#gmail.com'], #TO
fail_silently=False)
return render(request, 'nextone/email_send.html')
use request.user.email to access the email address of logged in user.
update your code with following:
def ViewSendEmail(request):
send_mail('Hello from Bilal Khan',
'Hello there, This is an automated message.',
request.user.email, #FROM
['bilalkhangood6#gmail.com'], #TO
fail_silently=False)
return render(request, 'nextone/email_send.html')

Django Reset PW emails not sending on AWS

Difficult to phrase this question and what I've done as I don't know where the error is occuring.
I'm using Django hosted on AWS Elastic Beanstalk and SES to send emails. (Only for password reset) However the password reset emails don't seem to be sending.
When I try to send an email as below however it works.
send_mail('Test', 'Email content', 'email#email.com',['email#email.com',])
Also locally the password reset email sends (its put in spam but that's a different issue)
My email settings are:
EMAIL_HOST = 'smtp.gmail.com' # mail service smtp
EMAIL_HOST_USER = 'email#email.com' # email id
EMAIL_HOST_PASSWORD = '****' #password
EMAIL_PORT = 587
# EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_ACCESS_KEY_ID = 'ACCESSKEY'
AWS_SECRET_ACCESS_KEY = 'SECRETKEY'
AWS_SES_REGION_NAME = 'eu-west-1'
AWS_SES_REGION_ENDPOINT = 'email.eu-west-1.amazonaws.com'
DEFAULT_FROM_EMAIL= 'email#email.com'
If it's relevant, anywhere I've written email#email.com is actually the same email.
Any help would be much appreciated.
Thanks

Django sending Email confusion

I've used one application with sending email to the user for notification. but in that application setting.py file contains some confusing terms like
in
setting file
EMAIL_USE_TLS = True
I'm not sure what this is and also
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'rquest186#gmail.com'
what is the variety in this two
and if host is declared here then
mail.py
def send_mail(title,message,reciver):
try:
mailS = 'smtp.gmail.com'
mailP = 587
mailUsr = "idefusiontest2015#gmail.com"
mailPass = "********"
# Create a text/plain message
msg = MIMEMultipart('alternative')
msg['Subject'] = title
msg['From'] = mailUsr
msg['To'] = reciver
html="<html><head></head><body><p>"+message.replace("\n","<br>")+"</p></body></html>"
part2 = MIMEText(html, 'html')
msg.attach(part2)
# Send the message via our own SMTP server, but don't include the
s = smtplib.SMTP(mailS,mailP)
s.ehlo()
s.starttls()
s.ehlo()
s.login(mailUsr,mailPass)
s.sendmail(mailUsr, [reciver], msg.as_string())
s.quit()
except Exception as e:
print(e)
in this
mailUsr = "idefusiontest2015#gmail.com" ???? what's this for ?
I'm new to this. and it's confusing for me.
thanks in adv.
You need to provide a gmail account to send mail from:
mailUsr = "idefusiontest2015#gmail.com"
mailPass = "IDEF2017"
MailUsr is the emailid and MailPass is passowrd of that account.
It is like for sending mail you are logging to gmail account using these emailid and password and then sending the mail.
So if you want to send then you need to use your emailid and password in place of that.
settings.py
You need to have these settings.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'username'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST is the smtp service you are using. In my case it is sendgrid in your case gmail.
EMAIL_HOST_USER is the username from that smtp service in your case your gmailid and EMAIL_HOST_PASSWORD password for that account.
I think it should be clear to you by now.
The the values from settings.py file will be automatically used while sending mail when if you are using django mail sending package like EmailMessage() for SendMail link
the second one that you are using is native python package . on that you have to explicitly provide the username and password and other configurations
sorry about the language .. :)

Categories