top of page
  • Black Facebook Icon
  • Black Instagram Icon
  • Black Pinterest Icon

Send email using Raspberry pi python program

Updated: Jan 14, 2022

In this post, am sharing you some useful code for sending email through python. In this program, we will use the python’s smtplib library to send emails using raspberry pi


here we discuss 2 types of mail sending codes, one is simple email sending and other is sending email with attachments


The below codes are tested to work on python 3.6 and onward versions of Python


Simple e-mail and email with attachment

import smtplib

server = smtplib.SMTP('smtp.gmail.com',587)

server.starttls()

server.login("vtsproject006","vidya1234")

msg = 'test msg'

server.sendmail("vtsproject006","amitrana3348@gmail.com",msg)

server.quit()


Send email using raspberry pi with subject

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
fromaddr = "YOUR ADDRESS"
toaddr = "ADDRESS YOU WANT TO SEND TO"

msg = MIMEMultipart()

msg['From'] = fromaddr

msg['To'] = toaddr

msg['Subject'] = "SUBJECT OF THE MAIL"




body = "YOUR MESSAGE HERE"

msg.attach(MIMEText(body, 'plain'))




server = smtplib.SMTP('smtp.gmail.com', 587)

server.starttls()

server.login(fromaddr, "YOUR PASSWORD")

text = msg.as_string()

server.sendmail(fromaddr, toaddr, text)

server.quit()


Send email with Attachment using raspberry pi

import smtplib

from email.MIMEMultipart import MIMEMultipart

from email.MIMEText import MIMEText

from email.MIMEBase import MIMEBase

from email import encoders

fromaddr = "YOUR EMAIL"

toaddr = "EMAIL ADDRESS YOU SEND TO"

msg = MIMEMultipart()

msg['From'] = fromaddr

msg['To'] = toaddr

msg['Subject'] = "SUBJECT OF THE EMAIL"

body = "TEXT YOU WANT TO SEND"

msg.attach(MIMEText(body, 'plain'))

filename = "NAME OF THE FILE WITH ITS EXTENSION"

attachment = open("PATH OF THE FILE", "rb")

part = MIMEBase('application', 'octet-stream')

part.set_payload((attachment).read())

encoders.encode_base64(part)

part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

msg.attach(part)

server = smtplib.SMTP('smtp.gmail.com', 587)

server.starttls()

server.login(fromaddr, "YOUR PASSWORD")

text = msg.as_string()

server.sendmail(fromaddr, toaddr, text)

server.quit()

댓글 2개


게스트
2022년 3월 26일

Since gmail has made it just about impossible to use their SMTP server in this way, how would I set up the SMTP outgoing service that my web hosting service provides?

좋아요
게스트
2023년 5월 31일
답글 상대:

Add the raspberry pi to your google account under App Passwords and youll get a unique code. use that on your pi coding instead of your normal password. worked for me. hope that helps!

좋아요

Enroll to Free Courses

Get In Touch

Address: Decent Enclave, 2nd Floor, Darga Rd, opp. More Mart, Ulkanagari, Aurangabad, Maharashtra 431001

Contact : +91-9404865609

Email

support (at) kitflix.com

  • Instagram
  • Facebook
  • Twitter
  • LinkedIn
  • YouTube

© 2022 by Kitflix Technologies Private Limited.

bottom of page