Science and technology

Sending customized emails with Python

Email stays a truth of life. Despite all its warts, it is nonetheless one of the simplest ways to ship info to most individuals, particularly in automated ways in which enable messages to queue for recipients.

One of the highlights of my work because the Fedora Community Action and Impact Coordinator is giving individuals excellent news about journey funding. I typically ship this info over electronic mail. Here, I will present you ways I ship customized messages to teams of individuals utilizing Mailmerge, a command-line Python program that may deal with easy and complicated emails.

Install Mailmerge

Mailmerge is packaged and out there in Fedora, and you may set up it from the command line with sudo dnf set up python3-mailmerge. You also can set up it from PyPI utilizing pip, because the mission’s README explains.

Configure your Mailmerge recordsdata

Three recordsdata management how Mailmerge works. If you run mailmerge –sample, it should create template recordsdata for you. The recordsdata are:

  • mailmerge_server.conf: This accommodates the configuration particulars in your SMTP host to ship emails. Your password is not saved on this file.
  • mailmerge_database.csv: This holds the customized knowledge for every electronic mail, together with the recipients’ electronic mail addresses.
  • mailmerge_template.txt: This is your electronic mail’s textual content with placeholder fields that might be changed utilizing the information from mailmerge_database.csv.

Server.conf

The pattern mailmerge_server.conf file consists of a number of examples that ought to be acquainted. If you’ve got ever added electronic mail to your cellphone or arrange a desktop electronic mail consumer, you’ve got seen this knowledge earlier than. The large factor to recollect is to replace your username within the file, particularly in case you are utilizing one of many instance configurations.

Database.csv

The mailmerge_database.csv file is a little more sophisticated. It should comprise (at minimal) the recipients’ electronic mail addresses and every other customized particulars vital to exchange the fields in your electronic mail. It is a good suggestion to put in writing the mailmerge_template.txt file on the identical time you create the fields checklist for this file. I discover it useful to make use of a spreadsheet to seize this knowledge and export it as a CSV file when I’m completed. This pattern file:

electronic mail,identify,quantity
myself@mydomain.com,"Myself",17
bob@bobdomain.com,"Bob",42

permits you to ship emails to 2 individuals, utilizing their first identify and telling them a quantity. This file, whereas not terribly fascinating, illustrates an necessary behavior: Always make your self the primary recipient within the file. This allows you to ship your self a take a look at electronic mail to confirm every part works as anticipated earlier than you electronic mail your entire checklist.

If any of your values comprise commas, you should enclose your entire worth in double-quotes (). If you might want to embrace a double-quote in a double-quoted subject, use two double-quotes in a row. Quoting guidelines are enjoyable, so examine CSVs in Python 3 for specifics.

Template.txt

As a part of my work, I get to share information about travel-funding choices for our Fedora contributor convention, Flock. A easy electronic mail tells individuals they have been chosen for journey funding and their particular funding particulars. One user-specific element is how a lot cash we will allocate for his or her airfare. Here is an abbreviated model of my template file (I’ve snipped out a number of the textual content for brevity):

$ cat mailmerge_template.txt
TO: Email
SUBJECT: Flock 2019 Funding Offer
FROM: Brian Exelbierd

Hi ,

I'm writing you on behalf of the Flock funding committee.  You requested funding in your attendance at Flock.  After cautious consideration we're in a position to give you the next funding:

Travel Budget:

<<snip>>

The high of the template specifies the recipient, sender, and topic. After the clean line, there’s the physique of the e-mail. This electronic mail wants the recipients’ Email, Name, and Travel_Budget from the database.csv file. Notice that these fields are surrounded by double curly braces (). The corresponding mailmerge_database.csv appears like this:

$ cat mailmerge_database.csv
Name,Email,Travel_Budget
Brian,bexelbie@redhat.com,1000
PersonA,persona@fedoraproject.org,1500
PèrsonB,personb@fedoraproject.org,500

Notice that I listed myself first (for testing) and there are two different individuals within the file. The second particular person, PèrsonB, has an accented character of their identify; Mailmerge will robotically encode it.

That’s the entire template idea: Write your electronic mail and put placeholders in double curly braces. Then create a database that gives these values. Now let’s take a look at the e-mail.

Test and ship easy electronic mail merges

Do a dry-run

Start by doing a dry-run that prints the emails, with the placeholder fields accomplished, to the display screen. By default, for those who run the command mailmerge, it should do a dry-run of the primary electronic mail:

$ mailmerge
>>> encoding ascii
>>> message zero
TO: bexelbie@redhat.com
SUBJECT: Flock 2019 Funding Offer
FROM: Brian Exelbierd
MIME-Version: 1.zero
Content-Type: textual content/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Date: Sat, 20 Jul 2019 18:17:15 -0000

Hi Brian,

I'm writing you on behalf of the Flock funding committee.  You requested funding in your attendance at Flock.  After cautious consideration we're in a position to give you the next funding:

Travel Budget: 1000

<<snip>>

>>> despatched message zero DRY RUN
>>> No attachments had been despatched with the emails.
>>> Limit was 1 messages.  To take away the restrict, use the --no-limit choice.
>>> This was a dry run.  To ship messages, use the --no-dry-run choice.

Reviewing the primary electronic mail (message zero, as counting begins from zero, like many issues in pc science), you’ll be able to see my identify and journey finances are appropriate. If you wish to evaluation each electronic mail, enter mailmerge –no-limit to inform Mailmerge to not restrict itself to the primary electronic mail. Here’s the dry-run of the third electronic mail, which exhibits the particular character encoding:

>>> message 2
TO: personb@fedoraproject.org
SUBJECT: Flock 2019 Funding Offer
FROM: Brian Exelbierd
MIME-Version: 1.zero
Content-Type: textual content/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Date: Sat, 20 Jul 2019 18:22:48 -0000

Hi P=E8rsonB,

That’s not an error; P=E8rsonB is the encoded type of PèrsonB.

Send a take a look at message

Now, ship a take a look at electronic mail with the command mailmerge –no-dry-run, which tells Mailmerge to ship a message to the primary electronic mail on the checklist:

$ mailmerge --no-dry-run
>>> encoding ascii
>>> message zero
TO: bexelbie@redhat.com
SUBJECT: Flock 2019 Funding Offer
FROM: Brian Exelbierd
MIME-Version: 1.zero
Content-Type: textual content/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Date: Sat, 20 Jul 2019 18:25:45 -0000

Hi Brian,

I'm writing you on behalf of the Flock funding committee.  You requested funding in your attendance at Flock.  After cautious consideration we're in a position to give you the next funding:

Travel Budget: 1000

<<snip>>

>>> Read SMTP server configuration from mailmerge_server.conf
>>>   host = smtp.gmail.com
>>>   port = 587
>>>   username = bexelbie@redhat.com
>>>   safety = STARTTLS
>>> password for bexelbie@redhat.com on smtp.gmail.com:
>>> despatched message zero
>>> No attachments had been despatched with the emails.
>>> Limit was 1 messages.  To take away the restrict, use the --no-limit choice.

On the fourth to final line, you’ll be able to see it prompts you in your password. If you are utilizing two-factor authentication or domain-managed logins, you will want to create an utility password that bypasses these controls. If you are utilizing Gmail and comparable methods, you are able to do it immediately from the interface; in any other case, contact your electronic mail system administrator. This won’t compromise the safety of your electronic mail system, however it is best to nonetheless hold the password complicated and secret.

When I checked my electronic mail account, I obtained a superbly formatted take a look at electronic mail. If your take a look at electronic mail appears prepared, ship all of the emails by coming into mailmerge –no-dry-run –no-limit.

Send complicated emails

You can actually see the facility of Mailmerge while you make the most of Jinja2 templating. I’ve discovered it helpful for together with conditional textual content and sending attachments. Here is a posh template and the corresponding database:

$ cat mailmerge_template.txt
TO: Email
SUBJECT: Flock 2019 Funding Offer
FROM: Brian Exelbierd
ATTACHMENT: attachments/File

Hi ,

I'm writing you on behalf of the Flock funding committee.  You requested funding in your attendance at Flock.  After cautious consideration we're in a position to give you the next funding:

Travel Budget:

Lodging: Lodging within the resort Wednesday-Sunday (four nights)
%- endif %

<<snip>>

$ cat mailmerge_database.csv
Name,Email,Travel_Budget,Hotel,File
Brian,bexelbie@redhat.com,1000,Yes,visa_bex.pdf
PersonA,persona@fedoraproject.org,1500,No,visa_person_a.pdf
PèrsonB,personb@fedoraproject.org,500,Yes,visa_person_b.pdf

There are two new issues on this electronic mail. First, there’s an attachment. I’ve to ship visa invitation letters to worldwide vacationers to assist them come to Flock, and the ATTACHMENT a part of the header specifies which file to connect. To hold my listing clear, I put all of them in my Attachments subdirectory. Second, it consists of conditional details about a resort, as a result of some individuals obtain funding for his or her resort keep, and I would like to incorporate these particulars for many who do. This is completed with the if building:


Lodging: Lodging within the resort Wednesday-Sunday (four nights)
%- endif %

This works identical to an if in most programming languages. Jinja2 could be very expressive and may do multi-level situations. Experiment with making your life simpler by together with database components that management the contents of the e-mail. Using whitespace is necessary for electronic mail readability. The minus () symbols in if and endif are a part of how Jinja2 controls whitespace. There are a lot of choices, so experiment to see what appears greatest for you.

Also notice that I prolonged the database with two fields, Hotel and File. These are the values that management the inclusion of the resort textual content and supply the identify of the attachment. In my instance, PèrsonB and I obtained resort funding, whereas PersonA did not.

Doing a dry-run and sending the emails is similar whether or not you are utilizing a easy or a posh template. Give it a strive!

You also can experiment with utilizing conditionals (ifendif) within the header. You can, for instance, have an attachment provided that one is within the database, or possibly you might want to change the sender’s identify for some emails however not others.

Mailmerge’s benefits

The Mailmerge program gives a robust however easy technique of sending a lot of personalized emails. Everyone will get solely the knowledge they want, and extraneous steps and particulars are omitted.

Even for easy group emails, I’ve discovered this technique far more efficient than sending one electronic mail to a bunch of individuals utilizing CC or BCC. Lots of people filter their electronic mail and delay studying something not despatched on to them. Using Mailmerge ensures that each particular person will get their very own electronic mail. Messages will filter correctly for the recipient and nobody can by accident “reply all” to your entire group.

Most Popular

breakingExpress.com features the latest multimedia technologies, from live video streaming to audio packages to searchable archives of news features and background information. The site is updated continuously throughout the day.

Copyright © 2017 Breaking Express, Green Media Corporation

To Top