raspberry pi

Raspberry Pi Send Email using Python

Send emails using Raspberry Pi:

Send emails using Raspberry Pi and Python Code- In this article you will learn how to send emails using Raspberry Pi and Python programming. After reading this article you will be able to send emails and you will also be able to send images taken with Raspberry Pi camera in an email. The Raspberry Pi code can be easily modified so this way you can also send sensors data in an email to a remote user. You can make amazing monitoring systems, Security Systems etc.

Sending e-mails in a Python program is not difficult in itself, but requires that various modules interact. The task for the first example is to send an email with both the subject and the message text also contain non-ASCII characters. For the real e-mail transport you must have access to an SMTP server. The abbreviation SMTP stands for Simple Mail Transfer Protocol. This is the usual protocol for sending of emails.


Amazon Purchase Links:

Raspberry Pi

raspberry pi 4 4gb kit

Wireless Keyboard and Mouse for raspberry pi:

Night vision Camera for Raspberry Pi:

Oled HDMI touch display for raspberry pi:

TOP10 Best Raspberry Pi Kits

Other Tools and Components:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

In the Python program, you must first write the actual email in a MIMEText Assemble object. It consists of a sender, one or more Recipients, the subject and the actual text. To send it, you create an SMTP object, with the Transfer the host name of the SMTP server. The starttls method initiates the encrypted Establishing a connection, which is now common with almost all SMTP servers. Then login carries out the authentication. sendmail finally sends the e-mail.

#! / usr / bin / python3

# Sendmail -text file - only .py

import smtplib, sys

from email. mime. text import MIMEText

from email. header import header

frm = 'Sender <sender @ myhost .com>'

to = 'recipient <. anyone @ gmail .com> '

subj = 'subject with äöüß'

msg = 'Message text with outer. \ nSecond line. \ nThird line.'

try:

mime = MIMEText (msg, 'plain', 'utf -8')

mime ['From'] = frm

mime ['To'] = to

mime ['Subject'] = Header (subj, 'utf -8')

smtp = smtplib. SMTP ("myhost. Com")

smtp. starttls ()

smtp. login ("login name", "top secret")

smtp. sendmail (frm, [to], mime. as_string ())

smtp. quit ()

except:

print ("An error occurred while sending the e-mail:",

sys. exc_info ())



Send an email with a bitmap

If you also want to send a picture with the email, that will Putting together the e-mail object a little more laborious: starting point is now a MIMEMultipart object that you can specify the sender, recipient, subject as well add the message text as MIMEText. You read the image file to be embedded first in a MIMEImage object and then add this object to the MIMEMultipart Object. Repeat this procedure if you have multiple images at once want to transfer.

#! / usr / bin / python3

# Sendmail .py file

import smtplib

from email. header import header

from email. mime. image import MIMEImage

from email. mime. multipart import MIMEMultipart

from email. mime. text import MIMEText

frm = 'Name <sender @ myhost .com>'

to = 'Another name <. anyone @ gmail .com> '

subj = 'subject'

msg = 'message text'

fn = 'tmp. jpg '# include this bitmap file

try:

# Compose email

mime = MIMEMultipart ()

mime ['From'] = frm

mime ['To'] = to

mime ['Subject'] = Header (subj, 'utf -8')

mime. attach (MIMEText (txt, 'plain', 'utf -8'))

# Add a picture

f = open (fn, 'rb')

img = MIMEImage (f. read ())

close ()

mime. attach (img)

# to ship

smtp = smtplib. SMTP ("...")

except:

print ("An error occurred while sending the e-mail:",

sys. exc_info ())


Send a recording of the Raspberry Pi camera

With this basic knowledge, it is no longer a problem to write a Python script program that first creates a recording with the Raspberry Pi camera and the photo is then sent via email.

#! / usr / bin / python3

# Sendmail file - photo .py

import picamera, smtplib, sys

from email. header import header

from email. mime. image import MIMEImage

from email. mime. multipart import MIMEMultipart

from email. mime. text import MIMEText

frm = 'Name <sender @ myhost .com>'

to = 'Another name <. anyone @ gmail .com> '

subj = 'photo'

msg = 'message text'

fn = 'photo. jpg '

try:

# Create photo

camera = picamera. PiCamera ()

camera. capture (fn, resize = (640, 480))

camera. close ()

# Compose email

mime = MIMEMultipart ()

mime ['From'] = frm

mime ['To'] = to

mime ['Subject'] = Header (subj, 'utf -8')

mime. attach (MIMEText (txt, 'plain', 'utf -8'))

# Add a picture

f = open (fn, 'rb')

img = MIMEImage (f. read ())

close ()

mime. attach (img)

# to ship

smtp = smtplib. SMTP ("...")

except:

print ("An error occurred while sending the e-mail:",

sys. exc_info ())

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button