4
from email.message import Message
5
from flexget.plugin import PluginError, PluginWarning, register_plugin
7
log = logging.getLogger('email')
10
class OutputEmail(object):
13
Send an e-mail with the list of all succeeded (downloaded) entries.
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 ?
33
smtp_host: smtp.host.com
35
Config example with smtp login:
40
smtp_host: smtp.host.com
43
smtp_username: my_smtp_login
44
smtp_password: my_smtp_password
47
Config multi-feed example:
53
smtp_host: smtp.host.com
70
smtp_host: smtp.gmail.com
73
smtp_username: gmailUser
74
smtp_password: gmailPassword
77
Default values for the config elements:
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')
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']]
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']:
130
# don't send mail when learning
131
if feed.manager.options.learn:
134
# don't send empty emails
135
if not feed.accepted:
138
# generate email content
139
entries_count = len(feed.accepted)
140
subject = '[FlexGet] %s : %d new entries downloaded' % (feed.name, entries_count)
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'])
149
content += " => %s (%s)" % (entry_path, entry_filename)
153
# prepare email 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())
166
if config['smtp_ssl']:
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')
170
# Create a SSL connection to smtp server
171
mailServer = smtplib.SMTP_SSL(config['smtp_host'], config['smtp_port'])
173
mailServer = smtplib.SMTP(config['smtp_host'], config['smtp_port'])
174
if config['smtp_tls']:
176
mailServer.starttls()
177
except socket.error, e:
178
raise PluginWarning('Socket error: %s' % e)
182
if config['smtp_login']:
183
mailServer.login(config['smtp_username'], config['smtp_password'])
184
mailServer.sendmail(message['From'], config['to'], message.as_string())
187
raise PluginWarning('Unable to send email, IOError %s' % e.message, log)
191
register_plugin(OutputEmail, 'email')