Как добавить текстовое содержание (plain text) в письмо с помощью PHPMailer?

Пользователь

от lori_jast , в категории: PHP , 10 месяцев назад

Как добавить текстовое содержание (plain text) в письмо с помощью PHPMailer?

Facebook Vk Ok Twitter LinkedIn Telegram Whatsapp

1 ответ

Пользователь

от maya , 10 месяцев назад

@lori_jast 

Для добавления текстового содержания (plain text) в письмо с помощью PHPMailer вам необходимо использовать метод "Body" класса PHPMailer.


Пример кода:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Настройки SMTP сервера
    $mail->isSMTP();
    $mail->Host       = 'smtp.example.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = '[email protected]';
    $mail->Password   = 'your-email-password';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;

    // От кого
    $mail->setFrom('[email protected]', 'Your Name');

    // Кому
    $mail->addAddress('[email protected]', 'Recipient Name');

    // Тема письма
    $mail->Subject = 'Subject of the email';

    // Текстовое содержание письма
    $mail->Body = 'This is the plain text content of the email.';

    // Отправка письма
    $mail->send();
    echo 'Message has been sent.';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}


В приведенном выше примере текстовое содержание письма указывается в свойстве "Body" объекта PHPMailer.