#emailSender.py secrets = { 'ssid': '', 'pw': '', 'ip': '', 'gateway': '', 'dns': '', 'sender_email': '', 'sender_name': '', 'sender_password': '', 'recipient_email': '' } #The Code #The email function is driven by the umail module. This has been published on GitHub by shawwwn. #To make use of the module we will need to download it from GitHub and then copy it over to our Pico. #I found this most easily accomplished by first downloading the file to the main computer and then #going File >> Open on Thonny and selecting the appropriate file. From there go File >> Save as… #and select the Pico as the location to save the file (making sure to save it with the appropriate #name (umail.py)). # #Our code is fairly straight forward in that it imports the appropriate modules, creates the WiFi #connection and then sends the email (not forgetting the secrets file) and looks like the following; import network import time import umail from secrets import secrets # Set up Wifi ssid = secrets['ssid'] password = secrets['pw'] rp2.country('NZ') # change to your country code wlan = network.WLAN(network.STA_IF) ip = secrets['ip'] netmask = secrets['netmask'] gateway = secrets['gateway'] dns = secrets['dns'] wlan.active(True) # activate the interface if not wlan.isconnected(): # check if connected to an AP print('Connecting to network...') wlan.connect(ssid, password) # connect to an AP wlan.ifconfig((ip,netmask,gateway,dns)) while not wlan.isconnected(): # wait till we are connected print('.', end='') time.sleep(0.1) print() print('Connected:', wlan.isconnected()) else: print("Already connected!") # Email details sender_email = secrets['sender_email'] sender_name = secrets['sender_name'] sender_password = secrets['sender_password'] recipient_email = secrets['recipient_email'] email_subject ='Test Email from Raspberry Pi Pico' # Connect to Gmail via SSL smtp = umail.SMTP('smtp.gmail.com', 465, ssl=True) # Login to the email account using the senders password smtp.login(sender_email, sender_password) # Specify the recipient smtp.to(recipient_email) # Write the email header smtp.write("From:" + sender_name + "<"+ sender_email+">\n") smtp.write("Subject:" + email_subject + "\n") # Write the body of the email smtp.write("Roses are red.\n") smtp.write("Violets are blue.\n") smtp.write("...\n") # Send the email smtp.send() # Quit the email session #emailSender.py