14

Does anybody have a working example how I can work with PHPMailer in Laravel 5? In Laravel 4 it was quiet simple to use but the same method doesn't work in L5. Here it is what I did in L4:

Added in composer.json:

"phpmailer/phpmailer": "dev-master",

And in the controller I've used it like this:

$mail = new PHPMailer(true);
try {
  $mail->SMTPAuth(...);
  $mail->SMTPSecure(...);
  $mail->Host(...);
  $mail->port(...);

  .
  .
  .

  $mail->MsgHTML($body);
  $mail->Send();
} catch (phpmailerException $e) {
  .
  .
} catch (Exception $e) {
  .
  .
}

But it doesn't work in L5. Any idea? Thanks!

Sas Sam
  • 681
  • 4
  • 10
  • 25

2 Answers2

12

Well there are multiple mistakes i think... This is a working example of sending mail with PhpMailer in Laravel 5. Just tested it.

        $mail = new \PHPMailer(true); // 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->SMTPAuth = true;  // use smpt auth
        $mail->SMTPSecure = "tls"; // or ssl
        $mail->Host = "yourmailhost";
        $mail->Port = 2525; // most likely something different for you. This is the mailtrap.io port i use for testing. 
        $mail->Username = "username";
        $mail->Password = "password";
        $mail->setFrom("youremail@yourdomain.de", "Firstname Lastname");
        $mail->Subject = "Test";
        $mail->MsgHTML("This is a test");
        $mail->addAddress("recipient@anotherdomain.de", "Recipient Name");
        $mail->send();
    } catch (phpmailerException $e) {
        dd($e);
    } catch (Exception $e) {
        dd($e);
    }
    die('success');

And of course, you need to do a composer update after adding the depency to composer.json

However, i would prefer the laravel built in SwiftMailer. http://laravel.com/docs/5.0/mail

shock_gone_wild
  • 6,700
  • 4
  • 28
  • 52
  • As I see, my example exactly the same except that *@+%! root namespace backslash char... :) My eyes just skipped this and I don't know why... :( So thanks, it's working now. And yes, I'll change the code to Laravel's in-built mailer but it's a legacy code and first I have to make the upgrade from 4.2 to 5 and then I'll make some improvements. Thanks again! – Sas Sam Apr 07 '15 at 17:22
  • When i said multiple mistakes , i thought that you were using things like $mail->Host = "yourmailhost"; not as an assignment but as function $mail->Host("yourmailhost"); But i see, you only wanted to point out what you generally did and i should'nt had seen it as correct syntax. Glad to help you anyway ;) – shock_gone_wild Apr 07 '15 at 19:12
  • can anyone answer that how can I use NONSSL in same settings ? without using ssl laravel gives error , and I don't have working SSL credentials – Chintan Gor Nov 29 '16 at 07:00
  • SSL credentials ? You mean SMTP credentials, right ? – shock_gone_wild Nov 29 '16 at 07:23
7

In Laravel 5.5 or Above, you need to do the following steps

Install the PHPMailer on your laravel Application.

composer require phpmailer/phpmailer

Then goto your controller where you want to use phpmailer.

<?php
namespace App\Http\Controllers;

use PHPMailer\PHPMailer;

class testPHPMailer extends Controller
{
    public function index()
    {
        $text             = 'Hello Mail';
        $mail             = new PHPMailer\PHPMailer(); // create a n
        $mail->SMTPDebug  = 1; // debugging: 1 = errors and messages, 2 = messages only
        $mail->SMTPAuth   = true; // authentication enabled
        $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
        $mail->Host       = "smtp.gmail.com";
        $mail->Port       = 465; // or 587
        $mail->IsHTML(true);
        $mail->Username = "testmail@gmail.com";
        $mail->Password = "testpass";
        $mail->SetFrom("testmail@gmail.com", 'Sender Name');
        $mail->Subject = "Test Subject";
        $mail->Body    = $text;
        $mail->AddAddress("testreciver@gmail.com", "Receiver Name");
        if ($mail->Send()) {
            return 'Email Sended Successfully';
        } else {
            return 'Failed to Send Email';
        }
    }
}
Naveen DA
  • 4,148
  • 4
  • 38
  • 57