Saturday, April 07, 2012

Ant script for zipping report and sending mail

Below is the ant tasks showing how to zip reports generated after test run and sending the zip through mail.
//Build.xml
  1. <target name="sendMail" depends="junitreport,addZip">
  2.     <mail mailhost="smtp.gmail.com" mailport="465" user="my_user_name@gmail.com"
  3.         password="my_password" ssl="on" messagemimetype="text/html"
  4.         charset="ISO-8859-1" subject="Mail subject"
  5.         tolist="myfriend1@gmail.com,myfriend2@gmail.com">
  6.         <from address="my_user_name@gmail.com"/>
  7.         <message>
  8.             Please view index.html page for report in attached zip.
  9.         </message>
  10.         <fileset dir="${zip}"/>
  11.     </mail>
  12.     <echo> Mail is sent successfully </echo>
  13. </target>  
  14. <target name="addZip">
  15.     <zip destfile="${zip}/testReport.zip" duplicate="preserve">
  16.         <zipfileset dir="${reports.dir}" />
  17.     </zip>
  18.     <zip destfile="${zip}/logs.zip" duplicate="preserve">
  19.         <zipfileset dir="${log.dir}" />
  20.     </zip>
  21. </target>
In the above build file task 'addZip' ,it zips the all the reports that are in the ${reports.dir} and stores the content in a new zip file namely 'testReport.zip' inside ${zip} directory and also it zips all the logs generated in the ${log.dir} directory and stores in a new zip file namely logs.zip.

Then the 'sendMail' task sends these zip files as email attachment.
In the 'sendMail' target use your own valid credentials for login to mail server. Here i have used 
user id: my_user_name@gmail.com
password: my_password

Note:
 This task is specially for mail sending through gmail. You can change the attribute values for different mail servers as your requirement.