I have configured a twilio webhook for forwarding an sms message to my email. It is working, but for some reason I keep receiving duplicate emails. I have verified that the script is not being called twice, so there must be a problem with the script itself.
`<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
// Twilio SMS webhook handling
$body = $_POST['Body'] ?? '';
$from = $_POST['From'] ?? '';
$to = $_POST['To'] ?? '';
if (!empty($body) && !empty($from)) {
// Forward SMS to email
forwardSmsToEmail($from, $body, $to);
}
function forwardSmsToEmail($sender, $message, $recipient) {
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->Host = '127.0.0.1';
$mail->SMTPAuth = true;
$mail->Username = 'email';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl'; // or 'ssl'
$mail->Port = 1025; // or 465 for SSL
$mail->SMTPOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
];
//Sender and recipient
$mail->setFrom('Sender');
$mail->addAddress('Recipient');
//Content
$mail->isHTML(false);
$mail->Subject = "Text from $sender to $recipient";
$mail->Body = "$message";
$mail->SMTPDebug = 3;
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}