For security reasons, sending emails using the anonymous mail()
function is not allowed on our servers. This helps protect against abuse and ensures better email deliverability.
Instead, you must use SMTP authentication to send emails from your PHP scripts. We recommend using PHPMailer — a widely-used and secure library for sending authenticated emails via SMTP.
Below is a basic example of how to configure PHPMailer for sending emails via SMTP:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
try {
// SMTP server configuration
$mail->isSMTP();
$mail->Host = 'smtp.yourdomain.com'; // Replace with your SMTP host
$mail->SMTPAuth = true;
$mail->Username = 'your@email.com'; // SMTP username
$mail->Password = 'your_password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // or PHPMailer::ENCRYPTION_SMTPS
$mail->Port = 587; // Typically 587 for TLS, or 465 for SSL
// Sender and recipient
$mail->setFrom('your@email.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
// Email content
$mail->isHTML(true);
$mail->Subject = 'Test Email from PHPMailer';
$mail->Body = '<p>This is a <strong>test email</strong> sent using PHPMailer with SMTP authentication.</p>';
$mail->AltBody = 'This is a plain-text version of the email content.';
$mail->send();
echo 'Email has been sent successfully';
} catch (Exception $e) {
echo "Email could not be sent. PHPMailer Error: {$mail->ErrorInfo}";
}
You can download the latest version of PHPMailer here:
🔗 https://github.com/PHPMailer/PHPMailer