# PHPMailer and Hugo

**URL:** <https://discourse.gohugo.io/t/phpmailer-and-hugo/11166>\
**Category:** Uncategorized\
**Created:** [March 23, 2018, 3:29pm UTC](https://discourse.gohugo.io/t/phpmailer-and-hugo/11166 "2018-03-23T15:29:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joerg](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@Joerg](https://discourse.gohugo.io/u/Joerg)\
**Post date:** [March 23, 2018, 3:29pm UTC](https://discourse.gohugo.io/t/phpmailer-and-hugo/11166/1 "2018-03-23T15:29:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [March 23, 2018, 4:06pm UTC](https://discourse.gohugo.io/t/phpmailer-and-hugo/11166/2 "2018-03-23T16:06:50Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![olafghanizadeh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/olafghanizadeh/32/4494_2.png) [@olafghanizadeh](https://discourse.gohugo.io/u/olafghanizadeh)\
**Post date:** [March 24, 2018, 10:53am UTC](https://discourse.gohugo.io/t/phpmailer-and-hugo/11166/3 "2018-03-24T10:53:51Z")

</div>

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

> **[Basin | Simple form backend for designers and developers](https://usebasin.com)**
>
> Nobody likes writing server side code for web forms. Basin saves you development time so you can start collecting form submissions and tracking conversions right away with no backend coding required.

  

> **[Formcarry. | Hassle-Free Form Backend](https://www.formcarry.com)**
>
> Hassle-free Form Backend for your forms.

---

<div class="post-metadata">

**Author:** ![Joerg](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@Joerg](https://discourse.gohugo.io/u/Joerg)\
**Post date:** [March 25, 2018, 3:36pm UTC](https://discourse.gohugo.io/t/phpmailer-and-hugo/11166/4 "2018-03-25T15:36:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Joerg](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@Joerg](https://discourse.gohugo.io/u/Joerg)\
**Post date:** [March 31, 2018, 2:13pm UTC](https://discourse.gohugo.io/t/phpmailer-and-hugo/11166/5 "2018-03-31T14:13:03Z")

</div>

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();
?>
```
