Instapy: Get Email Notification

Created on 9 Sep 2017  路  6Comments  路  Source: timgrossmann/InstaPy

Hello,

Is there any way already or can be added for getting an email notification when the script ends, with a small summary of the session? it would be a good feature to get a daily summary when using cron jobs.

Feature enhancement opinion wontfix

Most helpful comment

@imewish I set-up text messages on success and failure of the bot using Twilio. See Pull Request #488 Twilio supports email, as well. If there is further interest we could tweak to include the summaries. @converge is looking to move these type of features into a plugin structure.

The actual PR isn't updated, but you could copy the code and create a text_user.py file and place in your InstaPy/instapy directory and then use the quickstart.py configuration as documented in the comments of the PR.

All 6 comments

623 what do you think about this?

Sounds like a good idea

@imewish I set-up text messages on success and failure of the bot using Twilio. See Pull Request #488 Twilio supports email, as well. If there is further interest we could tweak to include the summaries. @converge is looking to move these type of features into a plugin structure.

The actual PR isn't updated, but you could copy the code and create a text_user.py file and place in your InstaPy/instapy directory and then use the quickstart.py configuration as documented in the comments of the PR.

That would be great!

@imewish - email's pretty easy ... you don't even need to set up a SMTP server (can be a hassle) if you use GMAIL:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

def timestamp():
    '''
    generate timestamp
    '''
    t = datetime.datetime.strftime(datetime.datetime.now(), '%a %b %d %H:%M')
    return t

def send_email_html(sender, recipient, subject, body, files_to_attach ):

    msg = MIMEMultipart() 
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = ', '.join(recipient)

    font_family = "Courier"
    #font_family = "Verdana"

    body_html = """\
    <html>
    <head></head>
    <body>
    <pre style=\"font-family:"""+font_family+"""; text-align:left; font-size:12px\">
    """+body+"""
    </pre>
    </body>
    </font>
    </html>
    """

    #part1 = MIMEText(body, 'plain')
    part2 = MIMEText(body_html, 'html')

    #msg.attach(part1)
    msg.attach(part2)

    for f in files_to_attach:

        if any(suf in f for suf in ['.png', '.jpg'] ):
            img_filename = f
            fp = open(img_filename, 'rb')
            msgImg = MIMEImage(fp.read(), name=img_filename)
            fp.close()
            #msgImg.add_header('Content-ID', '<image1>')
            #msgImg.add_header('Content-Disposition', 'inline', filename=img_filename)
            msg.attach(msgImg)
        elif any(suf in f for suf in ['.txt', '.log', '.dat'] ):
            txt_filename = f
            f = open(txt_filename,'r')
            msgTxt = MIMEText(f.read())
            msgTxt.add_header('Content-Disposition', 'attachment', filename=txt_filename)
            msg.attach(msgTxt)
        else:
            pass

    ### use this below for regular SMTP (must set it up)
    #s = smtplib.SMTP('localhost')

    ## use GMAIL mail sender account to send notification emails
    s = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    s.ehlo()
    s.login('[email protected]', 'YourP@$$w0rd')

    s.sendmail(sender, recipient, msg.as_string())
    s.quit()

# -----

sender = '[email protected]'
recipient = ['[email protected]','[email protected]']
subject = 'status report - '+timestamp()

## 

body = '\n'
try:
    f = open('log.txt', 'r')
    body += f.read()
    f.close()
    body += '\n'
except:
    pass

##

files_to_attach = ["plot1.png","plot2.jpg"]

# -----

send_email_html(sender, recipient, subject, body, files_to_attach )

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. > If this problem still occurs, please open a new issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

converge picture converge  路  3Comments

seuraltimez picture seuraltimez  路  3Comments

ingorichter picture ingorichter  路  3Comments

ghost picture ghost  路  3Comments

CodeMaster1 picture CodeMaster1  路  3Comments