|
24 | 24 |
|
25 | 25 | # region Imports |
26 | 26 | import tablib |
| 27 | +import smtplib |
| 28 | +import ssl |
| 29 | +from email.mime.text import MIMEText |
| 30 | +from email.mime.multipart import MIMEMultipart |
| 31 | +from email import encoders |
| 32 | +from email.mime.base import MIMEBase |
27 | 33 | from .io import FileManager |
28 | 34 | from .exception import ReportManagerError, ReportDataError |
29 | 35 |
|
@@ -245,7 +251,7 @@ def __str__(self): |
245 | 251 |
|
246 | 252 | :return: string |
247 | 253 | """ |
248 | | - return self._print_data() |
| 254 | + return str(self._print_data()) |
249 | 255 |
|
250 | 256 | def _print_data(self): |
251 | 257 | """Print data and count |
@@ -295,6 +301,64 @@ def export(self): |
295 | 301 | else: |
296 | 302 | raise ReportManagerError('the output object must be FileManager or NoneType object') |
297 | 303 |
|
| 304 | + def send(self, server, _from, to, cc=None, bcc=None, subject=None, body='', auth=None, _ssl=True, headers=None): |
| 305 | + """Send saved report to email |
| 306 | +
|
| 307 | + :param server: server SMTP |
| 308 | + :param _from: email address 'from:' |
| 309 | + :param to: email address 'to:' |
| 310 | + :param cc: email address 'cc:' |
| 311 | + :param bcc: email address 'bcc:' |
| 312 | + :param subject: email subject. Default is report title |
| 313 | + :param body: email body |
| 314 | + :param auth: authorization tuple "(user, password)" |
| 315 | + :param _ssl: boolean, if True port is 465 else 25 |
| 316 | + :param headers: more header value "(header_name, key, value)" |
| 317 | + :return: None |
| 318 | + """ |
| 319 | + if not self.output: |
| 320 | + raise ReportDataError('if you want send a mail with a report in attachment, must be specified output') |
| 321 | + |
| 322 | + # Prepare mail header |
| 323 | + message = MIMEMultipart("alternative") |
| 324 | + message["Subject"] = self.title if not subject else subject |
| 325 | + message["From"] = _from |
| 326 | + message["To"] = to |
| 327 | + if cc: |
| 328 | + message["Cc"] = cc |
| 329 | + if bcc: |
| 330 | + message["Bcc"] = bcc |
| 331 | + if headers: |
| 332 | + message.add_header(*headers) |
| 333 | + |
| 334 | + # Prepare body |
| 335 | + part = MIMEText(body, "html") |
| 336 | + message.attach(part) |
| 337 | + |
| 338 | + # Prepare attachment |
| 339 | + self.export() |
| 340 | + attach_file_name = self.output.file |
| 341 | + attach_file = open(attach_file_name, 'rb') |
| 342 | + payload = MIMEBase('application', 'octate-stream') |
| 343 | + payload.set_payload(attach_file.read()) |
| 344 | + encoders.encode_base64(payload) |
| 345 | + payload.add_header('Content-Disposition', 'attachment', filename=attach_file_name) |
| 346 | + message.attach(payload) |
| 347 | + |
| 348 | + # Prepare SMTP connection |
| 349 | + if _ssl: |
| 350 | + port = smtplib.SMTP_SSL_PORT |
| 351 | + protocol = smtplib.SMTP_SSL |
| 352 | + kwargs = {'context': ssl.create_default_context()} |
| 353 | + else: |
| 354 | + port = smtplib.SMTP_PORT |
| 355 | + protocol = smtplib.SMTP |
| 356 | + kwargs = {} |
| 357 | + with protocol(server, port, **kwargs) as srv: |
| 358 | + if auth: |
| 359 | + srv.login(*auth) |
| 360 | + srv.sendmail(_from, to, message.as_string()) |
| 361 | + |
298 | 362 |
|
299 | 363 | class ReportBook: |
300 | 364 |
|
@@ -395,4 +459,23 @@ def export(self, output=None): |
395 | 459 | for report in self: |
396 | 460 | report.export() |
397 | 461 |
|
| 462 | + def send(self, server, _from, to, cc=None, bcc=None, subject=None, body='', auth=None, _ssl=True, headers=None): |
| 463 | + """Send saved report to email |
| 464 | +
|
| 465 | + :param server: server SMTP |
| 466 | + :param _from: email address 'from:' |
| 467 | + :param to: email address 'to:' |
| 468 | + :param cc: email address 'cc:' |
| 469 | + :param bcc: email address 'bcc:' |
| 470 | + :param subject: email subject. Default is report title |
| 471 | + :param body: email body |
| 472 | + :param auth: authorization tuple "(user, password)" |
| 473 | + :param _ssl: boolean, if True port is 465 else 25 |
| 474 | + :param headers: more header value "(header_name, key, value)" |
| 475 | + :return: None |
| 476 | + """ |
| 477 | + for report in self: |
| 478 | + report.send(server, _from, to, cc=cc, bcc=bcc, subject=subject, body=body, auth=auth, |
| 479 | + _ssl=_ssl, headers=headers) |
| 480 | + |
398 | 481 | # endregion |
0 commit comments