PHPMailer and Hugo

Is it possible to implement the PHPMailer in Hugo? When yes, where is the right place for the files for PHPMailer. Is the static folder the right place? How to include the contact.php in the markdown file?

2nd question.
Is it better to implement a contact form in golang, because Hugo is also written in golang.

yes you can add php files to static.

But if you really have to use phpMailer, I would suggest you set it up on a seperate server as standalone API and call for this API from within your Hugo project. (using ajax)

1 Like

For a contact form you can use any of these form relayers:


Thank you for your post. I am not sure that I understand you right. Do you mean that I should put the PHPMailer files in a seperate folder on my webspace? Not in /static/PHPMailer? Can you explain me what do you mena with own API?

I would like to include a contact form in Hugo. I viewed basin and
Formcarry. I want to use PHPMailer on my webspace. The PHPMailer is outside of
my Hugo project.

How did I set the paths correctly. Is the idea right?

www/
├── Hugoproject/
│ └── content
│ ├── contact.de.md
│ └── contact.en.md

├── PHPMailer
│ ├── contactform.php
│ └── src
│ ├── Exception.php
│ ├── OAuth.php
│ ├── PHPMailer.php
│ └── SMTP.php

The contact.de.md

---
title: "Kontakt"
date: 2018-02-05T13:50:19+01:00
draft: false
slug: kontakt
menu:
  main:
     name: Kontakt
     url: /kontakt/
     weight: 4
---

{{% divmd class="item-8A card" %}}
## Ihr Kontakt zu uns

---

<div>
<?php
require_once("../../../PHPMailer/contactform.php");
?>
</div>

<form method="POST" action="contactform.php" enctype="multipart/form-data">
  <fieldset>
  <p>
  <label>Vorname:</label>
  <input type="text" name="firstname" placeholder="Vorname" required>
  </p>
  <p>
  <label>Nachname:</label>
  <input type="text" name="lastname" placeholder="Nachname" required>
  </p>
  <p>
  <label>Email:</label>
  <input type="email" name="email" placeholder="email@example.com" required>
  </p>
  <p>
  <label>Telefon:</label>
  <input type="checkbox" name="contact_checkbox" value="yes"  onclick = "var input = document.getElementById('phone'); if(this.checked){ input.disabled = false; input.focus();}else{input.disabled = true;}">
  <input name='phone' id='phone' class='form-control' placeholder='Ihre Telefonnumber' disabled='disabled' pattern='^(\+[0-9]{2,3}|0+[0-9]{2,5}).+[\d\s\/\(\)-]' type='text'/>
  </p>
  <p>
  <label>Betrifft:</label>
  <input type="text" name="subject" placeholder="Betrifft" required>
  </p>
  <p>
  <label>Nachricht:</label>
  <textarea name="message" placeholder="Ihre Nachricht" required></textarea>
  </p>
  <p class="button">
  <button type="reset" name="resetBtn" value="Reset" class="btn btn-yellow">Reset</button>
  <button name='sendBtn' id='sendBtn' class='btn btn-warning btn-green' type='submit'>Send</button>
  </p>
  </fieldset>
</form>
{{% /divmd %}}

Here is the contactform.php

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';

$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['phone'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];

$mail = new PHPMailer(true);                                                   // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                                      // Enable verbose debug output
    $mail->isSMTP();                                                           // Set mailer to use SMTP
    $mail->Host = 'server';                           // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                                                    // Enable SMTP authentication
    $mail->Username = 'username';                                       // SMTP username
    $mail->Password = 'passwd';                                         // SMTP password
    $mail->SMTPSecure = 'tls';                                                 // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                                         // TCP port to connect to

    //Recipients
    $mail->setFrom($email);
    $mail->addAddress('info@example.com', '');    // Add a recipient
    $mail->addReplyTo($email);

    //Content
    $mail->isHTML(true);                                                       // Set email format to HTML
    $mail->Subject = $subject;
    $mail->AltBody = <<<EOT
<strong>Vorname:</strong> {$_POST['firstname']}<br />
<strong>Nachname:</strong> {$_POST['lastname']}<br />
<strong>Email:</strong> {$_POST['email']}<br />
<strong>Phone:</strong> {$_POST['phone']}<br />
<strong>Subject:</strong> {$_POST['subject']}<br />
<strong>Message:</strong><br /> {$_POST['message']}
EOT;
    $mail->Body = <<<EOT
<strong>Vorname:</strong> {$_POST['firstname']}<br />
<strong>Nachname:</strong> {$_POST['lastname']}<br />
<strong>Email:</strong> {$_POST['email']}<br />
<strong>Phone:</strong> {$_POST['phone']}<br />
<strong>Subject:</strong> {$_POST['subject']}<br />
<strong>Message:</strong><br /> {$_POST['message']}
EOT;
    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

// close SMTP sessions
$mail->SmtpClose();
?>