programmazione:php:notifica_via_email
Notifica di eventi via Email con PHP
Autore: Fabio Di Matteo
Ultima revisione: 05/06/2025 10:56
Mi serviva una minuscola api per implementare un servizio di notifica privato. La logica è che mando i campi oggetto, body ed eventualmente allegati tramite post ad una pagina php e la pagina invia la mail agli indirizzi specificati.
Un esempio di utilizzo con curl:
#!/usr/bin/env bash curl -d 'subject=Oggetto'\ -d 'htmlBody=Ciao <b>mondo</b>'\ -d 'altBody=Ciao mondo'\ -d 'attachment=/tmp/natura0.jpg,/tmp/natura1.jpg'\ -d 'to=destinatario0@email.com,destinatario1@email.com'\ -d 'k=chiave_segreta'\ https://mioserver/notify/notify.php
L'allegato deve risiedere sul server.
Ed ecco il codice (che fa uso di PHPMailer):
notify.php
<?php include("phpmailer/src/PHPMailer.php"); include("phpmailer/src/SMTP.php"); include("phpmailer/src/Exception.php"); include("conf.php"); use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; if (isset($_POST["subject"])) $subject=$_POST["subject"]; if (isset($_POST["htmlBody"])) $htmlBody=$_POST["htmlBody"]; if (isset($_POST["altBody"])) $altBody=$_POST["AltBody"]; if (isset($_POST["k"])) $k=$_POST["k"]; if (isset($_POST["attachment"])) $attachment=$_POST["attachment"]; if (isset($_POST["to"])) $to=$_POST["to"]; $r = strcmp($key, $k); if ($r!=0) { header("HTTP/1.1 403 Forbidden"); echo "Forbidden"; die(); } $bccs=array(); if (isset($_POST["to"])) { $bccs=explode(',',$to); } $attachs=array(); if (isset($_POST["attachment"])) { $attachs=explode(',',$attachment); } //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 = $Host; //Set the SMTP server to send through $mail->SMTPAuth = $SMTPAuth; //Enable SMTP authentication $mail->Username = $Username; //SMTP username $mail->Password = $Password; //SMTP password $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption $mail->Port = $Port; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS` //Recipients $mail->setFrom($setFrom, $setFromName); $mail->addReplyTo($addReplyTo, $addReplyToName); foreach ($bccs as $v) { $mail->addBCC($v); } foreach ($attachs as $v) { if (file_exists($attachment)) { $mail->addAttachment($v); } } //Content $mail->isHTML(true); //Set email format to HTML $mail->Subject = $subject; $mail->Body = $htmlBody; $mail->AltBody = $altBody; $mail->send(); header("HTTP/1.1 200 OK"); echo 'Message has been sent'; } catch (Exception $e) { header("HTTP/1.1 500 Internal Server Error"); echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; }
conf.php
<?php $Host = 'smtp.mioserver.it'; $SMTPAuth = true; $Username = 'miouser@email.org'; $Password = 'secret'; $Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS` $setFrom = 'miouser@email.org'; $setFromName = 'Notify'; $addReplyTo = 'miouser@email.org'; $addReplyToName='Notify'; $key="mia_chiave_super_segreta"; # echo -n "secret $(date)"|md5sum ?>
Per realizzare la chiave di sicurezza ho usato:
# echo -n "bla bla bla"|md5sum
programmazione/php/notifica_via_email.txt · Ultima modifica: 12/06/2025 14:19 da Fabio Di Matteo