Script de envio de emails no PHPMAILER

Oi pessoal, Eu me chamado Anderson Serra, sou entusiasta em Tecnologia da Informação e hoje estou compartilhando com o vocês um script de envio de mensagens de email com o PHP.

Esse post refere-se ao código e não a instalação, mas como dica você pode instalar o phpmailer via composer ou então faça um git clone diretamente do site do github.

Segue o script:

<?php
// Autor: Anderson da Silva Serra
// data: 21/06/2020
// contato: anderson.silva.serra@gmail.com


//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

require_once("vendor/autoload.php");

/**
 * This example shows making an SMTP connection without using authentication.
 */
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //comentar se colocar em produção
//Set the hostname of the mail server
$mail->Host = 'hostname/fqdn - server'; //exemplo seuservidor.exemplo.com.br
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = '465';
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = 'true';
$mail->SMTPOptions = array(
                    'ssl' => array(
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                        'allow_self_signed' => true
                    )
                );
//Username to use for SMTP authentication
$mail->Username = 'remetente@exemplo.com.br';
//Password to use for SMTP authentication
$mail->Password = 'Password-here';
//Set who the message is to be sent from
$mail->setFrom('remetente@exemplo.com.br', 'Teste suporte');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@exemplo.com.br', 'Email válido para resposta de email');
//Set who the message is to be sent to
$mail->addAddress('destino@exemplo.com.br', 'Destinatário');
//Set the subject line
$mail->Subject = 'Script de envio de emails';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
//Replace the plain text body with one created manually
$mail->Body = 'Esse é email de teste do script de envios em lote de mensagens';
$mail->CharSet = 'UTF-8'; // Esse objeto é importante pois é a codificação padrão do navegador
//$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message sent!';
}

Comentários

Postagens mais visitadas deste blog

PYTHON - ENVIAR MENSAGEM VIA TELEGRAM

Dicas Zimbra

ZIMBRA - BACKUP CONTAS DE EMAILS FECHADAS A MAIS DE 90 DIAS