Showing posts with label MailServer. Show all posts
Showing posts with label MailServer. Show all posts

Monday, 24 October 2022

django send email using SMTP mail server including no module names 'django_smtp_ssl.

 django, write send email with scheduled times like cron :

https://stackoverflow.com/questions/63552113/gmail-schedule-send-email-in-django


EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your_email'
EMAIL_HOST_PASSWORD = 'your password'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_PORT = 465

Then in your tasks.py you can add the function for scheduling emails as follows:

from django.template.loader import render_to_string
from django.core.mail import EmailMessage


@periodic_task(
run_every=(crontab(hour=3, minute=34)), #runs exactly at 3:34am every day
name="Dispatch_scheduled_mail",
reject_on_worker_lost=True,
ignore_result=True)
def schedule_mail():
    message = render_to_string('app/schedule_mail.html')
    mail_subject = 'Scheduled Email'
    to_email = getmail
    email = EmailMessage(mail_subject, message, to=[to_email])
    email.send()

django send email offical documentation: 
https://docs.djangoproject.com/en/4.1/topics/email/

Mail is sent using the SMTP host and port specified in the EMAIL_HOST and EMAIL_PORT settings. The EMAIL_HOST_USER and EMAIL_HOST_PASSWORD settings, if set, are used to authenticate to the SMTP server, and the EMAIL_USE_TLS and EMAIL_USE_SSL settings control whether a secure connection is used.

django ModuleNotFoundError: No module named 'django_smtp_ssl'

https://stackoverflow.com/questions/45817296/django-1-8-6-modulenotfounderror-no-module-named-django-smtp-ssl

pip install django-smtp-ssl

For docker, add this in requirements.txt
django-smtp-ssl>=1.0,<2.0
https://pypi.org/project/django-smtp-ssl/#history

post fix admin docker compose set up with mail server

 post fix admin is a management of domain and mail boxes, it needs a mail server to work : 

https://linuxize.com/post/set-up-an-email-server-with-postfixadmin/



before begin, its important to set up MX record for desired mail server at DNS level :

  • A record, to point your system’s FQDN (hostname) to your mail server IPv4 address.
mail.linuxize.com. 3600 IN A   23.45.67.89
The FQDN consists of two parts, the hostname and the domain name.
  • MX record, to specify which mail server is responsible for accepting email messages on behalf of a recipient’s domain. In our case we want all emails sent to @linuxize.com email addresses to be accepted by the mail.linuxize.com mail server.
linuxize.com.      3600 IN MX  0 mail.linuxize.com.
  • SPF record, which is used to verify which mail servers are approved to send email on behalf of a given domain. In the example below we are approving the domain mail servers (mx) and if the SPF check fails, the result will be a soft failure (~all):
linuxize.com.      3600 IN TXT "v=spf1 mx ~all"

set up mail server with docker mail server

 docker mail server :

https://github.com/docker-mailserver/docker-mailserver



guide :

https://betterprogramming.pub/how-to-set-up-a-mailserver-within-a-docker-swarm-e6e9192200c4



Most important thing before starting is to set up mail record for desired domain:

https://linuxize.com/post/set-up-an-email-server-with-postfixadmin/


  • A record, to point your system’s FQDN (hostname) to your mail server IPv4 address.
mail.linuxize.com. 3600 IN A   23.45.67.89
The FQDN consists of two parts, the hostname and the domain name.
  • MX record, to specify which mail server is responsible for accepting email messages on behalf of a recipient’s domain. In our case we want all emails sent to @linuxize.com email addresses to be accepted by the mail.linuxize.com mail server.
linuxize.com.      3600 IN MX  0 mail.linuxize.com.
  • SPF record, which is used to verify which mail servers are approved to send email on behalf of a given domain. In the example below we are approving the domain mail servers (mx) and if the SPF check fails, the result will be a soft failure (~all):
linuxize.com.      3600 IN TXT "v=spf1 mx ~all"