Page 1 of 1

Error when sending email on raspberry pi.

Posted: Wed Jul 15, 2020 7:22 am
by Gigiux
Hello,
I have written a code in python/kivy that sends an alert email:

Code: Select all

subject = "WARNING"
msg = "To: %s\n" % app.mailText
msg += "From: %s\n" % "me@gmail"
msg += "Subject: %s\n\n" % subject
msg += "%s has missed once the pill collection on %s at %d:00" \
         % (app.nameText, week_day, self.current_hour)
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=ssl.create_default_context()) as server:
          server.login("me", "<password>")
          server.sendmail("me@gmail", app.mailText, msg)
It works on Chromebook with Linux extension, but when the block run in Raspberry I get:

Code: Select all

INFO   ] [Base        ] Leaving application in progress...
 Traceback (most recent call last):
   File "./switchboard.py", line 704, in <module>
     SwitchboardApp().run()
   File "/home/pi/.local/lib/python3.7/site-packages/kivy/app.py",
line 855, in run
     runTouchApp()
   File "/home/pi/.local/lib/python3.7/site-packages/kivy/base.py",
line 504, in runTouchApp
     EventLoop.window.mainloop()
   File "/home/pi/.local/lib/python3.7/site-packages/kivy/core/window/window_sdl2.py",
line 747, in mainloop
     self._mainloop()
   File "/home/pi/.local/lib/python3.7/site-packages/kivy/core/window/window_sdl2.py",
line 479, in _mainloop
     EventLoop.idle()
   File "/home/pi/.local/lib/python3.7/site-packages/kivy/base.py",
line 339, in idle
     Clock.tick()
   File "/home/pi/.local/lib/python3.7/site-packages/kivy/clock.py",
line 591, in tick
     self._process_events()
   File "kivy/_clock.pyx", line 384, in kivy._clock.CyClockBase._process_events
   File "kivy/_clock.pyx", line 414, in kivy._clock.CyClockBase._process_events
   File "kivy/_clock.pyx", line 412, in kivy._clock.CyClockBase._process_events
   File "kivy/_clock.pyx", line 167, in kivy._clock.ClockEvent.tick
   File "./switchboard.py", line 223, in update_time
     server.sendmail("me@gmail", app.mailText, msg)
   File "/usr/lib/python3.7/smtplib.py", line 881, in sendmail
     raise SMTPRecipientsRefused(senderrs)
 smtplib.SMTPRecipientsRefused: {'': (555, b'5.5.2 Syntax error.
u5sm7840570ejz.15 - gsmtp')}
Where is the 'syntax error'? Or is some extra setting I should look for?
Thank you

Re: Error when sending email on raspberry pi.

Posted: Wed Jul 15, 2020 8:54 am
by pcmanbob
So looking at your code you seem to be using port 465 for the gmail server you might try port 587 as that what I have always used.

Have you enabled allow less secure apps on the gmail account you are using to send from.

its also worth opening the account in question from a browser and looking to see if there are any mails from google warning you of a security problem, this often gives you a clue as to the problem.

lastly if non of that helps try this example code just add your account details between the quotes for the user/password & send to address , this I know works as I use a version of it on several pi to email the IP on boot.

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()

Re: Error when sending email on raspberry pi.

Posted: Wed Jul 15, 2020 11:31 am
by neilgl
Yes, what pcmanbob said and noting that port 465 works for me using smtplib and import of EmailMessage. I also enabled less secure apps on gmail account.