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?

├── Hugoproject/
│ └── content
│ ├──
│ └──

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


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

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



<form method="POST" action="contactform.php" enctype="multipart/form-data">
  <input type="text" name="firstname" placeholder="Vorname" required>
  <input type="text" name="lastname" placeholder="Nachname" required>
  <input type="email" name="email" placeholder="" required>
  <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'/>
  <input type="text" name="subject" placeholder="Betrifft" required>
  <textarea name="message" placeholder="Ihre Nachricht" required></textarea>
  <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>
{{% /divmd %}}

Here is the contactform.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

    $mail->addAddress('', '');    // Add a recipient

    $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']}
    $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']}
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;

// close SMTP sessions