Send your new year greetings by email and text message using Python | by Yong Cui | Towards Data Science

It’s new year time. Although we developers don’t usually bother to maintain friendship (is it a joke or not?), we don’t mind sending our greetings to our friends as long as there is a programmatic way to get this done!
Here, I’m showing you how to send your greetings by email and text message using Python in two parts.
Python has a built-in library called smtplib — a module that can be used to create an SMTP client session object, allowing us to send emails to any email service providers implementing SMTP or ESMTP. Another useful module for email sending is the email package that provides email-related functionalities, such as reading and writing.
For the simplicity of this current tutorial, we’ll just send a plain text email. To do that, we’ll just need MIMEText to create the email text body. In a later tutorial, I can show you how to send a more complex email (e.g., image attachment).
import smtplibimport emailfrom email.mime.text import MIMEText
In this tutorial, I’m going to use a gmail account to send the emails. Please note that you need to update your gmail security setting (https://myaccount.google.com/lesssecureapps) to allow your email to be sent programmatically using Python.
email_host = "smtp.gmail.com"email_port = 587email_sender = "" # change it to your own gmail accountemail_password = "" # change it to your gmail passwordemail_receivers = ["xxx@gmail.com", "yyy@gmail.com", "zzz@gmail.com"] # the list of recipient email addresses
Next, you can compose your message. You can specify the sender as your own email account. For the list of the email that is intended to be sent to, you can use the following format: First, Last<xxxx@gmail.com>, which is a standard format of email recipient.
message = MIMEText("I wish you a great 2020.")message["From"] = ""  # your email accountmessage["To"] = "John Smith<xxxx@gmail.com>, Mike Dickson<yyyyy@hotmail.com>" # the list of email addressesmessage["Subject"] = "Happy New Year"
You use the host and port to create the needed SMTP session. During initialization, Python will automatically establish the SMTP connection by calling the connect method . We then start the TLS for security reasons.
You just need to authenticate your identity using the email account and password. As mentioned previously, for gmail, you have to loosen your security setting before you can login using this method.
You can simply call the sendmail method to send your message and quit the session.
email_server = smtplib.SMTP(email_host, email_port)email_server.starttls()email_server.login(email_sender, email_password)email_server.sendmail(email_sender, email_receivers, message.as_string())email_server.quit()
There are multiple ways actually you can use Python to send your new year greetings by text messages.
As many of you may be aware, many cellphone carriers allow you to send text messages using email. Here are just a short list for major carriers in the United State.
AT&T: cell-phone-number@txt.att.net
Sprint: cell-phone-number@messaging.sprintpcs.com
T-Mobile: cell-phone-number@tmomail.net
Verizon: cell-phone-number@vtext.com
So instead of using the email address for the email_receivers variable, you can simply use 1234567890@txt.att.net as one of the email recipients.
However, there is a catch as you may have to know the carrier associated that number. If you don’t know it, you can just write some code to create all possible variations (e.g., 1234567890@messaging.springtpcs.com, 1234567890@tmomail.net) to cover all possibilities. I’ve also heard that there is paid third-party service allowing you to look up a certain phone number’s carrier. But I guess it’s an overkill for the objective we’re trying to accomplish.
You can also send text messages using AWS SNS. Apparently, you need to sign up an AWS account, which is beyond the scope of the current tutorial. You can refer to the official website (https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/).
Run the code (pip install boto3) in Terminal or a command line tool. The boto3 library is a Python module developed for AWS-related management.
The SNS is an AWS product that allows the developers to send text messages and other communications (e.g., emails) to end users.
import boto3client = boto3.client(    "sns",    aws_access_key_id="", # your aws access key    aws_secret_access_key="", # your aws secrete access key    region_name="us-east-1")
To send your greeting to multiple phone numbers, you need to create a topic, which allows you to add subscribers to this topic. Each subscriber is specified by a phone number, which should also include the country code (e.g., +1 for US) — the E.164 formatting. Because we’re sending messages (i.e., short message service or sms), we specify the protocol to be sms.
topic = client.create_topic(Name="new_year") # create a topictopic_arn = topic['TopicArn']  # get its Amazon Resource Name# Add SMS Subscribers to this topicphone_numbers = ["+1234567890"] for phone_number in phone_numbers:    client.subscribe(        TopicArn=topic_arn,        Protocol='sms',        Endpoint=phone_number    )
Once you have added the subscribers to the topic, you’re ready to publish a message to the topic. You just need to provide the message and specify the topic on which you want to publish this message. By doing that, all the subscribers will receive the text message.
client.publish(Message="Happy New Year!", TopicArn=topic_arn)
You can also choose to send text messages using the Twilio. Certainly, it requires a Twilio account, which is free to register, similar to an AWS account. Once you create a Twilio account, we’re ready to play the game!
Run the command pip install twilio to install the Twilio module, which will give you access to various functionalities provided by Twilio. Text messaging is just one of them.
The Cilent method will be used to create a Twilio client by taking your Twilio account SID and AUTH Token, both of which can be found on the dashboard of your Twilio account.
from twilio.rest import Clientclient = Client(    "", # your Twilio Account SID    "", # your Twilio AUTH token)
You create a list of all the phone numbers to which you’ll send your new year greetings. Again, be noted that the phone number is using the E.164 formatting similar to the use of AWS SNS.
One catch is that with free Twilio account, you can only send text messages to you verified phone numbers, giving you less flexibility compared to AWS SNS. However, you can upgrade your Twilio account, which will give you an option of sending to multiple phone numbers of your choice at very low cost (less than 1 cent per message).
phone_numbers = ["+1234567890", "+1234567891"] # the list of phone numbers to receive the messagesfor phone_number in phone_numbers:    client.messages.create(        to=phone_number,        from_="", # your Twilio phone number        body="Happy New Year!"    )
By following this tutorial, you should have created multiple python scripts for each of these jobs. In the future, you can use these scripts to send emails and text messages very conveniently.
So actually, programming does make certain things easier, doesn’t it?