Utilisateur:Orlodrim/Wikimag
Apparence
#!/usr/bin/python3
import argparse
import os
import re
import shutil
import smtplib
import subprocess
import sys
import tempfile
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import email.charset
import email.utils
class WikimagMailer:
# ...
def run(self, mode, force):
text_part, html_part = self._generate_email_content(force)
if self.title:
subject = self.title
else:
match = re.match(r'Wikimag ([0-9]+) du [^()]* \(semaine ([0-9]+)\)', text_part)
assert match
subject = '#%s, semaine %s' % (match.group(1), match.group(2))
if subject == self.last_issue and not force:
print('Issue "%s" with already distributed by e-mail' % subject)
return
self.last_issue = subject
email.charset.add_charset('utf-8', email.charset.QP, email.charset.QP)
msg_plain = MIMEText(text_part, 'plain')
# msg_plain.add_header('Content-Disposition', 'inline')
msg_html = MIMEText(html_part, 'html')
# msg_plain.add_header('Content-Disposition', 'inline')
with open('wikimag.png', 'rb') as f:
msg_logo = MIMEImage(f.read())
msg_logo.add_header('Content-ID', '<logo>')
# msg_logo.add_header('Content-Disposition', 'inline')
msg_html_and_logo = MIMEMultipart('related')
msg_html_and_logo.attach(msg_html)
msg_html_and_logo.attach(msg_logo)
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['Date'] = email.utils.formatdate(localtime=True)
msg['From'] = self.email_config['from']
msg['To'] = self.email_config['to_real' if mode == 'real' else 'to_test']
msg.attach(msg_plain)
msg.attach(msg_html_and_logo)
if mode == 'dryrun':
print(msg.as_string())
elif mode in ['mailtome', 'real']:
if mode == 'real':
self._save_state()
print('Connecting to ssl0.ovh.net')
server = smtplib.SMTP_SSL('ssl0.ovh.net', 465)
server.ehlo()
print('Sending user name and password')
server.login(msg['From'], self.email_config['from-pass'])
print('Sending e-mail to %s' % msg['To'])
server.sendmail(msg['From'], [msg['To']], msg.as_string())
print('Done')
server.quit()