How to send emails in Django?
Sending emails in Django is very straightforward. You just need to configure the settings.py file in your Django project to specify your mail server, and then use the django.core.mail module built-in to Django to send emails. Here is a simple example:
Configure your email server information in the settings.py file.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.yourmailserver.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your-email@example.com'
EMAIL_HOST_PASSWORD = 'your-email-password'
2. Add the code for sending emails in your view or any other appropriate location.
from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
fail_silently=False,
)
In the code above, the send_mail function takes five parameters: subject, message content, sender email, recipient email list, and whether to handle failed email sending silently. You can adjust these parameters according to your needs.
This way you can send emails in Django. You can also add more parameters to the send_mail function to customize your email, such as adding attachments or using HTML format.