PHP mail() does not usually allow you to use the external SMTP server and it does not support SMTP authentication — required by many a mail servers today, including ours.
To connect to our outgoing SMTP server from a PHP script using SMTP authentication and send an email:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/to/PHPMailer/src/Exception.php'; // Be sure to set the correct path
require 'path/to/PHPMailer/src/PHPMailer.php'; // Be sure to set the correct path
require 'path/to/PHPMailer/src/SMTP.php'; // Be sure to set the correct path
// Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = SMTP::DEBUG_OFF; // To enable debugging, set SMTP::DEBUG_SERVER
$mail->isSMTP();
$mail->Host = 'mail.web-solutions.eu';
$mail->SMTPAuth = true;
$mail->Username = 'user@domain.tld'; // Enter your email address
$mail->Password = 'password'; // Enter your password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->setFrom('from@domain.tld', 'From Name');
$mail->addAddress('to@domain.tld');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body in bold!';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
For more information, see the official documentation.
You will need the following details set:
Back Last updated: April 24, 2015 by