Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 send email after restart raspberry pi
#1
How raspberry send email when it restarting, I want receive email when raspberry is restarting, for example, when electricity drops.

Is it a possible?
Quote
#2
Of course it is.
https://realpython.com/python-send-email/

You can use the email and smtplib. A better API is provided by emails.
To start your script after booting, you can use:
  • a stupid init-script
  • /etc/rc.local
  • systemd
  • cron, google for @reboot cron
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#3
(Jun-05-2019, 10:15 AM)DeaD_EyE Wrote: Of course it is.
https://realpython.com/python-send-email/

You can use the email and smtplib. A better API is provided by emails.
To start your script after booting, you can use:
  • a stupid init-script
  • /etc/rc.local
  • systemd
  • cron, google for @reboot cron

Thank for your answer,but i am very beginner,can you write semple code and tell me what i do to start after restart raspberry?
Thank you very much
Quote
#4
I prefer the emails package. To use it, you have to install it on your Raspberry Pi.
Also you have to check the settings. Some providers do have different ports.

You can install the package as root:
pip3 install emails
or as unprivileged user:
pip3 install emails --user
or you can use a virtual environment as normal unprivileged user:
python3 -m venv python_env
source python_env/bin/activate
pip install emails
Later you have to use the symlinked binary python in the virtual environment.
YourHomePath/python_env/bin/python YourScript.py
Then you can try this code, after you have filled in all credentials, host, port, etc.
Try this, if this works.
Then the next step is to check, if @reboot works with cron.

import emails
import datetime

now_str = datetime.datetime.now().isoformat()

# you can also use only text
html_message = '''
    <h1>Restart at:</h1>
    <p>{}</p>
    '''.format(now_str)
text_message = 'Restart at:\n' + now_str 
subject = 'Restart'
mail_from = ('My Raspberry Pi', 'am1982@t-online.de')
to = ['am1982@t-online.de']
smtp = {
    'host': 'securesmtp.t-online.de',
    'port': 587,
    'tls': True,
    'timeout': 5,
    'user': 'am1982@t-online.de',
    'password': 'MySuperSecretPassword',
    }

message = emails.html(
    html=html_message, text=text_message,
    subject=subject, mail_from=mail_from
    )

result = message.send(to=to, smtp=smtp)
print(result.status_code, result.status_text)
snippsat likes this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#5
(Jun-06-2019, 01:12 PM)DeaD_EyE Wrote: I prefer the emails package. To use it, you have to install it on your Raspberry Pi.
Also you have to check the settings. Some providers do have different ports.

You can install the package as root:
pip3 install emails
or as unprivileged user:
pip3 install emails --user
or you can use a virtual environment as normal unprivileged user:
python3 -m venv python_env
source python_env/bin/activate
pip install emails
Later you have to use the symlinked binary python in the virtual environment.
YourHomePath/python_env/bin/python YourScript.py
Then you can try this code, after you have filled in all credentials, host, port, etc.
Try this, if this works.
Then the next step is to check, if @reboot works with cron.

import emails
import datetime

now_str = datetime.datetime.now().isoformat()

# you can also use only text
html_message = '''
    <h1>Restart at:</h1>
    <p>{}</p>
    '''.format(now_str)
text_message = 'Restart at:\n' + now_str 
subject = 'Restart'
mail_from = ('My Raspberry Pi', 'am1982@t-online.de')
to = ['am1982@t-online.de']
smtp = {
    'host': 'securesmtp.t-online.de',
    'port': 587,
    'tls': True,
    'timeout': 5,
    'user': 'am1982@t-online.de',
    'password': 'MySuperSecretPassword',
    }

message = emails.html(
    html=html_message, text=text_message,
    subject=subject, mail_from=mail_from
    )

result = message.send(to=to, smtp=smtp)
print(result.status_code, result.status_text)
i cant to install emails,it give me errors,i have smtplib if can with this.Thank you
Quote
#6
(Jun-06-2019, 02:04 PM)chano Wrote: i cant to install emails
What's the error?
Example working:
λ pip install emails
Collecting emails
Downloading 0.5.15-py2.py3-none-any.whl (57kB)
     |████████████████████████████████| 61kB 245kB/s
............
Installing collected packages: cssutils, cachetools, premailer, emails
Successfully installed cachetools-3.1.1 cssutils-1.0.2 emails-0.5.15 premailer-3.4.1
chano Wrote:i have smtplib if can with this
But do you have a SMTP service provider?
If have then the simplest way.
# my_email.py
import smtplib

def send_mail():
    server = smtplib.SMTP()
    server.connect('smtp.host.adress')
    server.sendmail('from@smtp.mail', 'to@gmail.com',
    'Subject: Camera down')
    print("Mail sent successfully")
    server.quit()

send_mail()
chano Wrote:How raspberry send email when it restarting, I want receive email when raspberry is restarting,
Usage of Cron,run script on reboot.
sudo crontab -e
Add:
@reboot python /home/pi/MyProgram/my_email.py &
Restart sudo reboot
Quote
#7
Hello,thank you all is ok.you are very good men.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  thonny and esp32/8266 on raspberry usb niala38 0 166 Mar-31-2019, 05:25 PM
Last Post: niala38
  BugTracker registration confirmation email not received bluejedi 2 814 Dec-15-2017, 04:34 PM
Last Post: bluejedi
  Bike Cadence/RPM displayed via Raspberry Pi 3 on Pimoroni Blinkt Ramasen 8 2,331 Sep-24-2017, 09:57 PM
Last Post: Ramasen

Forum Jump:


Users browsing this thread: 1 Guest(s)