我想发送HTML电子邮件,使用像这样的Django模板:
<html>
<body>
hello <strong>{{username}}</strong>
your account activated.
<img src="mysite.com/logo.gif" />
</body>
我找不到send_mail
的任何内容,django-mailer只发送HTML模板,没有动态数据。
如何使用Django的模板引擎生成电子邮件?
答案 0 :(得分:345)
从the docs,要发送HTML电子邮件,您要使用其他内容类型,如下所示:
from django.core.mail import EmailMultiAlternatives
subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
您可能需要两个电子邮件模板 - 一个类似于此的纯文本模板,存储在email.txt
下的模板目录中:
Hello {{ username }} - your account is activated.
和HTMLy版本,存储在email.html
:
Hello <strong>{{ username }}</strong> - your account is activated.
然后,您可以使用get_template
使用这两个模板发送电子邮件,如下所示:
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
from django.template import Context
plaintext = get_template('email.txt')
htmly = get_template('email.html')
d = Context({ 'username': username })
subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = plaintext.render(d)
html_content = htmly.render(d)
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
答案 1 :(得分:199)
男孩和女孩!
由于Django在send_email方法中的1.7,因此添加了html_message
参数。
html_message:如果提供了html_message,则生成的电子邮件将是 包含邮件作为文本/纯文本内容的多部分/备用电子邮件 type和html_message作为text / html内容类型。
所以你可以:
from django.core.mail import send_mail
from django.template.loader import render_to_string
msg_plain = render_to_string('templates/email.txt', {'some_params': some_params})
msg_html = render_to_string('templates/email.html', {'some_params': some_params})
send_mail(
'email title',
msg_plain,
'some@sender.com',
['some@receiver.com'],
html_message=msg_html,
)
答案 2 :(得分:26)
我已经使django-templated-email努力解决这个问题,受到这个解决方案的启发(并且需要在某些时候从使用django模板切换到使用mailchimp等模板进行事务处理,我自己项目的模板化电子邮件)。尽管如此,它仍然是一项正在进行的工作,但对于上面的示例,您可以这样做:
from templated_email import send_templated_mail
send_templated_mail(
'email',
'from@example.com',
['to@example.com'],
{ 'username':username }
)
在settings.py中添加以下内容(以完成示例):
TEMPLATED_EMAIL_DJANGO_SUBJECTS = {'email':'hello',}
这将分别在普通django模板目录/加载器中自动查找名为“templated_email / email.txt”和“templated_email / email.html”的模板,用于普通django模板dirs / loaders(抱怨如果找不到至少一个那些)。
答案 3 :(得分:14)
使用EmailMultiAlternatives和render_to_string来使用两个替代模板(一个是纯文本,一个是html):
from django.core.mail import EmailMultiAlternatives
from django.template import Context
from django.template.loader import render_to_string
c = Context({'username': username})
text_content = render_to_string('mail/email.txt', c)
html_content = render_to_string('mail/email.html', c)
email = EmailMultiAlternatives('Subject', text_content)
email.attach_alternative(html_content, "text/html")
email.to = ['to@example.com']
email.send()
答案 4 :(得分:9)
我知道这是一个古老的问题,但我也知道有些人和我一样,并且总是在寻找最新的答案,因为如果不及时更新,旧的答案有时会包含已弃用的信息。 / p>
现在是2020年1月,我正在使用Django 2.2.6和Python 3.7
注意:我使用的是DJANGO REST FRAMEWORK,下面用于发送电子邮件的代码位于我的views.py
的{{3}}中
所以在阅读了多个不错的答案后,这就是我所做的。
from django.template.loader import render_to_string
from django.core.mail import EmailMultiAlternatives
def send_receipt_to_email(self, request):
emailSubject = "Subject"
emailOfSender = "email@domain.com"
emailOfRecipient = 'xyz@domain.com'
context = ({"name": "Gilbert"}) #Note I used a normal tuple instead of Context({"username": "Gilbert"}) because Context is deprecated. When I used Context, I got an error > TypeError: context must be a dict rather than Context
text_content = render_to_string('receipt_email.txt', context, request=request)
html_content = render_to_string('receipt_email.html', context, request=request)
try:
#I used EmailMultiAlternatives because I wanted to send both text and html
emailMessage = EmailMultiAlternatives(subject=emailSubject, body=text_content, from_email=emailOfSender, to=[emailOfRecipient,], reply_to=[emailOfSender,])
emailMessage.attach_alternative(html_content, "text/html")
emailMessage.send(fail_silently=False)
except SMTPException as e:
print('There was an error sending an email: ', e)
error = {'message': ",".join(e.args) if len(e.args) > 0 else 'Unknown Error'}
raise serializers.ValidationError(error)
重要!那么render_to_string
如何获得receipt_email.txt
和receipt_email.html
?
在我的settings.py
中,我有TEMPLATES
,下面是它的外观
请注意DIRS
,此行为os.path.join(BASE_DIR, 'templates', 'email_templates')
这行使我的模板易于访问。在我的project_dir中,有一个名为templates
的文件夹和一个名为email_templates
的子目录,如project_dir->templates->email_templates
。我的模板receipt_email.txt
和receipt_email.html
在email_templates
子目录下。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, 'templates', 'email_templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
让我补充一下,我的recept_email.txt
看起来像这样;
Dear {{name}},
Here is the text version of the email from template
然后,我的receipt_email.html
看起来像这样;
Dear {{name}},
<h1>Now here is the html version of the email from the template</h1>
答案 5 :(得分:3)
示例中有错误....如果您按写入方式使用它,则会发生以下错误:
&LT;类型'exceptions.Exception'&gt;:'dict'对象没有属性'render_context'
您需要添加以下导入:
from django.template import Context
并将字典更改为:
d = Context({ 'username': username })
请参阅http://docs.djangoproject.com/en/1.2/ref/templates/api/#rendering-a-context
答案 6 :(得分:3)
Django Mail Templated 是一个功能丰富的Django应用程序,可以使用Django模板系统发送电子邮件。
安装:
pip install django-mail-templated
配置:
INSTALLED_APPS = (
...
'mail_templated'
)
模板:
{% block subject %}
Hello {{ user.name }}
{% endblock %}
{% block body %}
{{ user.name }}, this is the plain text part.
{% endblock %}
的Python:
from mail_templated import send_mail
send_mail('email/hello.tpl', {'user': user}, from_email, [user.email])
答案 7 :(得分:3)
我创建了Django Simple Mail,以便为您要发送的每笔交易电子邮件提供一个简单,可自定义和可重复使用的模板。
电子邮件内容和模板可以直接从Django的管理员进行编辑。
以您的示例为例,您将注册电子邮件:
from simple_mail.mailer import BaseSimpleMail, simple_mailer
class WelcomeMail(BaseSimpleMail):
email_key = 'welcome'
def set_context(self, user_id, welcome_link):
user = User.objects.get(id=user_id)
return {
'user': user,
'welcome_link': welcome_link
}
simple_mailer.register(WelcomeMail)
并以这种方式发送:
welcome_mail = WelcomeMail()
welcome_mail.set_context(user_id, welcome_link)
welcome_mail.send(to, from_email=None, bcc=[], connection=None, attachments=[],
headers={}, cc=[], reply_to=[], fail_silently=False)
我希望得到任何反馈。
答案 8 :(得分:1)
#pragma comment(lib, "Ws2_32.lib")
#include "winsock2.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <chrono>
#include <cstdint>
#include <cstring>
#include "Mstcpip.h"
#include "WS2tcpip.h"
#define BUFFSIZE 16
const char* getSocketErrorDescription(int socketError)
{
static char msg[100];
sprintf(msg, "Winsock error code %d", socketError);
return msg;
}
int read_select(SOCKET sockfd, long microsSec)
{
fd_set set;
struct timeval timeout;
FD_ZERO(&set);
FD_SET(sockfd, &set);
timeout.tv_sec = microsSec / 1000000;
timeout.tv_usec = microsSec % 1000000;
/* select returns 0 if timeout, 1 if input available, -1 if error. */
return select(0/*sockFd+1*/, &set, NULL, NULL, &timeout);
}
void parseMsg(std::string& buffer, std::vector<int64_t>& messages_list)
{
while (buffer.size() >= sizeof(int64_t))
{
int64_t value;
std::memcpy(&value, buffer.data(), sizeof(value));
messages_list.push_back(value);
buffer.erase(0, sizeof(value));
}
}
int main()
{
int counter = 0;
//std::ofstream res_file;
//res_file.open("D:\\cpp_times.txt");
WORD version = MAKEWORD(2, 2);
WSADATA data;
int res = WSAStartup(version, &data);
if (res != 0)
{
std::cout << ("WSAStartup failed. " << getSocketErrorDescription(res) << std::endl;
return 0;
}
sockaddr_in localAddr;
ZeroMemory(&localAddr, sizeof(localAddr));
localAddr.sin_family = PF_INET;
localAddr.sin_port = htons(5555);
//localAddr.sin_addr.s_addr = INADDR_ANY;
InetPton(PF_INET, "127.0.0.1", &(localAddr.sin_addr));
SOCKET sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd == INVALID_SOCKET)
{
std::cout << "socket failed. " << getSocketErrorDescription(WSAGetLasstError()) << std::endl;
WSACleanup();
return 0;
}
if (bind(sockfd, (sockaddr*)&localAddr, sizeof(localAddr)) == SOCKET_ERROR)
{
std::cout << "bind failed. " << getSocketErrorDescription(WSAGetLastError()) << std::endl;
closesocket(sockfd);
WSACleanup();
return 0;
}
if (listen(sockfd, SOMAXCONN) == SOCKET_ERROR)
{
std::cout << "listen failed. " << getSocketErrorDescription(WSAGetLastError()) << std::endl;
closesocket(sockfd);
WSACleanup();
return 0;
}
int clilen = sizeof(localAddr);
SOCKET new_socket = accept(sockfd, (sockaddr*)&localAddr, &clilen);
if (new_socket == INVALID_SOCKET)
{
std::cout << "accept failed. " << getSocketErrorDescription(WSAGetLastError()) << std::endl;
closesocket(sockfd);
WSACleanup();
return 0;
}
std::cout << "Listening..." << std::endl;
std::string m_buffer;
char recvbuf[BUFFSIZE];
while (true)
{
int nbytesRead;
// Receive until the peer closes the connection
// if (read_select(new_socket, 10000))
// {
// nbytesRead = recv(new_socket, recvbuf, BUFFSIZE, 0);
// }
// else
// {
// //std::cout << strMsg << ": timeout" << std::endl;
// continue;
// }
nbytesRead = recv(new_socket, recvbuf, BUFFSIZE, 0);
if (nbytesRead <= 0)
{
if (nbytesRead < 0)
std::cout << "recv failed. " << getSocketErrorDescription(WSAGetLastError()) << std::endl;
else
std::cout << "client disconnected" << std::endl;
closesocket(new_socket);
break;
}
m_buffer.append(recvbuf, nbytesRead);
std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(duration);
__int64 r_t = microseconds.count();
std::vector<int64_t> message_list;
parseMsg(m_buffer, message_list);
for (auto msg : message_list)
{
counter++;
int64_t diff = r_t - msg;
std::cout << "SendTime: " << msg << " ReceiveTime: " << r_t << " Diff: " << diff << std::endl;
std::cout << "Counter: " << counter << std::endl;
//res_file << diff << std::endl;
}
}
closesocket(sockfd);
WSACleanup();
return 0;
}
对我不起作用,所以我使用了send_emai()
here in django docs。
我提供了两个版本的分析服务商:
EmailMessage
如果您要包括电子邮件的纯文本版本,请按如下所示修改上面的内容:
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
# import file with html content
html_version = 'path/to/html_version.html'
html_message = render_to_string(html_version, { 'context': context, })
message = EmailMessage(subject, html_message, from_email, [to_email])
message.content_subtype = 'html' # this is required because there is no plain text email version
message.send()
我的纯文本和html版本如下所示: plain_version.html:
from django.template.loader import render_to_string
from django.core.mail import EmailMultiAlternatives # <= EmailMultiAlternatives instead of EmailMessage
plain_version = 'path/to/plain_version.html' # import plain version. No html content
html_version = 'path/to/html_version.html' # import html version. Has html content
plain_message = render_to_string(plain_version, { 'context': context, })
html_message = render_to_string(html_version, { 'context': context, })
message = EmailMultiAlternatives(subject, plain_message, from_email, [to_email])
message.attach_alternative(html_message, "text/html") # attach html version
message.send()
html_version.html
Plain text {{ context }}
答案 9 :(得分:0)
我写了一个snippet,允许您发送使用存储在数据库中的模板呈现的电子邮件。一个例子:
LambdaSubscriber.onNext("c") [Thread Elastic N]
答案 10 :(得分:0)
如果要为邮件使用动态电子邮件模板,则将电子邮件内容保存在数据库表中。 这就是我另存为HTML代码的数据库=
<p>Hello.. {{ first_name }} {{ last_name }}. <br> This is an <strong>important</strong> {{ message }}
<br> <b> By Admin.</b>
<p style='color:red'> Good Day </p>
您认为:
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
def dynamic_email(request):
application_obj = AppDetails.objects.get(id=1)
subject = 'First Interview Call'
email = request.user.email
to_email = application_obj.email
message = application_obj.message
text_content = 'This is an important message.'
d = {'first_name': application_obj.first_name,'message':message}
htmly = FirstInterviewCall.objects.get(id=1).html_content #this is what i have saved previously in database which i have to send as Email template as mentioned above HTML code
open("partner/templates/first_interview.html", "w").close() # this is the path of my file partner is the app, Here i am clearing the file content. If file not found it will create one on given path.
text_file = open("partner/templates/first_interview.html", "w") # opening my file
text_file.write(htmly) #putting HTML content in file which i saved in DB
text_file.close() #file close
htmly = get_template('first_interview.html')
html_content = htmly.render(d)
msg = EmailMultiAlternatives(subject, text_content, email, [to_email])
msg.attach_alternative(html_content, "text/html")
msg.send()
这将向动态HTML模板发送您保存在Db中的内容。
答案 11 :(得分:-1)
我喜欢使用此工具来轻松发送电子邮件HTML和TXT并进行简单的上下文处理:https://github.com/divio/django-emailit