Search code examples
stringemailsmtpmulepayload

Mule - Personalize Body SMTP Component


I have a mule flow that performs the following actions:

HTTP => JSON to XML => XSLT => Logger => SMTP

I want to personalize the body to the Email with the message.payload and my personal text for example:

It has received an XML message with the following information:

#[message.payloadAs(java.lang.String)]

The message.payload is the XML transformed with the XSLT component and the email will be received as follows:

It has received an XML message with the following information:

author:Gambardella, Matthew

What I need to personalize the message with my text?

Should I use a payload component or a property?


Solution

  • the SMTP connector it will use the payload as the body of the mail, so just set in the payload what you want to be sent for example:

    <set-payload value="It has received an XML message with the following information: #[message.payloadAs(java.lang.String)]" doc:name="Set Payload"/>

    Now if you want to send this email has HTML mail so that in your payload you can add html tag and even styles you can do that by adding to the SMTP a global connector and setting on the global connector the contentType, note that setting the mimeTypedirectly on the SMTP endpoint only will not have effect:

    <smtp:connector name="SMTP_exception_mailer" contentType="text/html" validateConnections="true" doc:name="SMTP"/>
    <smtp:outbound-endpoint host="host" port="25" connector-ref="SMTP_exception_mailer" to="[email protected]" from="system@mule" mimeType="text/html" doc:name="SMTP"/>
    

    BONUS TIP FOR COMPLEX HTML MAIL

    If you want to send complex html mail it's clear that using the set-payload component it will be quite complicated, a good alternative is to use the parse template component, this will allow you to write your html in an external file using also MEL expressions in it.