Send Email using Python

Tram Ho

I recently learned about Python 3 to see how to shoot a bunch of emails. There may be many simpler methods to do this, but the following ways I find effective at least for me.

And here’s the script: You have the names and email addresses of a range of contacts. And you want to send a message to each of those contacts, while adding a loved one “Dear [name]” name at the top of the message.

For simplicity, you can store contact details in a file rather than a database. You can also store samples of the messages you want to send in a file.

The Python smtplib module basically all you need is simple email, without any such subject line or additional information. But for real email, you need a subject line and a lot of information – maybe even pictures and attachments.

This is where Python’s email package appears. Remember that email messages cannot be sent just by package emails. You need a combination of both email and smtplib.

Here are four basic steps to sending email in Python: 1. Set up the SMTP server and log in to your account.

2. Create MIMEMultipart message object and load it with title that matches From, To and Subject fields

3. Add your message body.

4.Send the message using the SMTP server object.

Now let me guide you through the whole process

Suppose you have a contact file mycontacts.txt as follows:

Each line represents a unique contact. We have the name followed by the email address. I’m storing everything in lowercase. I’ll put it according to the programming logic to convert any field to uppercase or lowercase if needed. All of that is pretty easy in Python.

Next, we have the sample message.txt. file message.txt.

Notice the word ${PERSON_NAME}? It is a sample string in Python. Sample strings can easily be replaced with other strings; in this example, ${PERSON_NAME} will be replaced with that person’s real name, as you’ll see shortly.

Now let’s start with the Python code, we need to read the contact from the file mycontacts.txt . We can also generalize this bit into its own function.

The get_contacts() function get_contacts() the file name as its argument. It will open the file, read each line (i.e. each contact), divide it into a name and email, and then join them into two separate lists. Finally, two lists are returned from the function.

We also need a function to read in the template file (like message.txt ) and return an object Template created from its contents.

Just like the previous function, this function takes the file name as its argument.

To send email, you need to use SMTP (Simple Mail Transfer Protocol) . As mentioned earlier, Python provides libraries to handle this task.

In the code above, you are entering smtplib and then creating an SMTP version that encapsulates the SMTP connection. It takes the parameters of the server address and the port number, both of which depend entirely on the SMPT settings of your specific email service provider. For example, in the case of Outlook, line 4 above would be:

You should use the server address and port number of your specific email service provider for the entire operation.

MY_ADDRESS and PASSWORD above are two variables that contain the email address and full password of the account you will use.

Now would be a good time to get contact information and message templates using the functions we defined above.

Now, for each of those contacts, send a separate letter.

For each name and emai (from the directory file), you are creating a MIMEMultipar object, setting From , To , Subjectcontent-type headers as a keyword dictionary, and then attaching the message contents. MIMEMultipart object in the form of simple text. You may want to read the documentation to learn more about other MIME types that you can test.

Also note that on line 10 above, I will replace ${PERSON_NAME} with the actual name extracted from the directory file with the template creation mechanism in Python.

In this particular example, I will delete MIMEMultipart object and recreate it every time you loop through the loop.

Once that’s done, you can send the message using the send_message () function of the SMTP object you created earlier. Here is the full code:

I believe that after this tutorial, sending emails by pyhon has become much easier than before.

Please use and configure it to best suit your usage ?

In addition to the official Python documents, I have consulted other sources here ? http://naelshiab.com/tutorial-send-email-python/

References:

1. https://docs.python.org/3/library/smtplib.html

2. https://www.python.org/

Share the news now

Source : Viblo