1

Using a PowerShell script I invoke a web request and save the response in a variable

$SOAP_RESP=(Invoke-WebRequest $WEB_SRV_URL -Method Post -ContentType "text/xml" -InFile $SOAP_REQ_FILE_PATH).Content 

I have email body HTML formatted and I append the earlier obtained SOAP response to it.

$MESSAGE_BODDY="$($HTML_FORMATTED_MESSAGE)<br><br>$($SOAP_RESP)"

Next I want to send an email containing email body from the previous step -BodyAsHtml

Send-MailMessage -From $SMTP_FROM -To $SMTP_TO -Subject $MESSAGE_SUBJECT -Body $MESSAGE_BODDY -BodyAsHtml -Priority high 

Obviously, it formats the SOAP response and the email which is delivered is a mess.

Does anyone know how can I deal with this situation? I need SOAP request to be in the email.

[EDIT] The soap response is

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xmm="http://namespace.mynamespace/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SOAP-ENV:Body>
    <m:service_name xmlns:m="urn:com-s-s-rpc:SYSTEM">
      <_INPUT_PARAM>
        <UNIT_ID>0dbghfgd</UNIT_ID>
        <MSG>test</MSG>
      </_INPUT_PARAM>
      <_OUTPUT_PARAM>
        <RETURN_ERROR_MSG>Invalid Format</RETURN_ERROR_MSG>
      </_OUTPUT_PARAM>
    </m:service_name>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

What I get in email is only below string (not entire response)

<_INPUT_PARAM>0dbghfgdtest<_OUTPUT_PARAM>Invalid Format
Alex
  • 1,054
  • 6
  • 25
  • 42
  • "*Obviously*" - not obviously. You want the SOAP response in the email, you put the SOAP response in the email, it worked, you're unhappy? So, what exactly is wrong and what do you want instead? – TessellatingHeckler Jun 05 '17 at 23:59
  • -BodyAsHtml flag in Send-MailMessage command, causes SOAP response to be html formatted, so it is not properly displayed in the email body – Alex Jun 06 '17 at 00:06
  • 2
    Embedd it in `
    $($SOAP_RESP)
    ` tags, or 'code' tags, or both - https://stackoverflow.com/a/4611735/478656 ?
    – TessellatingHeckler Jun 06 '17 at 01:01
  • @TessellatingHeckler Doesn't work. I tried to add wrap the variable even with
    $($SOAP_RESP)
    and still not working
    – Alex Jun 06 '17 at 15:45

1 Answers1

2

Per the comment from TessellatingHeckler, the <pre> and <code> HTML tags both force the contents of those tags to be presented raw and any HTML tags within are not interpreted. Pre stands for Preformatted Text. Code means "This is computer code".

You can use both together for a belt and braces approach:

$MESSAGE_BODDY="$($HTML_FORMATTED_MESSAGE)<br><br><pre><code>$($SOAP_RESP)</pre></code>"
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
  • Hmmm... Doesn't work. I tried to add wrap the variable even with
    $($SOAP_RESP)
    and still not working
    – Alex Jun 06 '17 at 15:44
  • It seems that email clients perhaps don't support the
     or  tags. Does your soap response contain HTML like tags that are being misinterpreted as HTML, or is just messily formatted due to not being a fixed width font? Adding an example of the contents of $SOAP_RESP to your question would really help here.
    – Mark Wragg Jun 06 '17 at 17:17