Sending Email in OpenERP 5

Hello All

I was assigned the task of Setting up and Sending email from OpenERP 5.0.10 (The version is almost Obsolete Now but we are still using it). I checked, the function through which email is send is in “tools” package and the function email_send.

First you have to setup the SMTP server’s address, the port, User Name and Password settings. There are few ways you can do this.

I am setting up for gmail. You can replace it with your own settings

First you can simply pass them as parameter to OpenERP as show below

./ –addons-path=<path/to/addons> – – –smtp-port=587 –smtp-user=YourGmailUserName –smtp-password=********* –smtp-ssl=True

OR You can also do this by making the following settings in ~/.openerp_serverrc

Change the following entries

email_from =

smtp_port = 587

smtp_server =

smtp_user = YourGmailUserName

smtp_password = ***********

Or You can Make hard Coded changes in ERPHOME/tools/ file in class configmanager. Find all the entries as I shown above and change them accordingly.

Next to use the function, import config from tools as show below

from tools import config

When, I would use the function, It wouldn’t work, It will return errors like connection refused or some Auth Errors, though I checked all my parameters they were correct. I checked the code of email_send function, It was very strange, they used a class called writetolog in a function and all of the logic of sending email was in that class. It didn’t made any sense to me (and neither was it working or may be I was missing something, Correct me If I am wrong). So I made changes to the function to make it work.  the Source code of the function after I made changes is given below.

def email_send(email_to, subject, body, email_cc=None, email_bcc=None, reply_to=False,
               attach=None, tinycrm=False, ssl=False, debug=False, subtype='plain', x_headers=None):

    """Send an email."""
    import smtplib
    from email.MIMEText import MIMEText
    from email.MIMEBase import MIMEBase
    from email.MIMEMultipart import MIMEMultipart
    from email.Header import Header
    from email.Utils import formatdate, COMMASPACE
    from email.Utils import formatdate, COMMASPACE
    from email import Encoders
    import netsvc

    email_from = ''    

    if x_headers is None:
        x_headers = {}

    if not ssl:
        ssl = config.get('smtp_ssl', False)

    if not email_from and not config['email_from']:
        raise Exception("No Email sender by default, see config file")

    if not email_cc:
        email_cc = []
    if not email_bcc:
        email_bcc = []

    if not attach:
            msg = MIMEText(body.encode('utf8') or '',_subtype=subtype,_charset='utf-8')
            msg = MIMEText(body or '',_subtype=subtype,_charset='utf-8')
        msg = MIMEMultipart()

    msg['Subject'] = Header(ustr(subject), 'utf-8')
    msg['From'] = email_from
    del msg['Reply-To']
    if reply_to:
        msg['Reply-To'] = reply_to
        msg['Reply-To'] = msg['From']
    msg['To'] = COMMASPACE.join(email_to)
    if email_cc:
        msg['Cc'] = COMMASPACE.join(email_cc)
    if email_bcc:
        msg['Bcc'] = COMMASPACE.join(email_bcc)
    msg['Date'] = formatdate(localtime=True)

    # Add OpenERP Server information
    msg['X-Generated-By'] = 'Campus Management System - SERG - IMSciences'
    msg['X-OpenERP-Server-Host'] = socket.gethostname()
    msg['X-OpenERP-Server-Version'] = release.version

    # Add dynamic X Header

    for key, value in x_headers.items():
        msg['X-OpenERP-%s' % key] = str(value)

    if tinycrm:
        msg['Message-Id'] = "<%s-tinycrm-%s@%s>" % (time.time(), tinycrm, socket.gethostname())

    if attach:
            msg.attach(MIMEText(body.encode('utf8') or '',_subtype=subtype,_charset='utf-8'))

            msg.attach(MIMEText(body or '', _charset='utf-8', _subtype=subtype) )

        for (fname,fcontent) in attach:
            part = MIMEBase('application', "octet-stream")
            part.set_payload( fcontent )
            part.add_header('Content-Disposition', 'attachment; filename="%s"' % (fname,))

    s = smtplib.SMTP('')

    s.login(config['smtp_user'], config['smtp_password'])
    s.sendmail(email_from, flatten([email_to, email_cc, email_bcc]), msg.as_string())

    return True

Simply copy the code above and replace it with the code in ERPHOME/tools/

I made slight adjustment to the signature of the function by removing ‘email_from‘ so that the from email is always fixed.

Now to Use the fucntion just call the fucntion as show Below

tools.email_send(‘’, ‘Test Subject’, ‘just a Test Email’)

There are still few problems which I will correct as soon as possible.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s