[英]Sending a text + HTML email with a calendar ICS attachment in Django or Python
我一直在尋找一個庫或至少是功能性的代碼片段,讓我從 Django(或至少在 Python 中)發送電子郵件,其中包含文本內容、HTML 內容和每個專業都能識別的 ICS 日歷附件電子郵件客戶端。 對於我的特定用例,如果向用戶提供“添加到日歷”按鈕就足夠了。
我覺得現在這應該是一個已解決的問題,但我只找到引用未維護或以其他方式過時或不完整的庫的答案。 我已經測試了幾個將附加 ICS 文件的片段,但是 G-mail 並沒有像往常那樣讓我選擇將其添加到日歷中。
是否有我缺少的現成解決方案?
Django 內置了基於 python 的 smtplib 的解決方案:
https://docs.djangoproject.com/en/3.2/topics/email/
您需要在設置中向您的 smtp 服務器(gmail、mailgun 等)提供一些憑據,然后您可以使用 django.core.mail 模塊。
對於附加內容,您可以使用 EmailMessage.attach() 或 attach_file()
https://docs.djangoproject.com/en/3.2/topics/email/#django.core.mail.EmailMessage
首先創建一個 .ics 文件,這當然也可以通過 python 腳本來生成動態 .ics 文件。
.ics 文件
有關iCalendar 的更多信息:基本信息和通用模板(請參見下面的示例)。
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:uid1@example.com
DTSTAMP:19970714T170000Z
ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com
DTSTART:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:Bastille Day Party
GEO:48.85299;2.36885
END:VEVENT
END:VCALENDAR
如果您覺得更容易,您也可以生成自己的 iCalendar(請參閱下面生成的示例)。
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//ical.marudot.com//iCal Event Maker
CALSCALE:GREGORIAN
BEGIN:VTIMEZONE
TZID:Europe/Berlin
LAST-MODIFIED:20201011T015911Z
TZURL:http://tzurl.org/zoneinfo-outlook/Europe/Berlin
X-LIC-LOCATION:Europe/Berlin
BEGIN:DAYLIGHT
TZNAME:CEST
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZNAME:CET
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTAMP:20210716T221958Z
UID:20210716T221958Z-901688629@marudot.com
DTSTART;TZID=Europe/Berlin:20210717T120000
DTEND;TZID=Europe/Berlin:20210717T160000
SUMMARY:Stack Overflow
DESCRIPTION:iCalendar example for Stack Overflow user Ciske\n
LOCATION:Amsterdam
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:Stack Overflow
TRIGGER:-PT1H
END:VALARM
END:VEVENT
END:VCALENDAR
Django設置.py
EMAIL_HOST = SMTP server.
EMAIL_HOST_USER = Login credentials for the SMTP server.
EMAIL_HOST_PASSWORD = Password credential for the SMTP server.
EMAIL_PORT = SMTP server port.
EMAIL_USE_TLS or _SSL = True if secure connection.
Django 視圖.py
發送帶有 .ics 文件作為附件的電子郵件。
from django.core.mail import EmailMessage
# Send email with attachment
email = EmailMessage(
'Subject',
'Email body',
'from@example.com',
['to@example.com']
)
email.attach_file('assets/invite.ics', 'text/calendar')
email.send()
您甚至可以將 .html 文件添加到您的電子郵件中。
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
html_content = render_to_string('assets/email.html', context)
email = EmailMultiAlternatives('Subject', 'Email body', 'from@example.com', [to@example.com])
email.attach_alternative(html_content, 'text/html')
email.attach_file('assets/invite.ics', 'text/calendar')
email.send()
您可以嘗試使用mark_safe()
它將 js,html 代碼作為字符串呈現到 html 頁面。 我曾用它來自定義 Django 管理員。 檢查下面的例子:
some_sample_string = '''<h1>This is some sample </h1>'''
my_sample_html = mark_safe(some_sample_string)
你可以用 HTML 設計一個頁面,向它添加一些自定義設計,然后將 mark_safe 字節對象返回到 HTML 或任何網頁,並在那里標記它會起作用。
你可以檢查這些鏈接它可能對你有幫助
https://www.kite.com/python/docs/django.utils.safestring.mark_safe
https://www.fullstackpython.com/django-utils-safestring-mark-safe-examples.html
https://docs.djangoproject.com/en/3.0/_modules/django/utils/html/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.