Я хотел бы включить контактную форму в Hugo. Я смотрел бассейн и
Formcarry. Я хочу использовать PHPMailer на своем веб-пространстве. PHPMailer находится за пределами моего проекта Hugo. Как правильно реализовать контактную форму в hugo без внешних API?
Как я правильно установил пути. Идея верна?
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 protected]" 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 %}}
Вот 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('[email protected]', ''); // 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();
?>
Задача ещё не решена.
Других решений пока нет …