You may need send some informative mails within your devices over IoT devices.
Now a days, sending an e-mail from devices, is almost the same as sending a SMS from Cell/Smart phones. I here try to explain the basics of smtplib python library and sending a simple e-mail over smtplib python library, line by line.
We will use, smtplib.SMTP for login Gmail or any other SMTP mail server with support of starttls authentication. For defining running path of python;
We're going to use smtplib library
In python 3.5 MIMEMultipart, MIMEBase and MIMEText is in email.mime; You can create a new object structure by creating Message instances, adding attachments and all the appropriate headers manually. For MIME messages though, the email package provides some convenient subclasses to make things easier. All encoder functions take exactly one argument, the message object to encode. They usually extract the payload, encode it, and reset the payload to this newly encoded value. They should also set the Content-Transfer-Encoding header as appropriate.
from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMEText from email import encoders
In python 3.5 https://docs.python.org/3.5/library/os.path.html; for returning the base name of pathname path we're going to use; os.path.basename, so we should import os, or use "from os.path import basename" will do the same job with using less source for this situation.
from os.path import basename
We should define login credentials for starttls login;
user_name = "email@example.com" user_pass = "p@55w0rd"
Defining function for sendin mails;
def mail(to, subject, text, attach):
We should setup the basics of an e-mail message. If you want to add BCC or CC you should remove the "#" at the beginning.
msg = MIMEMultipart() msg['From'] = user_name msg['To'] = to #msg['Bcc'] = user_name #msg['Cc'] = user_name msg['Subject'] = subject
Attachment adding; here we use os.path.basement
msg.attach(MIMEText(text)) part = MIMEBase('application', 'octet-stream') part.set_payload(open(attach, 'rb').read()) encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % basename(attach)) msg.attach(part)
Mail server settings
mailServer = smtplib.SMTP("smtp.gmail.com", 587) mailServer.ehlo() mailServer.starttls() mailServer.ehlo() mailServer.login(user_name, user_pass) mailServer.sendmail(user_name, to, msg.as_string()) mailServer.quit() mailServer.close()
Using mail function mail(to, subject, text, attach);
mail("firstname.lastname@example.org", "Test me if you can!", "This is a email sent with python! =)", "C:/Users/UserName/Desktop/test.zip")
The result of the script should be;
>>> Mail send! >>>
You can run this script via idle, official python ide for running python scripts; This is a preview of the mail which was send by our code; You can download and run the python 3.5 code with your Python IDLE or directly from your Linux based OS, here you can find the instructions for how to run python codes in your Linux based OS.