question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Skip messages via Jinja template

See original GitHub issue

The 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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
awdeoriocommented, Apr 8, 2020

Here’s a brain dump of some different ways this could work. Feedback welcome.

  1. Include row numbers, similar to the cut -f option. From man cut:

    $ mailmerge --include-rows 1-10,13-20
    $ man cut
    ...
    Use one, and only one of -b, -c or -f.  Each LIST is  made  up  of  one
    range,  or  many ranges separated by commas.  Selected input is written
    in the same order that it is read, and is written exactly  once.   Each
    range is one of:
    
    N      N'th byte, character or field, counted from 1
    
    N-     from N'th byte, character or field, to end of line
    
    N-M    from N'th to M'th (included) byte, character or field
    
    -M     from first to M'th (included) byte, character or field
    
  2. Exclude row numbers, e.g.:

    $ mailmerge --exclude-rows 11-12
    
  3. Pattern match against a column, similar to csvgrep.

    $ mailmerge --column email --match umich.edu
    $ mailmerge --column email --match umich.edu --invert-match
    
  4. 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.

    $ mailmerge --skip-blank
    

    And the corresponding mailmerge_template.txt:

    {% if condition_based_on_current_record -%}
    TO: {{email}}
    FROM: from@test.com
    
    Hello world
    {% endif -%}
    
  5. 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 the senders list.

    $ mailmerge --skip-blank-to
    

    And the corresponding mailmerge_template.txt:

    {% if condition_based_on_current_record -%}
    TO: {{email}}
    {% endif -%}
    
    FROM: from@test.com
    
    Hello world
    

I’m leaning towards option 4, similar to @kapgit 's suggestion above.

1reaction
awdeoriocommented, Apr 6, 2020

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 or csvkit.

Finally, here’s an example of how to use the command line options to skip a message, e.g., message 11.

$ mailmerge --limit 10
$ mailmerge --resume 12
Read more comments on GitHub >

github_iconTop Results From Across the Web

Jinja Documentation (3.0.x) » Template Designer ...
From Jinja 2.2 onwards, you can mark an include with ignore missing ; in which case Jinja will ignore the statement if the...
Read more >
How to disable Jinja2 for sections of template with {}?
DebugUndefined it will ignore missing parameters and leave them as if a new Jinja template. Useful for say multi-stage parsing and u can...
Read more >
Primer on Jinja Templating - Real Python
With Jinja, you can build rich templates that power the front end of your web ... To see what it'll do, skip ahead...
Read more >
Jinja2 templates and filters - Pexip Infinity Docs
Pexip Infinity supports a subset of filters from the jinja2 templating language. Any jinja filters that are not listed below have been disabled...
Read more >
Server Side Template Injection with Jinja2 - OnSecurity
In this post, Gus looks into building Jinja2 SSTI payloads from zero. While also playing with bypass methods and different exploitation ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found