Re: Aw: Re: Extract lines from file, add to new files
On 1/30/24 14:46, AVI GROSS via Python-list wrote: Rich, You may want to broaden your perspective a bit when people make suggestions. Karsten did not spell out a full design and should not need to. But consider this as a scenario. You want to send (almost) the same message to one or more recipients. So call a program, perhaps some variant on a shell script, that does some prep work such as maybe creating a temporary or working directory/folder. Had one copy of your message ready in a file somewhere, Have a way to get a list of recipients intended and the file or files containing enough info to link email addresses to human names and anything else such as their preferred pronoun or address. I'd say based on the bits of the problem description I *have* absorbed, which almost certainly isn't all of them, there's a fairly basic capability, not terribly often used in my experience, that might be of some use: https://docs.python.org/3/library/string.html#template-strings -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On 1/30/2024 12:21 PM, Rich Shepard via Python-list wrote: On Tue, 30 Jan 2024, Thomas Passin via Python-list wrote: Fine, my toy example will still be applicable. But, you know, you haven't told us enough to give you help. Do you want to replace text from values in a file? That's been covered. Do you want to send the messages using those libraries? You haven't said what you don't know how to do. Something else? What is it that you want to do that you don't know how? Thomas, For 30 years I've used a bash script using mailx to send messages to a list of recipients. They have no salutation to personalize each one. Since I want to add that personalized salutation I decided to write a python script to replace the bash script. I have collected 11 docs explaining the smtplib and email modules and providing example scripts to apply them to send multiple individual messages with salutations and attachments. If I had a script that's been working for 30 years, I'd probably just use Python to do the personalizing and let the rest of the bash script do the rest, like it always has. The Python program would pipe or send the personalized messages to the rest of the bash program. Something in that ballpark, anyway. Today I'm going to be reading these. They each recommend using .csv input files for names and addresses. My first search is learning whether I can write a single .csv file such as: "name1","address1" "mane2","address2" which I believe will work; and by inserting at the top of the message block Hi, {yourname} the name in the .csv file will replace the bracketed place holder If the file contents are going to be people's names and email addresses, I would just tab separate them and split each line on the tab. Names aren't going to include tabs so that would be safe. Email addresses might theoretically include a tab inside a quoted name but that would be extremely obscure and unlikely. No need for CSV, it would just add complexity. data = f.readlines() for d in data: name, addr = line.split('\t') if line.strip() else ('', '') Still much to learn and the batch of downloaded PDF files should educate me. Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list
RE: Extract lines from file, add to new files
Thomas, on some points we may see it differently. Some formats can be done simply but are maybe better done in somewhat standard ways. Some of what the OP has is already tables in a database and that can trivially be exported into a CSV file or other formats like your TSV file and more. They can also import from there. As I mentioned, many spreadsheets and all kinds of statistical programs tend to support some formats making it quite flexible. Python has all kinds of functionality, such as in the pandas module, to read in a CSV or write it out. And once you have the data structure in memory, al kinds of queries and changes can be made fairly straightforwardly. As one example, Rich has mentioned wanting finer control in selecting who gets some version of the email based on concepts like market segmentation. He already may have info like the STATE (as in Arizona) in his database. He might at some point enlarge his schema so each entry is placed in one or more categories and thus his CSV, once imported, can do the usual tasks of selecting various rows and columns or doing joins or whatever. Mind you, another architecture could place quite a bit of work completely on the back end and he could send SQL queries to the database from python and get back his results into python which would then make the email messages and pass them on to other functionality to deliver. This would remove any need for files and just rely on the DB. There as as usual, too many choices and not necessarily one best answer. Of course if this was a major product that would be heavily used, sure, you could tweak and optimize. As it is, Rich is getting a chance to improve his python skills no matter which way he goes. -Original Message- From: Python-list On Behalf Of Thomas Passin via Python-list Sent: Tuesday, January 30, 2024 10:37 PM To: python-list@python.org Subject: Re: Extract lines from file, add to new files On 1/30/2024 12:21 PM, Rich Shepard via Python-list wrote: > On Tue, 30 Jan 2024, Thomas Passin via Python-list wrote: > >> Fine, my toy example will still be applicable. But, you know, you haven't >> told us enough to give you help. Do you want to replace text from values >> in a file? That's been covered. Do you want to send the messages using >> those libraries? You haven't said what you don't know how to do. >> Something >> else? What is it that you want to do that you don't know how? > > Thomas, > > For 30 years I've used a bash script using mailx to send messages to a list > of recipients. They have no salutation to personalize each one. Since I > want > to add that personalized salutation I decided to write a python script to > replace the bash script. > > I have collected 11 docs explaining the smtplib and email modules and > providing example scripts to apply them to send multiple individual > messages > with salutations and attachments. If I had a script that's been working for 30 years, I'd probably just use Python to do the personalizing and let the rest of the bash script do the rest, like it always has. The Python program would pipe or send the personalized messages to the rest of the bash program. Something in that ballpark, anyway. > Today I'm going to be reading these. They each recommend using .csv input > files for names and addresses. My first search is learning whether I can > write a single .csv file such as: > "name1","address1" > "mane2","address2" > which I believe will work; and by inserting at the top of the message block > Hi, {yourname} > the name in the .csv file will replace the bracketed place holder If the file contents are going to be people's names and email addresses, I would just tab separate them and split each line on the tab. Names aren't going to include tabs so that would be safe. Email addresses might theoretically include a tab inside a quoted name but that would be extremely obscure and unlikely. No need for CSV, it would just add complexity. data = f.readlines() for d in data: name, addr = line.split('\t') if line.strip() else ('', '') > Still much to learn and the batch of downloaded PDF files should educate > me. > > Regards, > > Rich -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
RE: Aw: Re: Extract lines from file, add to new files
Rich, You may want to broaden your perspective a bit when people make suggestions. Karsten did not spell out a full design and should not need to. But consider this as a scenario. You want to send (almost) the same message to one or more recipients. So call a program, perhaps some variant on a shell script, that does some prep work such as maybe creating a temporary or working directory/folder. Had one copy of your message ready in a file somewhere, Have a way to get a list of recipients intended and the file or files containing enough info to link email addresses to human names and anything else such as their preferred pronoun or address. Now have the script call your super-duper python program with enough info so it can find the folder to put amended COPIES of your letter into as well as. Perhaps the email address intended in the filename or whatever works for you. Your program will then simply identify each email recipient you want and look up the other info and prepend the customized salutation, or make substitutions in the template and write out a new file in the designated folder with perhaps the email address as the filename. When your loop ends, exit the python program with success, or perhaps report some failure. The shell script now resumes by checking the exit status and if OK, continuing to enter the folder and loop on all file contests and invoke the functionality to send each copied/enhanced file to the intended recipient. If you also need to support attachments, you can figure out how to attach the same ones to each as I assume those are not changed for each recipient. It may keep track of how many worked or failed and eventually clear out the files and perhaps the folder and you are done. This is NOT a required way to do it but for what sounds like a limited personal project, it should work well enough and have you do the limited amount of work you need in Python. Having said that, you can likely also easily do everything without python and I have written some huge shell scripts in my time to do way more complex things. But learning how to do things like this well in python can be time well spent as long as you don't tackle too much at a time and get overwhelmed. -Original Message- From: Python-list On Behalf Of Rich Shepard via Python-list Sent: Tuesday, January 30, 2024 12:53 PM To: python-list@python.org Subject: Re: Aw: Re: Extract lines from file, add to new files On Tue, 30 Jan 2024, Karsten Hilbert wrote: > Why not foxus on just the part you think you are better off using python, > namely personalization ? > > Create personalized files and send them with your trusted mailx solution ? Karsten, Too much time. And while mailx accepts the '-a' option for attachments but has none for individual salutations. Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On 1/30/2024 8:37 AM, Rich Shepard via Python-list wrote: On Mon, 29 Jan 2024, Thomas Passin via Python-list wrote: If you aren't going to use one or another existing template system, perhaps the easiest is to use unique strings in the message file. For example: Dear __##so-and-so##__: Please don't write this message off as mere spam. Respectfully, Rich Then you just do a replace of the unique string by the salutation. Don't change the original (i.e., template), make the changes to a copy that you will output. My script is not a web application, but an emailer that allows me to contact clients and prospective clients. From the command line on a linux host. Using the python smtplib and mail modules. Rich Fine, my toy example will still be applicable. But, you know, you haven't told us enough to give you help. Do you want to replace text from values in a file? That's been covered. Do you want to send the messages using those libraries? You haven't said what you don't know how to do. Something else? What is it that you want to do that you don't know how? -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On Mon, 29 Jan 2024, Thomas Passin via Python-list wrote: If you aren't going to use one or another existing template system, perhaps the easiest is to use unique strings in the message file. For example: Dear __##so-and-so##__: Please don't write this message off as mere spam. Respectfully, Rich Then you just do a replace of the unique string by the salutation. Don't change the original (i.e., template), make the changes to a copy that you will output. My script is not a web application, but an emailer that allows me to contact clients and prospective clients. From the command line on a linux host. Using the python smtplib and mail modules. Rich -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On Tue, Jan 30, 2024 at 1:13 AM AVI GROSS via Python-list wrote: > > It can be quite frustrating figuring out what someone wants, Grant, > especially when they just change it. > > It is worse when instead of starting a new thread with an appropriate > subject line, it continues and old one that was also frustrating to > understand. Is it worse than top posting? -- https://mail.python.org/mailman/listinfo/python-list
Re: Aw: Re: Extract lines from file, add to new files
On Tue, 30 Jan 2024, Karsten Hilbert wrote: Why not foxus on just the part you think you are better off using python, namely personalization ? Create personalized files and send them with your trusted mailx solution ? Karsten, Too much time. And while mailx accepts the '-a' option for attachments but has none for individual salutations. Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list
Aw: Re: Re: Extract lines from file, add to new files
> > Why not foxus on just the part you think you are better off using python, > > namely personalization ? > > > > Create personalized files and send them with your trusted mailx solution ? > > Karsten, > > Too much time. And while mailx accepts the '-a' option for attachments but > has none for individual salutations. It doesn't need to. It just sends the (pre-personalized-by-Python) mail files. Karsten -- https://mail.python.org/mailman/listinfo/python-list
Re: Aw: Re: Re: Extract lines from file, add to new files
On Tue, 30 Jan 2024, Karsten Hilbert wrote: It doesn't need to. It just sends the (pre-personalized-by-Python) mail files. Karsten, In which case, I might as well have Python format and send the messages. :-) Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list
Aw: Re: Re: Re: Extract lines from file, add to new files
> On Tue, 30 Jan 2024, Karsten Hilbert wrote: > > > It doesn't need to. It just sends the (pre-personalized-by-Python) mail > > files. > > Karsten, > > In which case, I might as well have Python format and send the messages. :-) Certainly. But it seems you are wrestling with Python. Might as well reduce the attack surface. Karsten -- https://mail.python.org/mailman/listinfo/python-list
RE: Extract lines from file, add to new files
I deleted the contents of the message so I can avoid both of the deadly sins of top posting and bottom posting and chance committing the sin of replying without any context. Of course, I am only replying to Jon wishing a real or feigned good luck to the OP. But seriously, the OP, AKA Rich, is making clear that he is making a tool for his own use. It sounds like he wants to maintain a data repository of his own with some info about his clients and then have the ability to specify a name and pop up an email directed to them, or something along those lines. Without further info, this sounds like what quite a few popular and even free mailers already do to some extent as you have an associated address book. Some start doing possible matches as you type. I hazard Rich is not using something this simple as he does seem to have some form-letter merge or similar functionality in mind, such as automating the seemingly mandatory "Dear XXX" salutation. But if he is the creator and maintainer of his client data and chooses not to use one of many available applications, then it seems he already has reasons he wants a particular design for the data and simply wants us to help him use it the way he wants. That could be communicated a bit more clearly but after many messages back and forth, I have not exactly understood it. In my opinion, having created all kinds of mailers over the years, sometimes the hard way, This strikes me as not really being about what mailing functionality exists at all. As a general rule, you first create supporting parts for your mail then pass them along to functionality that assembles the mail as a set of headers and a body and dispatches it. You as the programmer need to supply a body, tell it who to put on TO/CC/BB lines, perhaps provide a subject, perhaps specify attachments, and off you go. The exact methods will differ. But what Rich presumably needs to do is have his program interact with him in specifying who he wants to mail to and then looking it up in whatever file arrangement it contains. If he also wants to use other parts such as a human name or address inside the body of the text, his program needs to merge the text he supplies along with extracted parts of the data. Or is that not what he wants? The above could be quite straightforward and I recall doing things like this with a simple shell script in UNIX. Specifically, I created a text file where I recorded info for each person in some format like NAME|PHONE|EMAIL|COMMENT Then to search for someone, you could use something like grep to find a name by anchoring to the beginning or ending with the "|" so it does not match the text in another field such as email or address, and use other utilities ranging from cut to awk and getting the parts you want into variables and then interpolate them into a message template and so on. Of course, doing it in python is a good way to go too and should not be hard once it is decided how to store the data. But again, this is re-inventing things that others have already done. The python modules include many ways to store modular data. The books I have read mention them all the time. Pick one. And, yes, you can choose to maintain two files if that design works for you. Consider some storage method that stores data in sections like: [NAME one] First: whatever Email: whatever [NAME two] First: ... Email: ... There are specific formats along these lines and you can get python modules that you ask for "NAME one" and it reads the file until it finds a section as "[NAME one]" or not. If found, it returns the variables/values right below it. So your two step algorithm may consist of two files with one file containing just names, perhaps to use a grep functionality on. Some of those names will have a matching section like the above somewhere in the other file. So if you want to send mail to "Jo" then your program may search for all names starting with "Jo" and offer you "John Smith" and perhaps also "Joachim Martillo". The program takes whichever full name(s) you then select and calls a function that uses that full name to search the second file to find an exact match and returns what it finds there such as an email address. But unless you have lots of contacts, as already discussed, there are far easier ways to do things in a more brute force way. Take a one-line per entry format such as a CSV or TSV and extract whatever column contains the name as needed to do the first search. Yes, this tends to mean reading the entire file. And, for the record, I am not a fan of hiding replies at the bottom except for short messages. I prefer to use some combination of in-line if addressing many points in the original and mainly the top with perhaps a preface explaining what is being addressed. The reader is usually capable of digging below if they want to know more. But this is a more religious war having nothing to do with python specifically. My frustration is that I often want to help
Aw: Re: Extract lines from file, add to new files
> For 30 years I've used a bash script using mailx to send messages to a list > of recipients. They have no salutation to personalize each one. Since I want > to add that personalized salutation I decided to write a python script to > replace the bash script. Why not foxus on just the part you think you are better off using python, namely personalization ? Create personalized files and send them with your trusted mailx solution ? That'll take out wrestling with smptlib et al. Karsten -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On Tue, 30 Jan 2024, Thomas Passin via Python-list wrote: Fine, my toy example will still be applicable. But, you know, you haven't told us enough to give you help. Do you want to replace text from values in a file? That's been covered. Do you want to send the messages using those libraries? You haven't said what you don't know how to do. Something else? What is it that you want to do that you don't know how? Thomas, For 30 years I've used a bash script using mailx to send messages to a list of recipients. They have no salutation to personalize each one. Since I want to add that personalized salutation I decided to write a python script to replace the bash script. I have collected 11 docs explaining the smtplib and email modules and providing example scripts to apply them to send multiple individual messages with salutations and attachments. Today I'm going to be reading these. They each recommend using .csv input files for names and addresses. My first search is learning whether I can write a single .csv file such as: "name1","address1" "mane2","address2" which I believe will work; and by inserting at the top of the message block Hi, {yourname} the name in the .csv file will replace the bracketed place holder. Still much to learn and the batch of downloaded PDF files should educate me. Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list
RE: Extract lines from file, add to new files
On Tue, 30 Jan 2024, AVI GROSS via Python-list wrote: But seriously, the OP, AKA Rich, is making clear that he is making a tool for his own use. It sounds like he wants to maintain a data repository of his own with some info about his clients and then have the ability to specify a name and pop up an email directed to them, or something along those lines. Close, Avi. I have no issues sending messages to single individuals or mailing lists. I want to send the same message to several individuals at one time, which I've done -- without individual salutations -- for 30 years using a bash script and mailx. As I replied to Thomas on the list, I've downloaded 11 PDF docs from the Web (and a useful book on the Python3 standard library) and will start reading and learning from them today. I expect to find answers to my few remaining questions in these docs. Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list