Skip to main content
 首页 » 编程设计

python之在 Django 中通过 Amazon SES 发送电子邮件时如何更改显示名称

2024年08月26日38JeffreyZhao

目前我有一个在 Django 中使用 Amazon SES 发送电子邮件的有效实现。

我的设置如下所示。在 settings.py 中我有:

EMAIL_HOST = 'email-smtp....' 
EMAIL_PORT = 25 
EMAIL_HOST_USER = 'my-email-host-user' 
EMAIL_HOST_PASSWORD = 'my-email-host-password' 
EMAIL_USE_TLS = True 

在我看来我有:

from django.shortcuts import render 
from django.http import HttpResponse 
from django.conf import settings 
from django.contrib import messages 
from django.core.mail import send_mail 
from django.core.mail import EmailMessage 
 
def index(request): 
    email_message = EmailMessage('This is a title', 'This is a message body', 'FromEmail@example.com', ['ToEmail@example.com']) 
    email_message.send() 
    return HttpResponse("You just sent an email message.") 

当我打开消息时,我在标题中看到了这个:

FromEmail@example.com via amazonses.com  

我想自定义它,这样我就可以做这样的事情:

UserFirstName UserLastName via amazonses.com 

我该怎么做?

请您参考如下方法:

我怀疑你可以,但你应该可以使用这样的地址:

email_message = EmailMessage('This is a title', 'This is a message body', 'UserFirstName UserLastName <FromEmail@example.com>', ['ToEmail@example.com']) 

这会导调用子邮件客户端显示如下发件人地址:

UserFirstName UserLastName <FromEmail@example.com>