Skip messages via Jinja template
See original GitHub issueThe context: Using a DB as primary datasource
I’m using mailmerge, using a MySQL database as datasource. This DB contains data and a number of custom views. Each view queries the data and returns a row for each mail to be sent with mailmerge.
As mailmerge is accepting only CSV file as input (as far as I know), and not a DB view, a little utility bash script
send_to <view> <mail_template>
is executing the DB <view> and generates a transient CSV file containing the selected rows.
The script then calls mailmerge using the path to this CSV file and a Jinja2 <mail_template> as input parameters.
Mailmerge does its job and sends a mail for each line in the CSV file. Perfect!
Issue:
Sometimes, I need to quickly send a mail to a subset of the mysql <view>. One shot. No need to reuse the query. I could of course create/alter the DB view, but that’s not as agile as I would like and could quickly lead to proliferation of views, all with their own little variations on some ‘master’ view.
Question:
Is is possible to use some Jinja2 tagging in the mail template, that would have as an effect to skip the current record?
Something like:
{% if condition_based_on_current_record -%}
TO: {{email}}
SUBJECT: Interesting mail
FROM: My Self <myself@mydomain.com>
Dear {{name}},
You are the lucky recipient of this email.
Best regards,
Me
{% endif -%}
If condition_based_on_current_record is true then the mail is sent. Otherwise, the sender, recipient and message are all empty, and mailmerge would skip the record.
Thank you.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:7 (6 by maintainers)
Top GitHub Comments
Here’s a brain dump of some different ways this could work. Feedback welcome.
Include row numbers, similar to the
cut -f
option. Fromman cut
:Exclude row numbers, e.g.:
Pattern match against a column, similar to
csvgrep
.Skip message if entire rendered template is blank. This is similar to the suggestion of @kapgit above. I’d perhaps couple it with a command line flag. Perhaps check for blank messages and produce a helpful error message, suggesting this flag.
And the corresponding
mailmerge_template.txt
:Skip message if
TO
field is blank. EDIT: possible bug could be if there’s a CC or BCC field. TO, CC and BCC all end up in thesenders
list.And the corresponding
mailmerge_template.txt
:I’m leaning towards option 4, similar to @kapgit 's suggestion above.
One way to do it is to make a copy of the CSV file and delete some of the rows with a text editor. Another way to do it is to filter the CSV file using command line tools like
grep
orcsvkit
.Finally, here’s an example of how to use the command line options to skip a message, e.g., message 11.