简体   繁体   中英

Django: Setting the from address on an email

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?

Our settings:

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM