PHPMailer — SMTP, POP3, IMAP — библиотека работы с E-mail
Github: https://github.com/PHPMailer/PHPMailer
Установка через composer:
# composer require phpmailer/phpmailer
Пример работы:
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and 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 = 'smtp1.example.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
// Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->CharSet = "UTF-8"; // Кодировка письма
$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}";
}
Пример минимальной отправки через яндекс почту и отправка сообщения на телеграмм бота:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
if (isset($_POST['auto_user']) && isset($_POST['phone_user']))
{
require_once $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->Host = 'smtp.yandex.ru';
$mail->SMTPAuth = true;
$mail->Username = 'email.my@mail.net';
$mail->Password = 'pass my';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
//Recipients
$mail->setFrom('notif.send@web-rise.ru', 'ecuchip.ru');
$mail->addAddress('email.1@mail.net');
$mail->addAddress('email.2@mail.net');
$mail->addAddress('email.3@mail.net');
$mail->CharSet = "UTF-8";
// Content
$mail->isHTML(false);
$mail->Subject = 'Сообщение с сайта';
$mail->Body = "Авто: " . $_POST['auto_user'] . "\nНомер: " . $_POST['phone_user'];
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
$bot = new \TelegramBot\Api\BotApi('КЛЮЧ БОТА');
$messageText = "Авто: " . $_POST['auto_user'] . "\nНомер: " . $_POST['phone_user'];
$bot->sendMessage(9999999991, $messageText);
$bot->sendMessage(9999999992, $messageText);
$bot->sendMessage(9999999993, $messageText);
}