Forum Discussion

jimpson's avatar
jimpson
Contributor
13 years ago

HTML Email from Test Complete (w/Image support)

I just wanted to share this function in hopes that others might be able to use it:




function TestFailNotify(subject,body,intHeader,sndPic)

{

    // Subject

    subject = (subject == undefined) ? 'Testing Failure: ' + Sys.HostName : subject;

    

    // Body

    body = (body == undefined) ? GetCallStack() + '<br>Testing Failure' : body;

    body = Sys.HostName + "  " + aqDateTime.Now() + "<br>==============================================<br><br>" + body;

    

    // Should a picture be included

    sndPic = (sndPic == undefined) ? true : sndPic;

    

    if(sndPic)

    {   

        // Make sure the file path exists and set a file name

        var path = Project.Variables.PicPath;

        var src=Sys.HostName + "_" + aqConvert.DateTimeToFormatStr(aqDateTime.Now(), '%Y-%m-%d_%H%M%S') + '.jpg';

        path += src;



        // Save a picture of the desktop

        Sys.Desktop.Picture().SaveToFile(path);

        }

    

    intHeader = (intHeader == undefined) ? 0 : intHeader;

    

    var txtCol;

    var hdCol;

    

    // Set the email header's color scheme

    switch(intHeader)

    {

        case 0:

            // Message Color

            hdCol = '#4DB84D';

            txtCol = '#FFFFFF';

            break;

        case 1:

            // Warning Color

            hdCol = '#FFFF66';

            txtCol = '#000000';

            break;

        case 2:

            // Error Color

            hdCol = '#FF3333';

            txtCol = '#FFFFFF';

            break;

        default:

            // Default Color

            hdCol = '#1F2F3F';

            txtCol = '#FFFFFF';

            }

        

    var sender = 'QA-' + Project.Variables.System + '@yourCompany.com';

    var iMsg,strHTML, objCDO, mConfig, schema, objN;

    

    try

    {

        schema = 'http://schemas.microsoft.com/cdo/configuration/';

        mConfig = Sys.OleObject('CDO.Configuration');

        mConfig.Fields.Item(schema + 'sendusing') = 2; // cdoSendUsingPort

        mConfig.Fields.Item(schema + 'smtpserver') = 'mail.yourCompany.com'; // SMTP server

        mConfig.Fields.Item(schema + 'smtpserverport') = 25; // Port number

        mConfig.Fields.Item(schema + 'smtpauthenticate') = 0; // Authentication mechanism



        mConfig.Fields.Update();

    

        iMsg = Sys.OleObject('CDO.Message');

        iMsg.Configuration = mConfig;

        iMsg.MimeFormatted = true;

        iMsg.From = sender;

        iMsg.To = Project.Variables.EmailGroup;

        iMsg.Subject = subject;

        strHTML = '';

        strHTML += '<!DOCTYPE html PUBLIC "-//W3C//DTD//XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">';

        strHTML += '<html>';

        strHTML += '<style>';

        strHTML += 'a {';

        strHTML += '    color:white;';

        strHTML += '    text-decoration:none;';

        strHTML += '    }';

        strHTML += 'p {';

        strHTML += '    color:white;';

        strHTML += '    }';

        strHTML += 'hr {';

        strHTML += '    color:#1f2f3f;';

        strHTML += '    }';

        strHTML += 'img {';

        strHTML += '    height:50%;';

        strHTML += '    width:50%';

        strHTML += '    }';

        strHTML += 'body {';

        strHTML += '    background-color:grey;';

        strHTML += '    text-align:center;';

        strHTML += '    }';

        strHTML += '#topDiv {';

        strHTML += '    font-size:2em;';

        strHTML += '    background-color:' + hdCol + ';';

        strHTML += '    color:' + txtCol + ';';

        strHTML += '    margin-left:5%;';

        strHTML += '    margin-right:5%;';

        strHTML += '    text-align:center;';

        strHTML += '    padding-top:2%;';

        strHTML += '    padding-bottom:1%;';

        strHTML += '    }';

        strHTML += '#container {';

        strHTML += '    color:white;';

        strHTML += '    position:relative;';

        strHTML += '    text-align:center;';

        strHTML += '    background-color:#1f2f3f;';

        strHTML += '    border-style:outset;';

        strHTML += '    }';

        strHTML += '#content {';

        strHTML += '    position:absolute;';

        strHTML += '    }';

        strHTML += '#space {';

        strHTML += '    height:20px;';

        strHTML += '    }';

        strHTML += '</style>';

        strHTML += '<body>';

        strHTML += '<div id="container">';

        strHTML += '<div id="content"><div id="topDiv">'+ subject;

        strHTML += '</div><hr><hr><hr>';

        

        if(sndPic)

        {

            strHTML += '<a><img src="cid:' + src + '" alt="IMG"></a><hr>';

            }

            

        strHTML += '<p style="color:#FFFFCC;">**********TEST DATA**********</p>';

        strHTML += '<p>' + body + '</p><hr><hr><hr>';

        strHTML += '</div>';

        strHTML += '</div>';

        strHTML += '</body>';

        strHTML += '</html>';



        // Full html body of outbound email

        iMsg.HTMLBody = strHTML;

        

        if(sndPic)

        {

            objN = iMsg.AddRelatedBodyPart(path, "Fail.jpg", 0);

            objN.Fields.Item("urn:schemas:mailheader:Content-ID") = '<' + src + '>';

            objN.Fields.Update();

            }

    

        iMsg.Send();    

        }

        catch (ex)

    {

        Log.Warning(subject, body);

        Log.Error('E-mail cannot be sent', ex.description);

        return false;

    }

    return true;

    }



If you have any questions about setup or why it's configured the way it is, please drop me a note.