Tuesday, June 26, 2012

Send an e-mail from Web Content Manager


Refer this link in case of your smtp server is running on default port 25 and authentication doesn't requre connection over ssl - http://www-01.ibm.com/support/docview.wss?uid=swg21314850

Since, configuration of Web Content Manager (WCMConfigService.properties) was not working due to  several issues including unavailable config attributes to set for extra parameter like starttls and custom smtp port.
So, I have used websphere inbuilt mail provider feature. I wrote servlet to lookup mail provider jndi and processed e-mail setup information within the servlet.

1)    Mail Provider Configuration

1)      Login to Websphere application server console
2)      Goto Resources > Mail > Mail Providers
3)      Click on 'built-in mail provider'
4)      Click on 'Mail sessions'
5)      Click on 'New' button
6)      Enter following details and save apply the changes, in this case I have configured my gmail account to receive user's feedback
                Name : feedback
                JNDI : mail/feedback
                Outgoing Mail properties
                                Server : smtp.gmail.com
                                Protocol : smtp
                                User:  <smtp_user>@gmail.com
                                Password: <password>
                                Verify Password : <password>
                                Return e-mail address : <smtp_user>@gmail.com
7)      Once saved, Click on 'feedback' entry and add following Custom property entries
                mail.smtp.port  : 587
                mail.smtp.starttls.enable  : true
                mail.smtp.auth : true (Note - this is only required in case of smtp requires authentication otherwise ignore this entry)
8)      Add smtp ssl certificate to application server trust store (required only if smtp is secured)
a)      Login to WebSphere Application server console.
b)      Go to Security > SSL certificate and key management.
c)       Click on ' Key stores and certificates'> 'NodeDefaultTrustStore' > ' Signer certificates'.
d)      Click on "retrieve from port".
e)      Provide following details and click on 'Retrieve signer information' button.
Host : smtp.gmail.com
Port : 465
Alias : smtp.gmail.com
f)       Click on 'Ok' and Save.

Note - If  smtp requires authentication then we  also need to provide an entry for smtp password in servlet 'SendMail'  init configuration parameter named 'smtp.password'.

2)    SendMail servlet to read mail provider settings

This servlet reads mail contents from request parameters along with on which portal page servlet should redirect after the processing the email. There are two additional parameters urile_success, urile_failure which servlet uses to show mail send or failure acknowledgement back to the user.
Parameter s 'urile_success' and 'urile_failure' contain path of wcm contents.
This servlet looks up for mail provider jndi 'mail/feedback' which was created in the first step.
If the smtp requires credentials to send  mail, it reads init parameter name ' smtp.password' from the deployment descriptor (web.xml)  for this  servlet.
This servlet is mapped with url-pattern /SendMail, which is resided within the theme project context root '/mytechtheme '.
So to access this servlet the URL will be : http://portal.mytech.com:10039/mytechtheme/SendMail

3)    Create an HTML form (wcm html component) to be used as an e-mail form

1)      Reference to the sendmail servlet is specified in the form's post action. This html form includes a hidden fields which helps servlet in redirection to a portal page. After the e-mail  is successfully submitted or if it fails, the form will be  redirected to a portal page.
Important Hidden Parameters are  :
                To : Receiver of the feedback email
                Redirect : Portal page friendly url
                urile_success :  WCM content path to show after the successfully delivering mail
                urile_failure : WCM content path to show in case mail delivery fails

<form name="myform" action="/mytechtheme/SendMail" method="post">
<table width="100%" cellspacing="0" cellpadding="2" border="0">
              <tbody><tr>
                <td width="50%"><h1>Contact Details</h1></td>
              </tr>
               <tr>
                <td style="height:5px"></td>
               </tr>
               <tr>
                 <td>Company Name <b class="redtxt">*</b></td>
               </tr>
               <tr>
                 <td><label>
                   <input type="text" class="input width200px" id="companyname" name="companyname">
                   </label>
                 </td>
               </tr>
               <tr>
                 <td>Email Address <b class="redtxt">*</b></td>
               </tr>
               <tr>
                 <td><input type="text" class="input width200px" id="fromemail" name="from"></td>
               </tr>
               <tr>
                 <td>Mobile Number</td>
               </tr>
               <tr>
                 <td><input type="text" class="input width200px" id="mobile" name="mobile"></td>
               </tr>
               <tr>
                 <td>Comments</td>
               </tr>
               <tr>
                 <td><label>
                   <textarea class="input width200px" rows="3" cols="20" id="contents" name="contents"></textarea>
                 </label></td>
               </tr>
               <tr>
                 <td class="note">All <span class="redtxt">*</span> fields are mandatory </td>
               </tr>
               <tr>
                 <td align="center"><label>
                   <!-- input type="image" src="media/images/btn.gif" name="button" id="button" value="Submit" onclick="submitForm()" / -->
                   <input type="submit" class="brownbtn" value="submit">
                 </label></td>
               </tr>
               <tr>
                <td>&nbsp;</td>
               </tr>
            </tbody></table>
<input type="hidden" name="to" value="shashi.rj@gmail.com"/>
<input type="hidden" name="subject" value="Contact Details"/>
<input type="hidden" name="redirect" value="[Component name="mytechtechnical/urlcomposer"]/mytech/contactus"/>
<input type="hidden" name="urile_success" value="wcm:path:/mytechContent/Internet/Contact Us/Contact Details Success"/>
<input type="hidden" name="urile_failure" value="wcm:path:/mytechContent/Internet/Contact Us/Contact Details Failure"/>
</form>
2)      Create AT name it 'AT_ContactDetails' and add component reference name it 'Com_Ref' to hold above create HTML Form 'Contact Form'
3)      Create a PT name it 'PT_ContactDetails' with following markup
[Element context="current" type="content" key="Com_Ref"]
4)      Now that the pieces are in place for the e-mail form, all we have to do is create the content and site area to hold them. First, create a new site area in your mytechContent library. Name it "Contact Us", and map the presentation and authoring template to the site area that you created.

Create Content named 'Contact Details' using AT 'AT_ContactDetails'. Select 'Contact Form' for com_ref element. There after save and publish this content.

5)      Configure the WCM Content Preview portlet to access the wcm content (Contact Details)
6)      Send e-mail and verify it works as expected.

 Servlet mapping in theme web.xml - 

<servlet>
        <description>
        </description>
        <display-name>SendMail</display-name>
        <servlet-name>SendMail</servlet-name>
        <servlet-class>com.mytech.mail.SendMail</servlet-class>
        <init-param>
            <description>
            </description>
            <param-name>smtp.password</param-name>
            <param-value></param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>SendMail</servlet-name>
        <url-pattern>/SendMail</url-pattern>
    </servlet-mapping>

Servlet Source Code - https://www.box.com/s/dda990511fd603df1cf3

No comments:

Post a Comment