DWQA QuestionsHow to use PHPMailer in laravel ?
kandy asked 5 years ago

Step1: open command prompt and run following command

composer require phpmailer/phpmailer

Step2: Add following in your Controller or where you want to use mail function
use PHPMailerAutoload;
use PHPMailer;  
Step3: Use following code to send mail
$mail = new PHPMailer;

// notice the \ you have to use root namespace here
try {
$mail->isSMTP(); // tell to use smtp
$mail->CharSet = “utf-8”; // set charset to utf8
$mail->Host = $_SERVER[‘MAIL_HOST_NAME’];
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
$mail->Port = 25; // most likely something different for you. This is the mailtrap.io port i use for testing.
$mail->Username = “”;
$mail->Password = “”;
$mail->setFrom(“[email protected]xamle.com”, “examle Team”);
$mail->Subject = “examle”;
$mail->MsgHTML(“This is a test new test”);
$mail->addAddress(“[email protected]”, “admin”);
$mail->addAddress(“[email protected]”, “test”);
$mail->addReplyTo(‘[email protected]’, ‘Information’);
$mail->addBCC(‘[email protected]’);
$mail->addAttachment(‘/home/kundan/Desktop/abc.doc’, ‘abc.doc’); // Optional name
$mail->SMTPOptions= array(
‘ssl’ => array(
‘verify_peer’ => false,
‘verify_peer_name’ => false,
‘allow_self_signed’ => true
)
);

$mail->send();
} catch (phpmailerException $e) {
dd($e);
} catch (Exception $e) {
dd($e);
}
dd(‘success’);

 
Thanks 🙂