Comments (7)
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.
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
:$ 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
-
Exclude row numbers, e.g.:
$ mailmerge --exclude-rows 11-12
-
Pattern match against a column, similar to
csvgrep
.$ mailmerge --column email --match umich.edu $ mailmerge --column email --match umich.edu --invert-match
-
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 -%}
-
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.$ 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.
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.
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.
@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.
+1 for (5). Empty to currently results in a assertion error, so its anyway not a supported usecase.
from mailmerge.
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)
- Recipe for machine-readable messages
- FEATURE MISSING: personal attachments HOT 2
- Block list HOT 1
- Apple Mail with attachments issue (former title: UTF-8 support issue) HOT 11
- Move test dependecies to extras_require
- Debug flag
- mails limit per mail feature HOT 5
- Interactive mode
- Drop Python 2 Support HOT 5
- Change CI to GitHub Actions
- setup.py has "html5" but It has a new name "html5lib"
- Inline images not working in thunderbird? HOT 5
- Crashes on email adresses with illegal characters HOT 2
- Replace `setup.py` with `pyproject.toml`
- Support CSV files with semicolons as separators HOT 3
- Add support for 2FA OAuth authentication HOT 2
- New release on Pipy HOT 1
- No mailmerge_server.conf file in the repository HOT 2
- Load system default SSL cert
- Space at the end of the header row
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mailmerge.