Django:在电子邮件上设置发件人地址 [英] Django: Setting the from address on an email

查看:108
本文介绍了Django:在电子邮件上设置发件人地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的用户能够输入其电子邮件地址消息,然后发送以发件人地址为自己的电子邮件地址的电子邮件。当前 EMAIL_HOST 是在我们自己的域上设置的,当电子邮件发送时,其发件人地址等于我们的 HOST_USER ,但如果不是其他任何内容,则不会。

I'd like for my users to be able to enter their email-address and message and then send the email with the 'from address' being their own email-address. Currently the EMAIL_HOST is set on our own domain and the emails arrives when it is sent with a "from address" equal to our HOST_USER, but not if it is anything else. Is this possible?

我们的设置:

EMAIL_HOST = 'smtp02.hostnet.nl'  
EMAIL_PORT = 587  
EMAIL_USE_TLS = True  
EMAIL_HOST_USER = "xxx"  
EMAIL_HOST_PASSWORD = "xxx"  
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'


推荐答案

如果允许用户要设置发件人地址,您可能会发现您的电子邮件已被反垃圾邮件措施阻止。

If you allow your users to set the from addresss, you may find that your emails are blocked by anti-spam measures.

一种更好的方法是将您控制的电子邮件地址用作发件人地址,然后在电子邮件中设置 reply_to 标头。然后,当收件人单击答复时,答复将转到用户的发件人地址。

A better approach would be to use an email address you control as the from address, and set the reply_to header on your email. Then, when the recipients click 'reply', the reply will go to the user's from address.

email = EmailMessage(
    'Hello',
    'Body goes here',
    'your-email-address@example.com',  # from address
    ['to1@example.com', 'to2@example.com'], # recipients
    reply_to=[user_from_address],  # reply to address set by your user
)
email.send()

这篇关于Django:在电子邮件上设置发件人地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆