Skip to content

Frontend Email Form

objectivehtml edited this page Dec 22, 2014 · 1 revision

Overview

You can setup an email form that allows user to send emails from the front-end. To setup this up you need two components. The form itself to trigger the email, and the parcel which will send it. The basic concept is you can build your form however you need, and then when the form is submitted an event is fired. A parcel type (which you must setup in the control panel) will then listen for that event and send an email appropriately.

Variables

email

This is a Postmaster_EmailModel which has all the information about your email.

post

Any post data used in the form is also available. {{ post.yourFormField }}


Example Form

{% if email is not defined %}
	
	{% set email = craft.postmaster.email() %}

{% endif %}

<!-- The form action must be empty to post back to the current page -->
<form action="" method="post">

    <!-- The form action must be set to following value -->
    <input type="hidden" name="action" value="postmaster/forms/send">

    <!-- The path to redirect the user after email send -->
    <input type="hidden" name="redirect" value="some/path/goes/here">

    <!-- Required Field -->
    <p>
    	<label for="toEmail">To Email</label><br>
    	<input type="text" id="toEmail" name="toEmail" value="{{ email.toEmail }}" />
    	{% if email.getError('toEmail') %}
    	<label for="toEmail">{{ email.getError('toEmail') }}</label>
    	{% endif %}
    </p>

    <!-- Optional Field -->
    <p>
    	<label for="toName">To Name</label><br>
    	<input type="text" id="toName" name="toName" value="{{ email.toName }}" />
    	{% if email.getError('toName') %}
    	<label for="toName">{{ email.getError('toName') }}</label>
    	{% endif %}
    </p>

    <!-- Optional Field -->
    <p>
    	<label for="fromName">From Name</label><br>
    	<input type="text" id="fromName" name="fromName" value="{{ email.fromName }}" />
    	{% if email.getError('fromName') %}
    	<label for="fromName">{{ email.getError('fromName') }}</label>
    	{% endif %}
    </p>

    <!-- Optional Field -->
    <p>
    	<label for="fromEmail">From Email</label><br>
    	<input type="text" id="fromEmail" name="fromEmail" value="{{ email.fromEmail }}" />
    	{% if email.getError('fromEmail') %}
    	<label for="fromEmail">{{ email.getError('fromEmail') }}</label>
    	{% endif %}
    </p>

    <!-- Optional Field -->
    <p>
    	<label for="replyTo">Reply To</label><br>
    	<input type="text" id="replyTo" name="replyTo" value="{{ email.replyTo }}" />
    	{% if email.getError('replyTo') %}
    	<label for="replyTo">{{ email.getError('replyTo') }}</label>
    	{% endif %}
    </p>

    <!-- Optional Field -->
    <p>
    	<label for="cc">CC</label><br>
    	<input type="text" id="cc" name="cc" value="{{ email.cc }}" />
    	{% if email.getError('cc') %}
    	<label for="cc">{{ email.getError('cc') }}</label>
    	{% endif %}
    </p>

    <!-- Optional Field -->
    <p>
    	<label for="bcc">Bcc</label><br>
    	<input type="text" id="bcc" name="bcc" value="{{ email.bcc }}" />
    	{% if email.getError('bcc') %}
    	<label for="bcc">{{ email.getError('bcc') }}</label>
    	{% endif %}
    </p>

    <!-- Required Field -->
    <p>
    	<label for="subject">Subject</label><br>
    	<input type="text" id="subject" name="subject" value="{{ email.subject }}" />
    	{% if email.getError('subject') %}
    	<label for="subject">{{ email.getError('subject') }}</label>
    	{% endif %}
    </p>

    <!-- Required Field -->
    <p>
    	<label for="body">Message</label><br>
    	<textarea type="text" id="body" name="body">{{ email.body }}</textarea>
    	{% if email.getError('body') %}
    	<label for="body">{{ email.getError('body') }}</label>
    	{% endif %}
    </p>

    <button>Send</button>

</form>