How to send mail with attachments in Plone using a template approach?

老子叫甜甜 提交于 2019-12-10 20:25:14

问题


I've been reading the official docs about sending emails from Plone using some templates, and it's working so far.

My question is: how do I add attachments using the template approach?


回答1:


The MailHost.send command takes both python (unicode) strings and email.Message objects. That means you can use the python email package to construct a proper MIME message with attachments.

The standard library includes a great page of examples; any text can still be generated by templates just like in the documentation you linked.




回答2:


Use Python's email module.

Examples:

http://docs.python.org/library/email-examples.html

The composed messages can be passed to context.MailHost (the MTA of Zope).

It is in every case better generating and sending out emails from the Python level instead of using the old DTML sendmail facade...don't use it.




回答3:


This is my solution, maybe it is not the best:

create a mime_file DTML Method in portal_skin/custom:

  <dtml-mime type="text/text; charset=utf-8" encode="7bit">

  <dtml-var "text">

  <dtml-boundary type="application/octet-stream" disposition="attachment" 
  filename_expr="nomefile"><dtml-var "file"></dtml-mime>

Call it (for example from a Python Script) as:

message = context.mime_file(file=a_file, text=message, nomefile='attach_name.pdf')
context.MailHost.send(message, mTo, mFrom, mSubj)

where a_file is the content of the file.

inspired by:

http://www.zope.org/Members/visibleoffice/HowTo.2003-10-22.1455

This is a quick&dirt solution, using Python Scripts.



来源:https://stackoverflow.com/questions/6572672/how-to-send-mail-with-attachments-in-plone-using-a-template-approach

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!