0

I want to send a email in laravel this is the code that im using.. When i run this code it displayed 'The mail has been sent successfully' but i didnt reserve any email in my gmail. What's wrong with the code?

I already test this simple code. It works perfectly.

Mail::send(['text' => 'emails.html'], ['name' => 'Jay'], function($message)
        {
            $message->to('to@gmail.com', 'To Philex')->subject('test emails');
            $message->from('from@gmail.com', 'Philex');
        });

But i want to use swift mailer. any one can help me?

use Mail;
use Swift_SmtpTransport;
use Swift_Message;
use Swift_Mailer;


public function sendemail()
{

    $data_toview = array();
    $data_toview['bodymessage'] = "test from philex dev";

    $email_sender   = 'from@gmail.com';
    $email_pass     = 'yourpassword';
    $email_to       = 'to@gmail.com';

    // Backup your default mailer
    $backup = \Mail::getSwiftMailer();

    try{

        //https://accounts.google.com/DisplayUnlockCaptcha
        // Setup your gmail mailer
        $transport = new \Swift_SmtpTransport('smtp.gmail.com', 587, 'tls');
        $transport->setUsername($email_sender);
        $transport->setPassword($email_pass);

        // Any other mailer configuration stuff needed...
        $gmail = new \Swift_Mailer($transport);

        // Set the mailer as gmail
        \Mail::setSwiftMailer($gmail);

        $data['emailto'] = $email_sender;
        $data['sender'] = $email_to;
        //Sender dan Reply harus sama

        Mail::send('emails.html', $data_toview, function($message) use ($data)
        {

            $message->from($data['sender'], 'Laravel Mailer');
            $message->to($data['emailto'])
            ->replyTo($data['sender'], 'Laravel Mailer')
            ->subject('Test Email');

            echo 'The mail has been sent successfully';

        });

    }catch(\Swift_TransportException $e){
        $response = $e->getMessage() ;
        echo $response;
    }


    // Restore your original mailer
    Mail::setSwiftMailer($backup);

}
  • Have you enabled SMTP sending in your GMAIL settings? – George Hanson May 14 '19 at 08:15
  • 1
    Is there any reason you don't want to use Laravels default mailer, which is basically built on top of SwiftMailer? – Dan May 14 '19 at 08:19
  • @GeorgeHanson yes i already done setting up on gmail setting. – Arnie Mallari May 14 '19 at 08:22
  • @Dan because in laravels defaul mailer. i cant set my own body message on that. – Arnie Mallari May 14 '19 at 08:23
  • @Dan i mean is.. is this area of laravel default mailer ['text' => 'emails.html'] , i need to set a body message inside that views emails fodler html.blade.php . Otherwise in swiftmailer i just need to setup it here $data_toview['bodymessage'] = "test from philex dev"; – Arnie Mallari May 14 '19 at 08:30

0 Answers0