如何使用django send_mail()将电子邮件放入已发送文件夹?

时间:2019-03-12 20:35:48

标签: django email

我正在使用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,
)

如何使用此方法将已发送的电子邮件放入邮箱已发送文件夹?

1 个答案:

答案 0 :(得分:1)

我成功使用了下面的方法,但是我正在使用Django的EmailMultiAlternatives。基本上,“ send_mail”功能也应如此。根据您的电子邮件连接,您可能必须更改IMAP的一些详细信息。

pythons imaplib的文档: https://docs.python.org/3.8/library/imaplib.html

有关使用django发送电子邮件的其他信息:https://docs.djangoproject.com/en/2.2/topics/email/

所需模块:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
public LocalDate myDate;

发送邮件的代码:

from django.core.mail import EmailMultiAlternatives
from django.conf import settings
import imaplib, time

准备我们刚刚发送的电子邮件。之后将用于将电子邮件复制到已发送的文件夹中。

#Send email
email = EmailMultiAlternatives(
                    subject=subject, body=body_text, 
                    from_email=from_email, to=email_to, 
                    reply_to=['****@****.ch'],
                    headers={})
#Attaches html version of email
email.attach_alternative(message_html, "text/html")
#Attaches image
email.attach(signature())
#Sends email
email.send()

将电子邮件放置在已发送的文件夹中。

#Loads the email message to append it afterwards with IMAP
message = str(email.message())

使用django 2.2.9和python 3.8.1在我的一个项目中实现。

我自己的解决方案受此帖子的启发:Is it possible to save the sent email into the sent items folder using python?

相关问题