2017-05-08 86 views
0

我試圖從Google發送使用SMTP的電子郵件。我已經發送了一封電子郵件,將設置變量放在文件「settings.py」中,但我需要在視圖中配置這些變量並使用我的「配置」模型進行設置。Django:在視圖中設置電子郵件設置

這是我的模型代碼:

class Config(models.Model): 
    email_sender = models.CharField(max_length = 100, default = '', blank = True) 
    email_password = models.CharField(max_length = 30, default = '', blank = True) 
    smtp_server  = models.CharField(max_length = 100, default = '', blank = True) 
    smtp_port  = models.PositiveIntegerField(default = 587, blank = True) 

這是我的看法代碼:

def send_custom_mail(request): 
    config = Config.objects.get(pk = 1) 
    EMAIL_HOST = config.smtp_server 
    EMAIL_PORT = config.smtp_port 
    EMAIL_HOST_USER = config.email_sender 
    EMAIL_HOST_PASSWORD = config.email_password 
    EMAIL_USE_TLS = True 
    DEFAULT_FROM_EMAIL = EMAIL_HOST_USER 
    subject = 'test' 
    msg = '<p>Test: <strong>Mensaje</strong>></p>' 
    mail_from = config.email_sender 
    mail_to = ['{}'.format(email)] 
    from django.core.mail import EmailMessage 
    email2send = EmailMessage(subject, msg, mail_from, to=mail_to) 
    email2send.content_subtype = "html" # Main content is now text/html 
    email2send.send() 

我的問題:當我設置視圖中的變量電子郵件不會被髮送。

我需要配置這些變量dinamically所以我不能寫在setting.py文件中。

+0

嗯,不知道我明白你是如何在那裏代碼的設置。您是否試圖在視圖中設置Django配置變量(例如''EMAIL_HOST'')?如果是這樣,你得到的將不會工作,因爲你只是在本地範圍內設置變量,而不是修改Django設置。您的問題是關於如何修改視圖中的設置值? –

+0

@RodManning是的,這就是我想要做的。我想從視圖中設置這些變量以使其變爲動態。 – Nazkter

回答

1

我對有必要重寫視圖中的設置感到有點困惑;從未見過它的用例。

我通常會使用的策略是退回到較低級別的功能。

我建議你可以通過使用send_mail()函數和get_connection()來達到你所需要的。

沒有測試代碼,但認爲這將是這樣的:

from django.core import mail 

connection = mail.get_connection(
    host = config.smtp_server, 
    port = config.smtp_port, 
    username = config.email_sender, 
    password = config.email_password , 
    (... etc etc ...) 
) 

# Manually open the connection 
connection.open() 

# Send the message 
subject = 'test' 
msg = 'Test:Mensaje' 
mail_to = ['[email protected]'] 
email2send = EmailMessage(
    subject, 
    msg, t 
    o=mail_to 
    connection=connection 
) 

# Send the email using the custom connection 
email2send.send() 

# Close the connection 
connection.close() 

(注意connection=connection當EmailMessage是第一次設置通過自定義連接來發送電子郵件)

Django Email Backends docs很好地解釋了這一點,而且這似乎是一種比覆蓋設置更直接的方式。

+0

它工作正常,似乎是一個更好的方法 – Nazkter

0

我發現這個解決辦法,但我不知道這是否是對的形式給出這個問題的最好方法:在使用本

https://docs.djangoproject.com/en/1.11/_modules/django/test/utils/#override_settings

在我看來,我overrride設置變量:

from django.core.mail import EmailMessage 
from django.test.utils import override_settings 

@override_settings(EMAIL_HOST = Config.objects.get(pk = 1).smtp_server) 
@override_settings(EMAIL_HOST_USER = Config.objects.get(pk = 1).email_sender) 
@override_settings(EMAIL_PORT = Config.objects.get(pk = 1).smtp_port) 
def send_custom_mail(request): 
subject = 'test' 
    msg = 'Test:Mensaje' 
    mail_to = ['[email protected]'] 
    email2send = EmailMessage(subject, msg, to=mail_to) 
    email2send.content_subtype = "html" # Main content is now text/html 
    email2send.send() 

在這個問題中,我使用「django.test.utils」將裝飾器「@override_settings()」放在「send_custom_mail」函數之前。它覆蓋了設置,讓我從保存在名爲Config的模型中的郵件發送郵件。

實際上,我不確定它是否是最好的答案,因爲「django.test」模塊是用於單元測試,但我想分享我的解決方案。

+0

如果還有其他人可以回答正確的解決方法,我會很樂意爲他/她提供正確的答案。 – Nazkter