Wednesday, December 16, 2009

Use gmail smtp server for post commit notifications in Subversion via mailer.py

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: ...
Next you need to modify SMTPOutput class in mailer.py in order to use added “smtp_use_ssl” setting:
   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.

7 comments:

  1. how can i use another smtp server as ssmtp with mailer.py?
    or how can i configure sendmail to send mails via gmail?

    ReplyDelete
  2. sunevil hi,
    for your 1st question: as I showed in the post, in order to specify what smtp server should be used by mailer.py, you need to set the following settings:

    smtp_hostname = server_name:port
    smtp_username = login
    smtp_password = password

    in the mailer.conf file.
    For 2nd question: this post exactly shows how to configure mailer.py to send mails via gmail.

    ReplyDelete
  3. Hi Alex,
    Short: Is there any option to insert %(author)s resolution by openLDAP to mailer.conf file?
    Detailed: I running subversion 1.6.11 which authenticates users by OpenLDAP. I want to send email notifications to each user upon he/she makes commit.

    ReplyDelete
  4. Hi rugb,
    I didn't use LDAP for svn authentication, so can't help much here. If you need to send email notification to the author of the commit, I think you need to perform 2 actions:
    1. catch commit event
    2. determine email of the author
    Is it so that you have problems with 2nd step?

    ReplyDelete
  5. Hi Alex mailer.py how to add repository name

    eg:

    Repositoryname: Demo
    Author: Jhon
    Version: 143

    ReplyDelete
  6. running mailer.py file
    error : You need version 1.8.1 or better of the Subversion Python bindings.

    ReplyDelete