snxraven.me/mail_example.php

44 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2022-10-05 17:36:48 -04:00
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'dlinux.mail'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'username@dlinux.mail'; //SMTP username
$mail->Password = 'passwordhere'; //SMTP password
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
// $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 25; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('from@example.com', 'Website Contact');
$mail->addAddress('to@dlinux.mail', 'Username'); //Add a recipien
$mail->addReplyTo('info@example.com', 'Information');
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = $_POST['subject'];;
$mail->Body = $_POST['text'];;
$mail->send();
echo '<script>alert("The message has sent!")</script>';
header('Location: https://domain.tld/contact_complete.html');
exit();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}