cancel
Showing results for 
Search instead for 
Did you mean: 

sending an email via website

idiotsguide
Champ in-the-making
Champ in-the-making
Hi everyone,

I have built a website around my alfresco system (4.0), using the quickstart as my base, but I'm working on a contact us form and having a problem. I have created a simple form in my contactus.get.html.ftl file, and i've created a contactus.post.js file. In here I want to email myself to notify me that someone has filled out the form. I've been looking around all the documentation, etc and came across this….

var mail = actions.create("mail");
mail.parameters.to = "me@me.com";
mail.parameters.subject = "Someone has used the contact us form";
mail.parameters.from = "website@thewebsite.com";
mail.parameters.text = "All good";
mail.execute();

Which I have been trying to get working but this is what I get when the script runs..
Failed to execute script 'org.springframework.extensions.webscripts.ResourceStore$WebApplicationStoreScriptContent@2080bcd8': 04200010 ReferenceError: "actions" is not defined.

Am I completely on the wrong track here? (Just when I thought I was getting my head around Alfresco!).
I'd be most grateful if anyone could help.

Thanks.
17 REPLIES 17

idiotsguide
Champ in-the-making
Champ in-the-making
Hi amandaluniz
Thanks for the reply.

Yes, i did start to wonder if I needed to do something like that.
I can write a webscript in alfresco but how would I call that webscript from the wcmqs site? (sorry, my knowledge is increasing daily but there are still many mysteries to alfresco for me).
Thanks again for you help.

amandaluniz_z
Champ on-the-rise
Champ on-the-rise
you can use the remote connector you were talking about in previous posts

amandaluniz_z
Champ on-the-rise
Champ on-the-rise
Something like:


var connector = remote.connect("alfresco");
var data = connector.get("/send/email?to=….");

An easy way, I guess you could also do a post…

Adei

idiotsguide
Champ in-the-making
Champ in-the-making
Hi Adei

All becomes clear!!!
Thank you very much, think I would have gone slightly mad if it wasn't for your posts.
I'm well on my way now, so thanks again.

amandaluniz_z
Champ on-the-rise
Champ on-the-rise
Remember you'll need to create the alfresco webscript that matches the /send/email/… url

michaelc
Champ on-the-rise
Champ on-the-rise
Ok so in Repository> Data Dictionary> Web Scripts> org> alfresco
I created a folder test.
in the folder I created mail.get.desc.xml
 
<webscript>
  <shortname>mail</shortname>
  <description>Test mail program to send an email</description>
  <url>/test/mail</url>
  <format default="html">argument</format>
  <authentication>guest</authentication>
  <transaction>required</transaction>
</webscript>

JS  mail.get.js
var mail = actions.create("mail");
mail.parameters.to = "michael.c.ford@email.com";
mail.parameters.subject = "Someone has used the contact us form";
mail.parameters.from = "michael.c.ford@email.com";
mail.parameters.text = "All good";
mail.execute();

Find the service in the service catalog ( yaaaaaa )
try to run it /alfresco/service/test/mail

and it fails 500 can't find execute on line 6.
org.mozilla.javascript.EvaluatorException - Can't find method org.alfresco.repo.jscript.ScriptAction.execute(). (workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts/org/alfresco/test/mail.get.js#6)

seems pretty simple what did I miss ?

kaynezhang
World-Class Innovator
World-Class Innovator
According to definition of ScriptAction's execute(ScriptNode node) method, you should provide a ScriptNode parameter to it.

michaelc
Champ on-the-rise
Champ on-the-rise
Ok what I learned.
wcmqs does not have a connector alfresco, there is one alfresco-webscripts but if you changed ports it still needs to be modified. ( in surf.xml )
 
    <config evaluator="string-compare" condition="Remote">
        <remote>
            <endpoint>
                <id>alfresco-webscripts</id>
                <name>Alfresco Webscripts</name>
                <connector-id>http</connector-id>
                <endpoint-url>http://localhost:8080/alfresco/service</endpoint-url>
                <identity>declared</identity>
                <username>admin</username>
                <password>******</password>
            </endpoint>
            <endpoint>
                <id>alfresco</id>
                <name>Alfresco Webscripts</name>
                <connector-id>http</connector-id>
                <endpoint-url>http://localhost/alfresco/service</endpoint-url>
                <identity>declared</identity>
                <username>admin</username>
                <password>****</password>
            </endpoint>
        </remote>
    </config>


need to escape to pass the parameters ( should be a better way )
write.post.js

      // webSite.ugcService.postFeedback(assetId, name, email, website, type, subject, comment, 0);
      var conn = remote.connect('alfresco');
      var passString = '/test/mail?name=';
      passString = passString + escape(name);
      passString = passString + '&email=' + escape(email);
      passString = passString + '&subject=' + escape(subject);
      passString = passString + '&comment=' + escape(comment);
      var result = conn.call(passString);


To pass items to the template it needs to be an array in an array.
 

     var node = people.getGroup("GROUP_Contact"); 
     if(node){
        var members = people.getMembers(node);        //Get everyone who should know 

      for each (usr in members){
         if (usr == null &&  usr.properties["cm:email"] != null)
           {
             model.result = "No user or email address – mail not sent";
           }
        else
          {

            var mail = actions.create("mail");
            var templateArgs = new Array();
                    templateArgs['username'] = usr.properties["cm:firstName"]  + " "  +  usr.properties["cm:lastName"] ;
                    templateArgs['name'] = args.name;
                    templateArgs['subject'] = args.subject;            
                    templateArgs['comment'] = args.comment;
                    templateArgs['email'] = args.email;        

            var templateModel = new Array();
                   templateModel['args'] = templateArgs;

            mail.parameters.template_model = templateModel ;
            mail.parameters.template = companyhome.childByNamePath("Data Dictionary/Web Scripts/org/alfresco/test/userdetailsmail.ftl"); 

            mail.parameters.to = usr.properties["cm:email"];
            mail.parameters.subject = args.subject;
            mail.parameters.from = 'system@kp.org';           
            mail.execute(usr);   
           model.result = "Email sent to user " + usr.properties["cm:firstName"] + " " + usr.properties["cm:lastName"];         
         }
      }
     }else{
        model.result = "group not found";
     }


Hope this helps the next one to find this note.