php phpmailer code example
Example 1: php mailer
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp1.example.com;smtp2.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User');
$mail->addAddress('[email protected]');
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$mail->addAttachment('/var/tmp/file.tar.gz');
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->isHTML(true);
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Example 2: core php mail function without phpmailer
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
session_start();
$to = '[email protected]';
$subject = 'Subject xxxxx xxxxxx';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=charset=utf-8\r\n";
$headers .= "From: " . $_POST['email'] . "\r\n";
$headers .= "CC: [email protected]\r\n";
$message = "<html><body>";
$message .= '<table style="border-color: #666; background: #eee; cellpadding="10">';
$message .= "<tr><td><strong>Name:</strong> </td><td>" . $_POST['username'] . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . $_POST['email'] . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
echo $message;
$response=mail($to, $subject, $message, $headers);
if($response==1)
{
echo "<script language='javascript' type='text/javascript'>
window.location = 'index.html';
</script>";
}
else{
echo
"<script language='javascript' type='text/javascript'>
alert('mail send failed');
</script>";
}
?>
Example 3: phpmailer example php
<?php
require_once('class.phpmailer.php');
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->SMTPDebug = 1;
$mailer->Port = 587;
$mailer->Host = 'smtplw.com.br';
$mailer->SMTPAuth = true;
$mailer->Username = 'smtplocaweb';
$mailer->Password = 'Gwb9etA323';
$mailer->FromName = 'Bart S. Locaweb';
$mailer->From = '[email protected]';
a mesma caixa postal configurada no remetente do SMTP
$mailer->AddAddress('[email protected]','Nome do
destinatário');
$mailer->Subject = 'Teste enviado através do PHP Mailer
SMTPLW';
$mailer->Body = 'Este é um teste realizado com o PHP Mailer
SMTPLW';
if(!$mailer->Send())
{
echo "Message was not sent";
echo "Mailer Error: " . $mailer->ErrorInfo; exit; }
print "E-mail enviado!"
?>
Example 4: require php mailer
composer require phpmailer/phpmailer
Example 5: phpmailer
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'smtp1.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'secret';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User');
$mail->addAddress('[email protected]');
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$mail->addAttachment('/var/tmp/file.tar.gz');
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->isHTML(true);
$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}";
}
Example 6: PHPMailer
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);