flexget.plugins.output_email
Covered: 83 lines
Missed: 70 lines
Skipped 39 lines
Percent: 54 %
  1
import logging
  2
import smtplib
  3
import socket
  4
from email.message import Message
  5
from flexget.plugin import PluginError, PluginWarning, register_plugin
  7
log = logging.getLogger('email')
 10
class OutputEmail(object):
 12
    """
 13
        Send an e-mail with the list of all succeeded (downloaded) entries.
 15
        Config:
 16
          from          : the email address from which the email will be sent (required)
 17
          to            : the email address of the recipient (required)
 18
          smtp_host     : the host of the smtp server
 19
          smtp_port     : the port of the smtp server
 20
          smtp_login    : should we use anonymous mode or login to the smtp server ?
 21
          smtp_username : the username to use to connect to the smtp server
 22
          smtp_password : the password to use to connect to the smtp server
 23
          smtp_tls      : should we use TLS to connect to the smtp server ?
 24
          smtp_ssl      : should we use SSL to connect to the smtp server ?
 25
                          Due to a bug in python, this only works in python 2.6.3 and up
 26
          active        : is this plugin active or not ?
 28
        Config basic example:
 30
        email:
 31
          from: xxx@xxx.xxx
 32
          to: xxx@xxx.xxx
 33
          smtp_host: smtp.host.com
 35
        Config example with smtp login:
 37
        email:
 38
          from: xxx@xxx.xxx
 39
          to: xxx@xxx.xxx
 40
          smtp_host: smtp.host.com
 41
          smtp_port: 25
 42
          smtp_login: true
 43
          smtp_username: my_smtp_login
 44
          smtp_password: my_smtp_password
 45
          smtp_tls: true
 47
        Config multi-feed example:
 49
        global:
 50
          email:
 51
            from: xxx@xxx.xxx
 52
            to: xxx@xxx.xxx
 53
            smtp_host: smtp.host.com
 55
        feeds:
 56
          feed1:
 57
            rss: http://xxx
 58
          feed2:
 59
            rss: http://yyy
 60
            email:
 61
              active: False
 62
          feed3:
 63
            rss: http://zzz
 64
            email:
 65
              to: zzz@zzz.zzz
 67
        GMAIL example:
 68
            from: from@gmail.com
 69
            to: to@gmail.com
 70
            smtp_host: smtp.gmail.com
 71
            smtp_port: 587
 72
            smtp_login: true
 73
            smtp_username: gmailUser
 74
            smtp_password: gmailPassword
 75
            smtp_tls: true
 77
        Default values for the config elements:
 79
        email:
 80
          active: True
 81
          smtp_host: localhost
 82
          smtp_port: 25
 83
          smtp_login: False
 84
          smtp_username:
 85
          smtp_password:
 86
          smtp_tls: False
 87
          smtp_ssl: False
 88
    """
 90
    def validator(self):
 91
        from flexget import validator
 92
        email = validator.factory('dict')
 93
        email.accept('boolean', key='active')
 94
        email.accept('text', key='to', required=True)
 95
        email.accept('list', key='to', required=True).accept('text')
 96
        email.accept('text', key='from', required=True)
 97
        email.accept('text', key='smtp_host')
 98
        email.accept('integer', key='smtp_port')
 99
        email.accept('boolean', key='smtp_login')
100
        email.accept('text', key='smtp_username')
101
        email.accept('text', key='smtp_password')
102
        email.accept('boolean', key='smtp_tls')
103
        email.accept('boolean', key='smtp_ssl')
104
        return email
106
    def get_config(self, feed):
107
        config = feed.config['email']
108
        config.setdefault('active', True)
109
        config.setdefault('smtp_host', 'localhost')
110
        config.setdefault('smtp_port', 25)
111
        config.setdefault('smtp_login', False)
112
        config.setdefault('smtp_username', '')
113
        config.setdefault('smtp_password', '')
114
        config.setdefault('smtp_tls', False)
115
        config.setdefault('smtp_ssl', False)
116
        if not isinstance(config['to'], list):
117
            config['to'] = [config['to']]
118
        return config
120
    def on_feed_output(self, feed):
121
        """Count the email as an output"""
123
    def on_feed_exit(self, feed):
124
        """Send email at exit."""
125
        config = self.get_config(feed)
127
        if not config['active']:
128
            return
131
        if feed.manager.options.learn:
132
            return
135
        if not feed.accepted:
136
            return
139
        entries_count = len(feed.accepted)
140
        subject = '[FlexGet] %s : %d new entries downloaded' % (feed.name, entries_count)
141
        content = (u'Hi,\n'
142
                    'FlexGet has just downloaded %d new entries for feed %s :' % (entries_count, feed.name))
144
        for entry in feed.accepted:
145
            content += "\n - %s (%s)" % (entry['title'], entry['url'])
146
            entry_path = entry.get('path', feed.config.get('download'))
147
            entry_filename = entry.get('filename', entry['title'])
148
            if entry_path:
149
                content += " => %s (%s)" % (entry_path, entry_filename)
151
        content += "\n\n"
154
        message = Message()
155
        message['To'] = ','.join(config['to'])
156
        message['From'] = config['from']
157
        message['Subject'] = subject
158
        message.set_payload(content.encode('utf-8'))
159
        message.set_charset('utf-8')
162
        if feed.manager.options.test:
163
            log.info('Would send email : %s' % message.as_string())
164
        else:
165
            try:
166
                if config['smtp_ssl']:
167
                    import sys
168
                    if sys.version_info < (2, 6, 3):
169
                        raise PluginError('SSL email support requires python >= 2.6.3 due to python bug #4066, upgrade python or use TLS')
171
                    mailServer = smtplib.SMTP_SSL(config['smtp_host'], config['smtp_port'])
172
                else:
173
                    mailServer = smtplib.SMTP(config['smtp_host'], config['smtp_port'])
174
                    if config['smtp_tls']:
175
                        mailServer.ehlo()
176
                        mailServer.starttls()
177
            except socket.error, e:
178
                raise PluginWarning('Socket error: %s' % e)
181
            try:
182
                if config['smtp_login']:
183
                    mailServer.login(config['smtp_username'], config['smtp_password'])
184
                mailServer.sendmail(message['From'], config['to'], message.as_string())
185
            except IOError, e:
187
                raise PluginWarning('Unable to send email, IOError %s' % e.message, log)
189
            mailServer.quit()
191
register_plugin(OutputEmail, 'email')