【问题标题】:How to use multiple SMTP in django_mail_admin (Gmail & Outlook)如何在 django_mail_admin (Gmail & Outlook) 中使用多个 SMTP
【发布时间】:2022-06-13 19:10:05
【问题描述】:

我们在项目中使用 django_mail_admin 来发送和接收电子邮件。 我们的要求是在 Gmail 和 Outlook 两个帐户上发送电子邮件。 1- Gmail 帐户将使用 Gmail SMTP 向 Gmail 帐户发送电子邮件 2- Outlook 帐户将使用 Outlook SMTP 向 Outlook 帐户发送电子邮件。 注意:切换 SMTP 是根据 Gmail 和 Outlook 更改 SMTP 的功能。 使用单个 Gmail 帐户:- Gmail 正在从 Gmail 向 Gmail 帐户、发件人和收件人发送电子邮件,工作正常。 使用单个 Outlook 帐户:- Outlook 正在从 Outlook 向 Outlook 帐户、发件人和收件人发送电子邮件,工作正常。 同时使用 Gmail 和 Outlook 当我们同时使用 Gmail 和 Outlook 并将 SMTP Gmail 切换到 Outlook 时,我们会遇到问题(发件人不变,Gmail 接收来自 Gmail 的电子邮件,但 Outlook 接收来自 Gmail 的电子邮件)

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: django email admin


【解决方案1】:
Use multiple SMTP by adding below code in settings.py

    DJANGO_MAIL_ADMIN = {
        'BACKENDS': {
            'default': 'django_mail_admin.backends.CustomEmailBackend',
            'smtp': 'django.core.mail.backends.smtp.EmailBackend',
        }
    }

    EMAIL_HOST = "smtp.gmail.com"
    EMAIL_HOST_USER = "any"
    EMAIL_HOST_PASSWORD = "any"
    EMAIL_PORT = 587
    EMAIL_USE_TLS = True


use 'default' for outlook and 'smtp' for gmail then it work for both 

# If you omit `backend_alias` argument, `default` will be used
mail.send(
    'from@example.com',
    ['recipient@example.com'],
    subject='Hello',
)

# If you want to send using `smtp` backend
mail.send(
    'from@example.com',
    ['recipient@example.com'],
    subject='Hello',
    backend='smtp',
)

【讨论】:

    猜你喜欢
    • 2011-12-20
    • 2022-06-17
    • 1970-01-01
    • 2015-08-19
    • 2013-06-21
    • 2012-01-24
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多