In my previous post I described how to enable conditional post commit notifications in Subversion. In this post I’m going to show how to extend mailer.py from subversion tools in order to use gmail smtp server (smtp.gmail.com:587) for sending emails.
You should note one thing if you want to use gmail smtp server for emailing in your applications – it requires SSL enabled. So first you need to create gmail account – its credentials will be used to connect to smtp.gmail.com. Once gmail account created you need to add new setting “smtp_use_ssl” in mailer.conf file:
1: [general]
2:
3: smtp_hostname = smtp.gmail.com:587
4: smtp_username = your_gmail_login
5: smtp_password = your_gmail_password
6:
7: # 2009-12-13 asadomov: add custom smtp_use_ssl parameter
8: smtp_use_ssl = true
9: ...
1: class SMTPOutput(MailedOutput):
2: "Deliver a mail message to an MTA using SMTP."
3:
4: def start(self, group, params):
5: MailedOutput.start(self, group, params)
6:
7: self.buffer = StringIO()
8: self.write = self.buffer.write
9:
10: self.write(self.mail_headers(group, params))
11:
12: def finish(self):
13: server = smtplib.SMTP(self.cfg.general.smtp_hostname)
14:
15: # 2009-12-13 asadomov: add ssl configuration (e.g. for gmail smtp server)
16: if self.cfg.is_set('general.smtp_use_ssl') and self.cfg.general.smtp_use_ssl.lower() == "true":
17: server.ehlo()
18: server.starttls()
19: server.ehlo()
20:
21: if self.cfg.is_set('general.smtp_username'):
22: server.login(self.cfg.general.smtp_username,
23: self.cfg.general.smtp_password)
24: server.sendmail(self.from_addr, self.to_addrs, self.buffer.getvalue())
25: server.quit()
After that you will be able to use gmail smtp server to send post commit notifications via mailer.py.