0

I have created a function the would send mail to a particular user when redirected to a particular url. It was working until today. However, today when it gets redirected to the url, the email is displayed in the terminal and not in the inbox of reciever. I am attaching the mail function and settings in the code.

views.py

def mail(request,pk):
    pr = UserProfile.objects.get(pk=pk)
    subject = "Greetings"
    msg = "Congratulations for your success"
    to = 'urvi0728@gmail.com'
    res = send_mail(subject, msg, settings.EMAIL_HOST_USER, [to])
    if(res == 1):
        msg = "Mail Sent Successfuly"
    else:
        msg = "Mail could not be sent"
    return HttpResponse(msg)

settings.py

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '*****@gmail.com'
EMAIL_HOST_PASSWORD = '*********'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Srvastav4
  • 177
  • 2
  • 12
  • If the email is displaying in the console, it sounds as if you have `EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'` instead of `EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'`. I would add `from django.conf import settings; print(settings.EMAIL_BACKEND)` to the `mail` method to check what the value really is. – Alasdair Jun 18 '19 at 13:27
  • You've tagged your question `django-mailer`. Are you really using [django-mailer](https://github.com/pinax/django-mailer)? If so, then add more information about that to your question. – Alasdair Jun 18 '19 at 13:29
  • In the console it is displaying django.core.mail.backends.console.EmailBackend although I have set it to smtp.EmailBackend in settings. – Srvastav4 Jun 18 '19 at 13:34
  • Then you need to find out where you have set it to `django.core.mail.backends.console.EmailBackend`. Perhaps you repeat `EMAIL_BACKEND` further in your settings, or you haven't saved/checked in/deployed your changes, or you haven't restarted the server since switching to the smtp backend. – Alasdair Jun 18 '19 at 13:56

3 Answers3

0
email_msg = EmailMessage(subject="subject",
                                     body="content",
                                     to=["xyz@gmail.com"])
email_msg.send()

use EmailMassage

0

Try this one. This works for me. Your settings.py file seems correct.

from django.core.mail import EmailMessage

SUBJECT = "Welcome To my site"


def send_email(email, password):
    """ send email to new user with temp password"""

    msg = EmailMessage(SUBJECT, 'Your temporary login password here. {password}'.format(password=password), to=[email])
    msg.send()
    return True
Gautam Ajani
  • 71
  • 1
  • 3
0

Try this one

from django.core.mail import EmailMessage 

to_email = your_user_email
mail_subject = your_message_subject
message = your_content
send_message = EmailMessage(mail_subject, message, to=[to_email])
send_message.content_subtype = "html"
send_message.send()

As content you can add dictionaries or other data what you wish. If you want to send a template then you can use

render_to_string

from django.template.loader import render_to_string

message = render_to_string('path_to_your_template.html', {
        'domain':current_site,
        'email': request.user.email,
        'data' : your_data
    })
send_message = EmailMessage(mail_subject, message, to=[to_email])
send_message.content_subtype = "html"
send_message.send()