Contact form with phpmailer + Uniform

Update:
class.phpmailer.php became PHPMailer.php and you can find it here.

So you have to replace the part about it in my tutorial by:


In /site/plugins create a phpmailer-driver folder, and copy the official PHPMailer.php you can found it on the official phpmailer github repo. This give you the base functions of phpmailer.

add a phpmailer-driver.php with this content:

<?php
use PHPMailer\PHPMailer\PHPMailer;

email::$services['phpmailer'] = function($email) {
    require_once(__DIR__ . DS . 'PHPMailer.php');

    $mail = new PHPMailer();

    $mail->CharSet = 'UTF-8';
    $mail->setFrom($email->from);
    $mail->addReplyTo($email->replyTo);
    $mail->addAddress($email->to);


    $mail->isHTML(true);
    $mail->Subject = $email->subject;
    $mail->Body = $email->body;

    if (!$mail->send()) {
        throw new Error('PHPMailer error: ' . $mail->ErrorInfo);
    }
}
?>

And there is a little mistake in the form snippet, with wrong quote in <p aria-hidden="true" class="visually-hidden" id="pot"> causing accessibility issues:

<form id="contact" method="post" action="#contact">
  <fieldset>
    <h3>Contact me</h3>

<?php if ($form->success()): ?>
    <p class="success">Message sent</p>
<?php else: ?>
    <?php snippet('uniform/errors', ['form' => $form]) ?>
<?php endif; ?>

    <p>
      <label for="name">Enter your name (optional) :</label>
      <input id="name" <?php if ($form->error('name')): ?> class="error"
      <?php endif; ?> name="name" type="text" value="<?php echo $form->old('name') ?>">
    </p>
    <p>
      <label for="email">Enter your email* :</label>
      <input id="email" <?php if ($form->error('email')): ?> class="error"
      <?php endif; ?> name="email" type="email" value="<?php echo $form->old('email') ?>">
    </p>
    <p>
      <label for="message">Write your message* :</label>
      <textarea id="message" <?php if ($form->error('message')): ?> class="error"<?php endif; ?> name="message"><?php echo $form->old('message') ?></textarea>
    </p>
    <!-- The following field is for robots only, invisible to humans: -->
    <p aria-hidden="true" class="visually-hidden" id="pot">
      <?php echo csrf_field() ?>
      <?php echo honeypot_field() ?>
    </p>
    <p>
      <label for="copy"><input id="copy" name="receive_copy" type="checkbox" value="true" checked="true">Receive a copy of your message.</label>
      *Mandatory fields
    </p>
    <p>
      <input type="submit" value="Send !" class="submit" />
    </p>
  </fieldset>
</form>

and rename the phpmailer-driver folder to PHPMailer-driver

3 Likes