59 lines
2.0 KiB
PHP
59 lines
2.0 KiB
PHP
<?PHP
|
|
/**
|
|
* Contains a class MDMailerHelper for improved handling of mails in the md ecosystem.
|
|
*/
|
|
declare(strict_types = 1);
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\SMTP;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
/**
|
|
* Class containing static functions for an easier handling of mails.
|
|
*/
|
|
final class MDMailerHelper {
|
|
|
|
/**
|
|
* PGP-encrypts a message to a given email address.
|
|
*
|
|
* @param string $to Recipient email address.
|
|
* @param string $msg Message contents.
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function pgp_encrypt(string $to, string $msg):string {
|
|
|
|
$msg = shell_exec("echo " . escapeshellarg($msg) . " | gpg2 --always-trust --recipient " . escapeshellarg($to) . " --encrypt --armor --local-user " . escapeshellarg(MD_CONF_EMAIL::PGP_ENC_KEY) . " --sign");
|
|
if ($msg === null) return "Error encrypting message";
|
|
return $msg;
|
|
|
|
}
|
|
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @return PHPMailer
|
|
*/
|
|
public static function setup_PHPMailer():PHPMailer {
|
|
|
|
$mail = new PHPMailer(true);
|
|
|
|
//Server settings
|
|
# $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
|
|
$mail->isSMTP(); // Send using SMTP
|
|
$mail->Host = MD_CONF_EMAIL::SMTP_HOST; // Set the SMTP server to send through
|
|
$mail->SMTPAuth = true; // Enable SMTP authentication
|
|
$mail->Username = MD_CONF_EMAIL::SMTP_USERNAME; // SMTP username
|
|
$mail->Password = MD_CONF_EMAIL::SMTP_PASSWORD; // SMTP password
|
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
|
|
$mail->Port = 465; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
|
|
|
|
$mail->setFrom(MD_CONF_EMAIL::SMTP_FROM, MD_CONF_EMAIL::SMTP_DISPLAYED_FROM);
|
|
$mail->addReplyTo(MD_CONF_EMAIL::SMTP_REPLY_TO_DEFAULT);
|
|
|
|
return $mail;
|
|
|
|
}
|
|
|
|
}
|