Giter VIP home page Giter VIP logo

Comments (7)

awdeorio avatar awdeorio commented on August 19, 2024 1

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

from mailmerge.

awdeorio avatar awdeorio commented on August 19, 2024 1

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: [email protected]
    
    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: [email protected]
    
    Hello world
    

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

from mailmerge.

kapgit avatar kapgit commented on August 19, 2024

Maybe there is a better method, but at the moment, I'm applying a quick and dirty solution that seems to work for me:

In template_message.py (commented the 3 assertions):

    def render(self, context):
        """Return rendered message object."""
        try:
            raw_message = self.template.render(context)
        except jinja2.exceptions.TemplateError as err:
            raise MailmergeError(
                "{}: {}".format(self.template_path, err)
            )
        self._message = email.message_from_string(raw_message)
        self._transform_encoding(raw_message)
        self._transform_recipients()
        self._transform_markdown()
        self._transform_attachments()
        self._message.__setitem__('Date', email.utils.formatdate())
        #assert self._sender # COMMENTED
        #assert self._recipients # COMMENTED
        #assert self._message # COMMENTED
        return self._sender, self._recipients, self._message

In __main__.py (enclosed the 'sendmail' in an 'if' statement):

def main(sample, dry_run, limit, no_limit, resume,
         template_path, database_path, config_path,
         output_format):
...
   try:
        template_message = TemplateMessage(template_path)
        csv_database = read_csv_database(database_path)
        sendmail_client = SendmailClient(config_path, dry_run)
        for _, row in enumerate_range(csv_database, start, stop):
            sender, recipients, message = template_message.render(row)
            if sender and recipients and message: # ADD THIS LINE + INDENT FOLLOWING
                sendmail_client.sendmail(sender, recipients, message)
                print_bright_white_on_cyan(
                    ">>> message {message_num}"
                    .format(message_num=message_num),
                    output_format,
                )
                print_message(message, output_format)
                print_bright_white_on_cyan(
                    ">>> message {message_num} sent"
                    .format(message_num=message_num),
                    output_format,
                )
                message_num += 1

    except MailmergeError as error:
...

from mailmerge.

bexelbie avatar bexelbie commented on August 19, 2024

It'd be great to see a flag or similar for this if we can do it in a UI friendly way. I have a routine email that I send that needs to come from one of two email addresses. Today I have to bifurcate my data set and reset the conf for each run. Not having to bifurcate the data set would be a small win.

from mailmerge.

bexelbie avatar bexelbie commented on August 19, 2024

@awdeorio I like what you have. FWIW, I'd like to express my preference for number 5 because it is smaller and keeps the if small to prevent weird nesting typos later.

from mailmerge.

captn3m0 avatar captn3m0 commented on August 19, 2024

+1 for (5). Empty to currently results in a assertion error, so its anyway not a supported usecase.

from mailmerge.

awdeorio avatar awdeorio commented on August 19, 2024

I can get behind (5). Any volunteers? :)

I think this would involve:

  • Code to add command line flag
  • Code to add "skip" functionality
  • Unit test: basic, with both skipped and not-skipped messages. Include CC, BCC.
  • Unit test: jinja logic results in blank "TO". User forgets the command line flag. Probably should error with a helpful message in this case.
  • Documentation. Maybe an "advanced usage" subsection?

from mailmerge.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.