1

“将 get_connection 传递给 EmailMultiAlternatives”

我做连接动态例如:

设置.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 = '*********'

视图.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()
  • 运行此代码:电子邮件失败。开启状态:ConnectionRefusedError(无法建立连接,因为目标计算机明确拒绝了该连接)

  • 如果我取消注释 EMAIL_HOST、EMAIL_USE_TLS 等,则连接被拒绝,因为它们并不总是匹配,只有在它们匹配时才有效,15 分之一。

否则。如果我只将 EMAIL_BACKEND 更改为 'django.core.mail.backends.smtp.EmailBackend'。它工作完美。

我能做些什么 ????

我需要 post_office 的所有好处(crons、列出的邮件、重新排队、retry_failed 等)

4

0 回答 0