[ckan-dev] Email Config: SMTPException - SOLVED - Mail on Registration?- SOLVED TOO

lucia.espona at wsl.ch lucia.espona at wsl.ch
Fri Oct 21 11:38:44 UTC 2016


Dear Pierre

I am not sure what you mean, the whole extension (tested only for CKAN 2.5.2) is available at: https://github.com/espona/ckanext-restricted

To run the code as it is, first you need to have the SMTP and mail settings in the CKAN config ini file (including 'mail_to', otherwise you can specify the address directly in the code), for example:
## Email settings

email_to = envidat at wsl.ch
error_email_from = envidat at wsl.ch
smtp.server = xxxx.wsl.ch
smtp.starttls = False
smtp.user = envidat at wsl.ch
smtp.password = xxxxx
smtp.mail_from = envidat at wsl.ch

Here it is the relevant code in plugin.py (I modified it for display but it should work anyway):

import ckan.plugins as plugins
from ckan.lib.mailer import mail_recipient, MailerException
from ckan.logic.action.create import user_create
from pylons import config

from logging import getLogger
log = getLogger(__name__)

def user_create_and_notify(context, data_dict):

    # Create a nice body message from the user information
    def body_from_user_dict(user_dict):
         body = '\n'
         for key,value in user_dict.items():
             body += ' \t - '+ str(key.upper()) + ': ' + str(value) + '\n'
         return body

    # Call the core default user creation procedure
    user_dict = user_create(context, data_dict)

    # Send your email using ckan.lib.mailer.mail_recipient 
    try:
        name = 'CKAN System Administrator'
        # Alternatively you can put here another email address
        email = config.get('email_to')
        subject = 'New Registration: ' +  user_dict.get('name', 'new user') + ' (' +  user_dict.get('email') + ')'
        body = 'A new user registered, please review the information: ' + body_from_user_dict(user_dict)
        log.debug('Mail sent to ' + email + ', subject: ' + subject)
        # Send the mail
        mail_recipient(name, email, subject, body)

    except MailerException as mailer_exception:
        log.error("Cannot send mail after registration ")
        log.error(mailer_exception)
        pass

    return (user_dict)


class YourPlugin(plugins.SingletonPlugin):
    plugins.implements(plugins.IActions)
    
    # IActions
    def get_actions(self):
        return { 'user_create': user_create_and_notify }
 

Please, let me know if you have further questions.

Best,
Lucia


_________________________________________________________
Dr. Lucia Espona Pernas

Swiss Federal Institute for Forest, Snow and Landscape Research WSL
Hauptgebäaude Labortrakt (HL C21)
Zürcherstrasse 111
8903 Birmensdorf
Switzerland

+41 44 739 28 71 phone direct
+41 44 739 21 11 reception

www.wsl.ch

-----"ckan-dev" <ckan-dev-bounces at lists.okfn.org> wrote: -----
To: CKAN Development Discussions <ckan-dev at lists.okfn.org>
From: Forum 
Sent by: "ckan-dev" 
Date: 19.10.2016 13:50
Subject: Re: [ckan-dev] Email Config: SMTPException - SOLVED - Mail on Registration?- SOLVED TOO

                   Dear Lucia,
     May I ask you to explain what you did, and how to setup your       changes?
     I am also looking to send a e-mail when a user register, but I       did not understand exactetly what do:
     https://github.com/espona/ckanext-restricted/blob/master/ckanext/restricted/plugin.py
     I can           not find ckanext-restricted           folder!
     Thank a lot
         Cheers
         Pierre
         
            
On 10/17/16 9:23 PM,       lucia.espona at wsl.ch wrote:
           Thanks           Adria
           
           It worked, in case it helps someone else this is the code: https://github.com/espona/ckanext-restricted/blob/master/ckanext/restricted/plugin.py
           
           Best,
           Lucia
           _________________________________________________________
           Dr. Lucia Espona Pernas
           
           Swiss Federal Institute for Forest, Snow and Landscape           Research WSL
           Hauptgebäaude Labortrakt (HL C21)
           Zürcherstrasse 111
           8903 Birmensdorf
           Switzerland
           
           +41 44 739 28 71 phone direct
           +41 44 739 21 11 reception
           
           www.wsl.ch
         
         -----"ckan-dev"           <ckan-dev-bounces at lists.okfn.org> wrote: -----         
           
To: CKAN Development Discussions             <ckan-dev at lists.okfn.org>
             From: Adrià Mercader 
               Sent by: "ckan-dev" 
                 Date: 17.10.2016 12:44
                 Subject: Re: [ckan-dev] Email Config: SMTPException -                 SOLVED - Mail on Registration?
                 
                 
Hi                     Lucía,
                     
                     There is nothing built in to send emails after user                     registration, but
                     you can override the user_create action, wrapping                     the core one and
                     sending the email afterwards. Something like this                     should get you
                     started:
                     
                     
                     
                     from ckan.lib.mailer import mail_recipient,                     MailerException
                     
                     class MyPlugin(...):
                     
                         implements(p.IActions)
                     
                         def get_actions(self):
                     
                             return {'user_create': my_user_create}
                     
                     
                     def my_user_create(context, data_dict):
                     
                     ÿÿ ÿuser_dict = core_user_create(context, data_dict)
                     
                     ÿÿ ÿ# Send your email, check ckan.lib.mailer for                     params
                     ÿÿ ÿtry:
                     ÿÿ ÿ ÿ ÿmail_recipient(name, email, subject, body)
                     ÿÿ ÿexcept MailerException:
                     ÿÿ ÿ ÿ ÿlog.error(...)
                     ÿÿ ÿ ÿ ÿpass
                     
                     
                     ÿÿ ÿreturn user_dict
                     
                     
                     Hope this helps,
                     
                     Adri…
                     
                     On 15 October 2016 at 21:55,                     ÿ<lucia.espona at wsl.ch> wrote:
                     > Dear all,
                     >
                     > Sorry for the mail, it turned out my SMTP                     server "really" did not support
                     > AUTH and I just needed to comment out the user                     and password in the config:
                     >
                     > ## Email settings
                     > email_to = envidat at wsl.ch
                     > error_email_from = envidat at wsl.ch
                     > smtp.server = xxxxx.wsl.ch
                     > smtp.starttls = False
                     > #smtp.user = envidat at wsl.ch
                     > #smtp.password = xxxxxxx
                     > smtp.mail_from = envidat at wsl.ch
                     >
                     > I have another question now, how can I get a                     mail sent to a particular
                     > address every time a new user registers?
                     >
                     > Thanks,
                     > Lucia
                     >
                     >                     _________________________________________________________
                     > Dr. Lucia Espona Pernas
                     >
                     > Swiss Federal Institute for Forest, Snow and                     Landscape Research WSL
                     > Hauptgeb„aude Labortrakt (HL C21)
                     > Zrcherstrasse 111
                     > 8903 Birmensdorf
                     > Switzerland
                     >
                     > +41 44 739 28 71 phone direct
                     > +41 44 739 21 11 reception
                     >
                     > www.wsl.ch
                     >
                     > -----"ckan-dev"                     <ckan-dev-bounces at lists.okfn.org> wrote: -----
                     > To: "CKAN Development Discussions"                     <ckan-dev at lists.okfn.org>
                     > From: lucia.espona at wsl.ch
                     > Sent by: "ckan-dev"
                     > Date: 15.10.2016 22:17
                     > Subject: [ckan-dev] Email Config:                     SMTPException(
                     >
                     > Dear all
                     >
                     > I am trying to set up the SMTP server from my                     institution so that CKAN
                     > Server can send mails.
                     > My configuration is following (@ replaced by '                     at '):
                     >
                     > ## Email settings
                     > email_to = envidat at wsl.ch
                     > error_email_from = envidat at wsl.ch
                     > smtp.server = xxxxx.wsl.ch
                     > smtp.starttls = False
                     > smtp.user = envidat at wsl.ch
                     > smtp.password = xxxxxxx
                     > smtp.mail_from = envidat at wsl.ch
                     >
                     >
                     > When I try to reset the password (user/reset),                     it fails to send a mail with
                     > the following message:
                     >
                     > Could not send reset link: SMTPException('SMTP                     AUTH extension not supported
                     > by server.',)
                     >
                     > Further messages in the console:
                     >
                     > 2016-10-15 22:14:13,808 DEBUG [ckan.logic]                     check access OK - request_reset
                     > user=
                     > 2016-10-15 22:14:13,812 DEBUG [ckan.logic]                     check access OK - user_show user=
                     > 2016-10-15 22:14:13,831 DEBUG                     [ckan.lib.activity_streams_session_extension]
                     > session had no _object_cache or no revision,                     skipping this commit
                     > Traceback (most recent call last):
                     > ÿ File
                     > "/usr/lib/ckan/default/src/ckan/ckan/lib/activity_streams_session_extension.py",
                     > line 52, in before_commit
                     > ÿ ÿ revision = session.revision
                     > AttributeError: 'Session' object has no                     attribute 'revision'
                     > 2016-10-15 22:14:13,842 ERROR [ckan.lib.mailer]                     SMTPException('SMTP AUTH
                     > extension not supported by server.',)
                     > Traceback (most recent call last):
                     > ÿ File                     "/usr/lib/ckan/default/src/ckan/ckan/lib/mailer.py",                     line 80, in
                     > _mail_recipient
                     > ÿ ÿ smtp_connection.login(smtp_user,                     smtp_password)
                     > ÿ File "/usr/lib/python2.7/smtplib.py", line                     585, in login
                     > ÿ ÿ raise SMTPException("SMTP AUTH extension                     not supported by server.")
                     > SMTPException: SMTP AUTH extension not                     supported by server.
                     > 2016-10-15 22:14:13,845 DEBUG [ckan.lib.base]                     rendering
                     >                     /usr/lib/ckan/default/src/ckan/ckan/templates/user/request_reset.html
                     > [jinja2]
                     >
                     >
                     > Is this a CKAN configuration issue or is the                     STMP server of my institution
                     > not compatible? Any hints will be appreciated.
                     >
                     > Thanks a lot and best regards,
                     > Lucia
                     >
                     >
                     >
                     >
                     >                     _________________________________________________________
                     > Dr. Lucia Espona Pernas
                     >
                     > Swiss Federal Institute for Forest, Snow and                     Landscape Research WSL
                     > Hauptgeb„aude Labortrakt (HL C21)
                     > Zrcherstrasse 111
                     > 8903 Birmensdorf
                     > Switzerland
                     >
                     > +41 44 739 28 71 phone direct
                     > +41 44 739 21 11 reception
                     >
                     > www.wsl.ch
                     > _______________________________________________
                     > ckan-dev mailing list
                     > ckan-dev at lists.okfn.org
                     > https://lists.okfn.org/mailman/listinfo/ckan-dev
                     > Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
                     >
                     > _______________________________________________
                     > ckan-dev mailing list
                     > ckan-dev at lists.okfn.org
                     > https://lists.okfn.org/mailman/listinfo/ckan-dev
                     > Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
                     >
                     _______________________________________________
                     ckan-dev mailing list
                     ckan-dev at lists.okfn.org
                     https://lists.okfn.org/mailman/listinfo/ckan-dev
                     Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
                                                         
              
       
_______________________________________________
ckan-dev mailing list
ckan-dev at lists.okfn.org
https://lists.okfn.org/mailman/listinfo/ckan-dev
Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
          
     
_______________________________________________
ckan-dev mailing list
ckan-dev at lists.okfn.org
https://lists.okfn.org/mailman/listinfo/ckan-dev
Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20161021/cdec1cbb/attachment-0003.html>


More information about the ckan-dev mailing list