0

I'm rookie in PHP. I connected PHPMailer. I was training send an email, but I got error

My code

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require ("vendor/autoload.php");

$mail = new PHPMailer(true);
try {
    //Server settings
    $mail->SMTPDebug = 2;

    $mail->isSMTP();
    $mail->Host = 'localhost';
    $mail->SMTPAuth = true;
    $mail->Username = ''; -The mail I'm trying to send an email to
    $mail->Password = ''; - password of this email
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    $mail->setFrom('', 'admin'); The mail I'm trying to send an email to
    $mail->addAddress('', 'Recipient'); -recipient
    $mail->isHTML(true);
    $mail->Subject = 'Test Mail Subject!';
    $mail->Body    = 'This is SMTP Email Test';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}

I want to say in advance that this mail is not secure. I also tried change STMPSECURE(tls)

I got

SMTP ERROR: Failed to connect to server: Connection refused (111)

4
  • $mail->Host = 'localhost';, have you built a localhost mail server ? Commented Oct 29, 2019 at 6:29
  • But what should I write in the host? the mailbox from which I send the letter? Commented Oct 29, 2019 at 6:32
  • no, the SMTP server that will relay your email, e.g. smtp.googlemail.com, but you must have an account on that server for $mail->Username and $mail->Password Commented Oct 29, 2019 at 6:38
  • Please read docs and search before you post. You can't use TLS/SSL connections to localhost because certificate names will not match, though you can safely use connections to localhost if you have a local mail server; there is no requirement to use an external server as the accepted answer suggests. Commented Oct 29, 2019 at 8:30

1 Answer 1

1

You must have an account on a SMTP server, for example: smtp.googlemail.com (and easiest to get), so you can set like this:

$mail->Host = 'smtp.googlemail.com';
$mail->SMTPAuth = true;
$mail->Username = 'YOUR_GOOGLE_USERNAME';  // NOT email address to send to
$mail->Password = 'YOUR_GOOGLE_PASSWORD';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.