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

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?

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.

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.

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.

Related

Mailcow sieve script that removes attachments and adds a message to the body

I'm trying to find out how to remove non-whitelisted attachments (by mime type) (f.e. zip, exe, ...)
and append a message about the removed attachments.
I found this: https://superuser.com/a/1502589
And it worked to add a message to the subject.
But I cannot find out how to add a message to the body.
My plan was to use a regex on the attachment mime types and allow f.e.
text/* and application/json etc.
But I cannot find a single example how to change the body.
I'm using mailcow and sieve script (which I'm both new to).
Or is there a better way to "sanitize" emails before the get put into the inbox?
EDIT (2023-02-07) : I found this today:
Extension foreverypart.
Sieve Email Filtering: MIME Part Tests, Iteration, Extraction, Replacement, and Enclosure
https://www.rfc-editor.org/rfc/rfc5703.html \
The "replace" command is defined to allow a MIME part to be replaced
with the text supplied in the command.
Exactly what I try to do.
Now I need to find out how to install the extension and try it out.

Sending an e-mail through MATLAB using Microsoft Outlook

I am using the function found at the following blog: https://uk.mathworks.com/matlabcentral/answers/94446-can-i-send-e-mail-through-matlab-using-microsoft-outlook to send e-mails from my Outlook account from the MATLAB editor. It indeed works, and I can also include attachments or pictures etc.
My question is whether it is possible to send a result of another MATLAB function saved and run at the same directory. I tried calling the function like that :
sendolmail('email_address','Subject included',result of a function);
When I run this, a mistake is returned. It seems as if only strings or attachments from my computer can be sent through the function. Any ideas on how results of functions can be added and sent?
No, it's all in HTML format, so you have to save the results to a file or convert the output to an HTML formatted output (could just be a string).

How does one access (POST) arguments from nginx upload module in embedded lua/perl?

I've been trying to figure out how to access the results of the nginx upload module from embedded perl (using nginx-perl) or lua (using the embedded lua module). I've only been able to find examples of how to use the module with fastcgi (or similar), something I would, if possible, like to avoid having to use.
Simply letting the upload_pass have a lua/perl content handler does not seem to work; with the body being somehow truncated to just the first line (yes, I've told it to wait for the body and made sure it's not written to a file).
At least when using Perl (I haven't tried Lua, but I'm suspecting the same thing will happen), the complete body (as raw multipart/form-data) can be made available if one does a proxy_pass to another nginx instance.
My question is threefold. Firstly is this expected behaviour/how are arguments passed from the upload module? Secondly, is it possible to access the results of the upload module without (re)parsing the multipart/form-data using a perl/lua library in the content handler.
Finally, if the latter is not possible, can I use multipart/form-data parser used by nginx/upload without manually exporting the functions and using some form of FFI.
Thanks in advance.
With lua you can get at normal params via methods like this:
ngx.req.read_body()
local inputjson = ngx.req.get_body_data()
For post args documented: http://wiki.nginx.org/HttpLuaModule#ngx.req.get_post_args
Regular vars:
ngx.var.my_var
Lua nginx module has this well documented: http://wiki.nginx.org/HttpLuaModule
The vars that are documented for the upload module: http://www.grid.net.ru/nginx/upload.en.html
upload_set_form_field $upload_field_name.name "$upload_file_name";
upload_set_form_field $upload_field_name.content_type "$upload_content_type";
upload_set_form_field $upload_field_name.path "$upload_tmp_path";
should be accessible via:
ngx.var.upload_field_name.path
Just do a log or a print on the var to verify

Trouble with message character encoding in Amazon SQS messages

I have a rather puzzling problem with Amazon SQS and Zend(1.11.2). I am sending a message to the queue that I have setup with a snip-it that looks like this:
$sqs->send($queueURL, "opt1=foo opt2=bar");
The message comes in to the app at the far side and is seen as:
"opt1%3Dfoo+opt2%3Dbar"
The receiving app is written in Java using the com.xerox.amazonws.sqs2 library and is in production now.
There was similar sending code in an older php module that used Tarzan under Drupal that worked just fine. I have searched high and low and read the documentation for Zend, Amazon and the Java library and I am stuck.
The encoding of the string is understandable but I don't recognize the method being used. Further tests show that single quotes, angle brackets, etc. are also escaped as hex.
Any ideas?
Ken
Typica (com.xerox.amazonaws) has a 'encoded' option that internally uses Base64 to encode messages. This is on by default.
In general, we have found that encoding messages using Base64 is less troublesome then URL/percent encoding when using SQS.
I would Base64 the message text in PHP, write it to the queue, and Typica should decode just fine using the default config.

How does the email header field 'thread-index' work?

I was wondering if anyone knew how the thread-index field in email headers work?
Here's a simple chain of emails thread indexes that I messaged myself with.
Email 1 Thread-Index: AcqvbpKt7QRrdlwaRBKmERImIT9IDg==
Email 2 Thread-Index: AcqvbpjOf+21hsPgR4qZeVu9O988Eg==
Email 3 Thread-Index: Acqvbp3C811djHLbQ9eTGDmyBL925w==
Email 4 Thread-Index: AcqvbqMuifoc5OztR7ei1BLNqFSVvw==
Email 5 Thread-Index: AcqvbqfdWWuz4UwLS7arQJX7/XeUvg==
I can't seem to say with certainty how I can link these emails together. Normally, I would use the in-reply-to field or references field, but I recently found that Blackberrys do NOT include these fields. The only include Thread-Index field.
They are base64 encoded Conversation Index values. No need to reverse engineer them as they are documented by Microsoft on e.g. http://msdn.microsoft.com/en-us/library/ms528174(v=exchg.10).aspx and more detailed on http://msdn.microsoft.com/en-us/library/ee202481(v=exchg.80).aspx
Seemingly the indexes in your example doesn't represent the same conversation, which probably means that the software that sent the mails wasn't able to link them together.
EDIT: Unfortunately I don't have enough reputation to add a comment, but adamo is right that it contains a timestamp - a somewhat esoteric encoded partial FILETIME. But it also contains a GUID, so it is pretty much guarenteed to be unique for that mail (of course the same mail can exist in multiple copies).
There's a good analysis of how exactly this non-standard "Thread-Index" header appears to be used, in this post and links therefrom, including this pdf (a paper presented at the CEAS 2006 conference) and this follow-up, which includes a comment on the issue from the evolution source code (which seems to reflect substantial reverse-engineering of this undocumented header).
Executive summary: essentially, the author eventually gives up on using this header and recommends and shows a different approach, which is also implemented in the c-client library, part of the UW IMAP Toolkit open source package (which is not for IMAP only -- don't let the name fool you, it also works for POP, NNTP, local mailboxes, &c).
I wouldn't be surprised if there are mail clients out there which would not be able to link Blackberry's mails to their threads. The Thread-Index header appears to be a Microsoft extension.
Either way, Novell Evolution implements this. Take a look at this short description of how they do it, or this piece of code that finds the thread parent of a given message.
I assume that, because the lengths of the Thread-Index headers in your example are all the same, these messages were all thread starts? Strange that they're only 22-bytes, though I suppose you could try applying the 5-bytes-per-message rule to them and see if it works for you.
If you are interested in parsing the Thread-Index in C# please take a look at this post
http://forum.rebex.net/questions/3841/how-to-interprete-thread-index-header
The snippet you will find there will let you parse the Thread-Index and retrieve the Thread GUID and message DateTime. There is a problem however, it does not work for all Thread-Indexes out there. Question is why do some Thread-Indexes generate invalid DateTime and what to do to support all of them???