L
L
LoveProject2021-09-26 19:46:18
PHPMailer
LoveProject, 2021-09-26 19:46:18

PhpMailer SMTP how to send mail?

How to implement sending a message using PhpMailer, using the SMTP method?
I'm only interested in sending the letter itself, everything else works, before that I used the mail () function.

<?php
namespace application\models;
use phpmailer\PHPMailer\PHPMailer;
use phpmailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/PHPMailer.php';
require 'phpmailer/SMTP.php';
require 'phpmailer/Exception.php';

use application\core\Model;
class Account extends Model {

public function register($post) {
    $params = [
      'id' => null,
      'email' => $post['email'],
      'login' => $post['login'],
      'password' => password_hash($post['password'], PASSWORD_BCRYPT),
      'token' => $token,
      'status' => 0,
    ];

    $this->db->query('INSERT INTO accounts VALUES (:id, :email, :login, :password, :token, :status)', $params);
    $mail = new PHPMailer();
    $mail->isSMTP(); 
    $mail->Host   = 'smtp.yandex.ru';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'login';
    $mail->Password   = 'password';
    $mail->SMTPSecure = 'ssl';
    $mail->Port   = 465;
     
    $mail->setFrom('[email protected]', 'Иван');
    $mail->addAddress($post['email'], $post['Login']); 
     
    $mail->Subject = 'Register';
    $mail->msgHTML('Confirm: '.$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/account/confirm/'.$token);
    // Отправляем
    if ($mail->send()) {
      echo 'Письмо отправлено!';
    } else {
      echo 'Ошибка: ' . $mail->ErrorInfo;
  };
 ?>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question