Skip to content
Advertisement

How to send an email to currently logged in user Django

I am currently trying to set up a view so when the user visits the path /clip it will send an email to the user’s inbox.

Eg. I visit path, email turns up in my inbox.

I am using this:

from django.core.mail import send_mail

def clippy(request):
  current_user = request.user
  subject = "test"
  message = "testing"
  recipient_list = [current_user.email]
  send_mail(subject, message, recipient_list)

I’m using 3.0.4 and get this error when I visit the path:

send_mail() missing 1 required positional argument: 'recipient_list'

Can anyone help? Thanks

EDIT: I have used the answer by reza heydari and it fixes this issue, now I get the following:

TypeError at /clip/
send_mail() missing 1 required positional argument: 'from_email'
@login_required()
def clippyemail(request):
  current_user = request.user
  subject  =  'Clippy here', 
  message = 'Hi! I am clippy! You resserected me somehow so thanks!',
  recipient_list =  [current_user.email,  ]
  send_mail(subject, message,  recipient_list=recipient_list)

is there any way I can set it up so it just sends the email using the SMTP settings in settings.py?

Advertisement

Answer

The 3rd arg of send_mail is from_email based on docs Change your code like that:

send_mail(subject, message, from_email="example@email.com", recipient_list=recipient_list)

And also add

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

For your local to recieve email in terminal

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement