Kanna
Posts: 16
Joined: Wed Feb 12, 2020 2:51 pm

i am not able to send email using smtp mailutils

Tue May 05, 2020 2:00 pm

I followed all steps mentioned in the video
i made all attemps by giving access to low secure app and changing the password
i am getting error as
smtpUser , smtpPass not accepted
below is the youtude link i followed

https://youtu.be/0kpGcMjpDcw

pcmanbob
Posts: 9464
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: i am not able to send email using smtp mailutils

Tue May 05, 2020 2:48 pm

I assume you are using raspbian buster ? ( not specified in post )

so what did you install along with mailutils ?

SSMTP or MSMTP ?

as ssmtp does not work with raspbian buster,

You need to install MSMTP in its place after uninstalling SSMTP

see this thread of configuration / set up.

viewtopic.php?f=28&t=244147#p1517480
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Kanna
Posts: 16
Joined: Wed Feb 12, 2020 2:51 pm

Re: i am not able to send email using smtp mailutils

Wed May 06, 2020 2:06 am

Thank for your information now i will try msmtp

Kanna
Posts: 16
Joined: Wed Feb 12, 2020 2:51 pm

Re: i am not able to send email using smtp mailutils

Wed May 06, 2020 3:54 am

Now i am able emails using echo command
actually i am trying it for email alert using moisture sensor but it is not working

Code: Select all

import RPi.GPIO as GPIO # This is the GPIO library we need to use the GPIO pins on the Raspberry Pi
import smtplib # This is the SMTP library we need to send the email notification
import time # This is the time library, we need this so we can use the sleep function

# Define some variables to be used later on in our script

# You might not need the username and password variable, depends if you are using a provider or if you have your raspberry pi setup to send emails
# If you have setup your raspberry pi to send emails, then you will probably want to use 'localhost' for your smtp_host

smtp_username = "enter_username_here" # This is the username used to login to your SMTP provider
smtp_password = "enter_password_here" # This is the password used to login to your SMTP provider
smtp_host = "smtp.gmail.com" # This is the host of the SMTP provider
smtp_port = 587 # This is the port that your SMTP provider uses

smtp_sender = "sender@email.com" # This is the FROM email address
smtp_receivers = ['receiver@email.com'] # This is the TO email address

# The next two variables use triple quotes, these allow us to preserve the line breaks in the string. 

# This is the message that will be sent when NO moisture is detected

message_dead = """From: Sender Name <sender@email.com>
To: Receiver Name <receiver@email.com>
Subject: Moisture Sensor Notification
Warning, no moisture detected! Plant death imminent!!! :'(
"""

# This is the message that will be sent when moisture IS detected again

message_alive = """From: Sender Name <sender@email.com>
To: Receiver Name <receiver@email.com>
Subject: Moisture Sensor Notification
Panic over! Plant has water again :)
"""

# This is our sendEmail function

def sendEmail(smtp_message):
	try:
		smtpObj = smtplib.SMTP(smtp_host, smtp_port)
		smtpObj.login(smtp_username, smtp_password) # If you don't need to login to your smtp provider, simply remove this line
		smtpObj.sendmail(smtp_sender, smtp_receivers, smtp_message)         
		print "Successfully sent email"
	except smtplib.SMTPException:
		print "Error: unable to send email"

# This is our callback function, this function will be called every time there is a change on the specified GPIO channel, in this example we are using 17

def callback(channel):  
	if GPIO.input(channel):
		print "LED off"
		sendEmail(message_dead)
	else:
		print "LED on"
		sendEmail(message_alive)

# Set our GPIO numbering to BCM
GPIO.setmode(GPIO.BCM)

# Define the GPIO pin that we have our digital output from our sensor connected to
channel = 17
# Set the GPIO pin to an input
GPIO.setup(channel, GPIO.IN)

# This line tells our script to keep an eye on our gpio pin and let us know when the pin goes HIGH or LOW
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
# This line asigns a function to the GPIO pin so that when the above line tells us there is a change on the pin, run this function
GPIO.add_event_callback(channel, callback)

# This is an infinte loop to keep our script running
while True:
	# This line simply tells our script to wait 0.1 of a second, this is so the script doesnt hog all of the CPU
	time.sleep(0.1)
Attachments
Webp.net-compress-image (3).jpg
Webp.net-compress-image (3).jpg (92.29 KiB) Viewed 348 times

pcmanbob
Posts: 9464
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: i am not able to send email using smtp mailutils

Wed May 06, 2020 9:48 am

You did not say you were trying to use smtplib.

try this example code

Code: Select all

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders

email_user = "account@gmail.com"
email_password = "password"
email_send = "address to send to"

subject = "Test email from pi"

msg = MIMEMultipart()
msg["From"] = email_user
msg["To"] = email_send
msg["Subject"] = subject

body = "Hi there, sending this email from Python!"
msg.attach(MIMEText(body,"plain"))

text = msg.as_string()
server = smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
server.login(email_user,email_password)


server.sendmail(email_user,email_send,text)
server.quit()
you need to put in your mail account details including password and the address to send the mail to, putting the information between the "... "

this is what I use on may of my pi projects to send mail from python so I know it works.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Kanna
Posts: 16
Joined: Wed Feb 12, 2020 2:51 pm

Re: i am not able to send email using smtp mailutils

Wed May 06, 2020 2:49 pm

Thank you sir example code you post worked
now the problem i encountered is how link this with soil moisture sensor
i am not getting the idea to built the code

pcmanbob
Posts: 9464
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: i am not able to send email using smtp mailutils

Wed May 06, 2020 4:13 pm

So using you posted code I added my example code to send email to yours and I changed to time.sleep in the while true loop to 1 second as all it is doing is keeping the script running so no need to loop so quickly.

Code: Select all

import RPi.GPIO as GPIO # This is the GPIO library we need to use the GPIO pins on the Raspberry Pi
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import time # This is the time library, we need this so we can use the sleep function

# This is our sendEmail function
def send_mail(body):
    email_user = "account@gmail.com"
    email_password = "password"
    email_send = "address to send to"

    subject = "Moisture Sensor Notification"

    msg = MIMEMultipart()
    msg["From"] = email_user
    msg["To"] = email_send
    msg["Subject"] = subject

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

    text = msg.as_string()
    server = smtplib.SMTP("smtp.gmail.com",587)
    server.starttls()
    server.login(email_user,email_password)

    server.sendmail(email_user,email_send,text)
    server.quit()


# This is our callback function, this function will be called every time there is a change on the specified GPIO channel, in this example we are using 17

def callback(channel):  
	if GPIO.input(channel):
		print "LED off"
		body = "Warning, no moisture detected! Plant death imminent!!! :'("
        send_mail(body)
	else:
		print "LED on"
        body = "Panic over! Plant has water again :)"
        send_mail(body)

# Set our GPIO numbering to BCM
GPIO.setmode(GPIO.BCM)

# Define the GPIO pin that we have our digital output from our sensor connected to
channel = 17
# Set the GPIO pin to an input
GPIO.setup(channel, GPIO.IN)

# This line tells our script to keep an eye on our gpio pin and let us know when the pin goes HIGH or LOW
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
# This line asigns a function to the GPIO pin so that when the above line tells us there is a change on the pin, run this function
GPIO.add_event_callback(channel, callback)

# This is an infinte loop to keep our script running
while True:
	# This line simply tells our script to wait 1 of a second, this is so the script doesnt hog all of the CPU
	time.sleep(1)
this is untested so expect errors.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Kanna
Posts: 16
Joined: Wed Feb 12, 2020 2:51 pm

Re: i am not able to send email using smtp mailutils

Thu May 07, 2020 9:38 am

when the sensor detects moisture, the output is LOW (0V). When the sensor can no longer detect moisture the output is HIGH (3.3V).
sensor working condition

Code: Select all

import RPi.GPIO as GPIO
import time
import datetime

def check(channel):
    print datetime.datetime.now()
    if GPIO.input(channel):
        print('No moisture detected')
       
    else:
        print('Moisture detected')

GPIO.setmode(GPIO.BCM)


channel = 17

GPIO.setup(channel, GPIO.IN)

 
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)

GPIO.add_event_callback(channel, check)


while True:
	time.sleep(0.1)
I want to run code based on time that is for example every 1 hour or 2 hours gap
I want to send the following message alerts based on if and else statements
subject = "Status from moisture sensor"
for if statement i need to send
body="Warning, no moisture detected! Plant death imminent!!!"
and for else statement i need send
body="No need to water your plant"

pcmanbob
Posts: 9464
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: i am not able to send email using smtp mailutils

Thu May 07, 2020 10:54 am

Kanna wrote:
Thu May 07, 2020 9:38 am
when the sensor detects moisture, the output is LOW (0V). When the sensor can no longer detect moisture the output is HIGH (3.3V).
sensor working condition

Code: Select all

import RPi.GPIO as GPIO
import time
import datetime

def check(channel):
    print datetime.datetime.now()
    if GPIO.input(channel):
        print('No moisture detected')
       
    else:
        print('Moisture detected')

GPIO.setmode(GPIO.BCM)


channel = 17

GPIO.setup(channel, GPIO.IN)

 
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)

GPIO.add_event_callback(channel, check)


while True:
	time.sleep(0.1)
I want to run code based on time that is for example every 1 hour or 2 hours gap
I want to send the following message alerts based on if and else statements
subject = "Status from moisture sensor"
for if statement i need to send
body="Warning, no moisture detected! Plant death imminent!!!"
and for else statement i need send
body="No need to water your plant"
See my post above dated 06 May 2020, 17:13 used your previously posted code as a base and added my email code to it.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Kanna
Posts: 16
Joined: Wed Feb 12, 2020 2:51 pm

Re: i am not able to send email using smtp mailutils

Fri May 08, 2020 11:13 am

Thank you so much sir code worked well
Final step of my project is to merge two code
i am strucked at this step help me sir

Code: Select all

import sys
import RPi.GPIO as GPIO
import os
import Adafruit_DHT
import urllib2
import smbus
import time
from ctypes import c_short
DHTpin=14
key='92L7QM03PXTZU0B2'
GPIO.setmode(GPIO.BCM)
def readDHT():
    humi, temp=Adafruit_DHT.read_retry(Adafruit_DHT.DHT11,DHTpin)
    return(str(int(humi)),str(int(temp)))
def main():
    print ('system ready...')
    URL='https://api.thingspeak.com/update?api_key=%s'% key
    print ('wait..')
    while True:
        (humi,temp)=readDHT()
        finalURL=URL+'&field1=%s&field2=%s'%(humi,temp)
        print (finalURL)
        s=urllib2.urlopen(finalURL);
        print (humi +  ' ' + temp + ' ')
        s.close()
        time.sleep(10)
       
if __name__=='__main__':
    main()

Code: Select all

import RPi.GPIO as GPIO # This is the GPIO library we need to use the GPIO pins on the Raspberry Pi
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import time # This is the time library, we need this so we can use the sleep function

# This is our sendEmail function
def send_mail(body):
    email_user = "account@gmail.com"
    email_password = "password"
    email_send = "address to send to"

    subject = "Moisture Sensor Notification"

    msg = MIMEMultipart()
    msg["From"] = email_user
    msg["To"] = email_send
    msg["Subject"] = subject

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

    text = msg.as_string()
    server = smtplib.SMTP("smtp.gmail.com",587)
    server.starttls()
    server.login(email_user,email_password)

    server.sendmail(email_user,email_send,text)
    server.quit()


# This is our callback function, this function will be called every time there is a change on the specified GPIO channel, in this example we are using 17

def callback(channel):  
	if GPIO.input(channel):
		print "LED off"
		body = "Warning, no moisture detected! Plant death imminent!!! :'("
       	        send_mail(body)
	else:
		print "LED on"
        	body = "Panic over! Plant has water again :)"
        	send_mail(body)

# Set our GPIO numbering to BCM
GPIO.setmode(GPIO.BCM)

# Define the GPIO pin that we have our digital output from our sensor connected to
channel = 17
# Set the GPIO pin to an input
GPIO.setup(channel, GPIO.IN)

# This line tells our script to keep an eye on our gpio pin and let us know when the pin goes HIGH or LOW
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
# This line asigns a function to the GPIO pin so that when the above line tells us there is a change on the pin, run this function
GPIO.add_event_callback(channel, callback)

# This is an infinte loop to keep our script running
while True:
	# This line simply tells our script to wait 1 of a second, this is so the script doesnt hog all of the CPU
	time.sleep(1)
  


pcmanbob
Posts: 9464
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: i am not able to send email using smtp mailutils

Fri May 08, 2020 12:25 pm

This is a simple copy and paste job , copying over the setup/import/function lines from the first program in to the second, then just adding the code from the main function in the first program to the keep alive while true loop on the second program.

Code: Select all

import sys
import os
import Adafruit_DHT
import urllib2
import smbus
from ctypes import c_short
import RPi.GPIO as GPIO # This is the GPIO library we need to use the GPIO pins on the Raspberry Pi
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import time # This is the time library, we need this so we can use the sleep function

DHTpin=14
def readDHT():
    humi, temp=Adafruit_DHT.read_retry(Adafruit_DHT.DHT11,DHTpin)
    return(str(int(humi)),str(int(temp)))

# This is our sendEmail function
def send_mail(body):
    email_user = "account@gmail.com"
    email_password = "password"
    email_send = "address to send to"

    subject = "Moisture Sensor Notification"

    msg = MIMEMultipart()
    msg["From"] = email_user
    msg["To"] = email_send
    msg["Subject"] = subject

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

    text = msg.as_string()
    server = smtplib.SMTP("smtp.gmail.com",587)
    server.starttls()
    server.login(email_user,email_password)

    server.sendmail(email_user,email_send,text)
    server.quit()


# This is our callback function, this function will be called every time there is a change on the specified GPIO channel, in this example we are using 17

def callback(channel):  
	if GPIO.input(channel):
		print "LED off"
		body = "Warning, no moisture detected! Plant death imminent!!! :'("
       	        send_mail(body)
	else:
		print "LED on"
        	body = "Panic over! Plant has water again :)"
        	send_mail(body)

# Set our GPIO numbering to BCM
GPIO.setmode(GPIO.BCM)

# Define the GPIO pin that we have our digital output from our sensor connected to
channel = 17
# Set the GPIO pin to an input
GPIO.setup(channel, GPIO.IN)

# This line tells our script to keep an eye on our gpio pin and let us know when the pin goes HIGH or LOW
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
# This line asigns a function to the GPIO pin so that when the above line tells us there is a change on the pin, run this function
GPIO.add_event_callback(channel, callback)


key='92L7QM03PXTZU0B2'
print ('system ready...')
URL='https://api.thingspeak.com/update?api_key=%s'% key
print ('wait..')
# This is an infinte loop to keep our script running
while True:
    (humi,temp)=readDHT()
    finalURL=URL+'&field1=%s&field2=%s'%(humi,temp)
    print (finalURL)
    s=urllib2.urlopen(finalURL);
    print (humi +  ' ' + temp + ' ')
    s.close()
    time.sleep(10)



untested so expect errors.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Kanna
Posts: 16
Joined: Wed Feb 12, 2020 2:51 pm

Re: i am not able to send email using smtp mailutils

Sat May 09, 2020 11:30 am

Thank you so much sir you helped me alot
without your help i may not able to complete my project on time
once again thank you sir

Return to “Networking and servers”