This week we looked at sending texts and emails from our python scripts.
To send text messages you will need to set up an account on Twilio which is a platform that allows coders to make and receive phone calls, send and receive text messages from their programmes. You then install the Twilio python library which will allow us to send texts from our script using code like this.
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console # DANGER! This is insecure. See http://twil.io/secure account_sid = 'your account_sid' auth_token = 'your auth_token' client = Client(account_sid, auth_token) message = client.messages \ .create( body="Hello Bodgers", from_='+440123456789', to='+353123456789' ) print(message.sid)
To send an email we use smtplib which is an email library that’s built into python and which works well with Gmail. We need to change our Gmail setting to allow insecure apps and then we can use the code below to send our message.
import smtplib server = smtplib.SMTP('smtp.gmail.com', 485) server.starttls() server.login("firstname.lastname@example.org", "myPassword") msg = "Hello World" server.sendmail("email@example.com", "firstname.lastname@example.org", msg) server.quit()
See you all on Saturday
Declan, Dave and Alaidh