[英]Django: Display website name in Template/Emails without using Sites framework
[英]Django Display latest emails to certain user in Template
我創建的 django 應用程序在很大程度上依賴於這樣發送的電子郵件:
from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
fail_silently=False,
)
假設我不斷向用戶的 email 發送 email ,如下所示:
post = get_object_or_404(Post, pk=pk)
post_title = post.title
author_of_post = post.author
post_email = author_of_post.email
send_mail(
'An User Comment on Your Post',
'''Dear User, ''' '''
Your post, ''' + post_title + ''' was comment on by an user. Want to reply and check it out?
--From the People at Site'''
,
'randomemailuser@domain.com',
[post_email],
)
現在我想添加一個通知區域,在上面的post_email
示例中,它將顯示發送給用戶的所有最新電子郵件。 那我該怎么做。 總而言之,我想要一個模板,用戶可以在其中查看發送到他們帳戶的最新電子郵件,有點像通知區域。 謝謝。
如果您想跟蹤您發送的電子郵件,您需要將此信息保存在您的數據庫中,這意味着引入 model。 這樣的事情應該可以解決問題:
#models.py
class SentEmail(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
email_subject = models.CharField(max_length=255)
...
您顯然可以在此 model 上附加您想要的任何其他信息(發送時間、email 正文等)。 然后,當您發送 email 時,您還需要保存 model 實例,例如:
from .models import SentEmail
...
send_mail( ... ) # all the stuff you had before
SentEmail.objects.create(
email_subject="An User Comment on Your Post"
user=whoever_the_user_is
)
然后,您只需創建一個視圖和模板,以與任何其他視圖相同的方式顯示此信息。
一些第三方服務允許您管理所有事務性電子郵件(sendgrid、mailgun 等),它們可能會提供 API 供您獲取發送給特定用戶的所有電子郵件。 這將使您能夠實現上面描述的那種事情,但老實說,我認為我上面建議的方法會簡單得多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.