Answer the question
In order to leave comments, you need to log in
Why can't PHPmailer send a message via STMP?
Good evening, dear experts. I have already broken my head in an attempt to figure out why I have an error Message could not be sent. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubl...
I follow instructions from github and look for advice from people on the internet. But I still get an error connecting via stmp.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require $_SERVER["DOCUMENT_ROOT"].'/PHPMailer/src/Exception.php';
require $_SERVER["DOCUMENT_ROOT"].'/PHPMailer/src/PHPMailer.php';
require $_SERVER["DOCUMENT_ROOT"].'/PHPMailer/src/SMTP.php';
//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 = 'smtp.yandex.ru'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = '[email protected]'; //SMTP username
$mail->Password = 'password'; //SMTP password
$mail->SMTPSecure = 'TLS'; //Enable implicit TLS encryption
$mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User'); //Add a recipient
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$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}";
}
Answer the question
In order to leave comments, you need to log in
The simplest option - outgoing mail is prohibited on the hosting in order to combat spam
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question