1

"passing get_connection to EmailMultiAlternatives"

I doing the connection dynamic for example:

settings.py

# EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_BACKEND = 'post_office.EmailBackend'
# EMAIL_HOST = 'smtp.office365.com'
# EMAIL_USE_TLS = True
# EMAIL_PORT = 587
# EMAIL_HOST_USER = '123s@123.sg'
# EMAIL_HOST_PASSWORD = '*********'

views.py

from django.core.mail import EmailMultiAlternatives, get_connection

def email_template_participants():
    
    connection = get_connection(host=arg.email_backend.email_host, use_tls=arg.email_backend.use_tls,
                                port=arg.email_backend.port, username=email_back, password=arg.host_password)
    .......
    email_message = EmailMultiAlternatives(subject, body, from_email, to_email, connection=connection)
    template = get_template('email_participants.html', using='post_office')
    html = template.render(context_html)
    email_message.attach_alternative(html, 'text/html')
    email_message.attach_file(attach_file(id)
    template.attach_related(email_message)
    email_message.send()
  • running this code : emails fail. On status: ConnectionRefusedError ( Unable to establish a connection as the target computer expressly denied that connection )

  • If I uncomment EMAIL_HOST, EMAIL_USE_TLS, etc the connection is denied, because they not always match, only works when they match, 1 in 15.

Otherwise. if I only change EMAIL_BACKEND to 'django.core.mail.backends.smtp.EmailBackend'. It works Perfect.

What can I do ????

I need all Benefits from post_office, (crons, mails listed, Requeue, retry_failed, etc)

0 Answers0