Test Mail

Last modified by Manuel Smeria on 2021/03/18 11:28

cogA quick UI to verify a SMTP setup, that allows to send easily test emails
TypeSnippet
Category
Developed by

Jerome

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

This is a snippet using the old MailSender plugin which has been superseded by the new Mail Sender API.

Here's an example using the new Mail Sender API (will only work with version 6.2.5+):

{{velocity}}
#set ($message = $services.mailsender.createMessage("[email protected]", "subject"))
#set ($discard = $message.addPart("text/plain", "text content"))
#set ($result = $services.mailsender.send($message))
## Check if the message was created properly and if we have permissions to send emails
#if ($services.mailsender.lastError)
  {{error}}$exceptiontool.getStackTrace($services.mailsender.lastError){{/error}}
#end
## Check if the mail we tried to send has failed to be sent
#set ($statuses = $result.statusResult.getByState('FAILED'))
#if ($statuses.hasNext())
  {{error}}Error: $statuses.next().error{{/error}}
#end
{{/velocity}}

Note that this snippet doesn't store mail status results. If you wish to do that you should use instead:

#set ($result = $services.mailsender.send([$message], 'database'))

Restricted to admins.

{{velocity}}
#if($xwiki.hasAccessLevel('admin'))
#if("$!{request.action}" == 'send_mail' && "$!{request.lastname}" == '')
  ## Check submission and honey pot.
  #if("$!request.recipient" != '' && "$!request.subject" != '')
    #set($result = $xwiki.mailsender.sendTextMessage("[email protected]", $request.recipient, $request.subject, "$!request.message"))
    #if($result == -1)
    {{error}}A problem occurred while sending the mail: $context.error{{/error}}
    #else
    {{info}}Everything went OK.{{/info}}
    #end
  #else
    {{error}}Please specify at least a recipient and a subject{{/error}}
  #end
#end

{{html wiki=true}}
<form action="" method="post">
 <input name="action" value="send_mail" type="hidden" />
  ## this is a honey-pot hidden text field hat hopefully robots will not leave empty
 <div class="hidden">
 <input name="lastname" type="text" />
 </div>
 <fieldset>
   **TO:**<br />
   <input type="text" name="recipient" /><br />
   **SUBJECT:**<br />
   <input type="text" name="subject" /><br />
   **MESSAGE:**<br />
   <textarea name="message" style="width:99%;" rows=10>Your test message here.</textarea>
 </fieldset>
 <div>
   <span class="buttonwrapper"><input type="submit" class="button" value="Send" /></span>
 </div>
</form>
{{/html}}
#else
{{error}}Oops. Area restricted to Admins, sorry.{{/error}}
#end
{{/velocity}}

Get Connected