Code Monkey home page Code Monkey logo

laravel-swiftmailer's Introduction

Laravel and Swift Mailer integrator

Latest Stable Version Total Downloads License

WAS Deprecated...

This package was deprecated, as starting from Laravel 5.0 and above, original Mail class automatically theoretically reconnects on every message. See commit here: [Force reconnection to fix mailing on daemon queues] (https://github.com/laravel/framework/commit/af8eb1face000f82e5c85e6eb822075fc313cbb9).

However, it looks like just calling stop on a transport doesn't do full reset, therefore may throw an error - see discussion here: laravel/framework#4573 (comment)


Package, which tries to solve long-term daemon worker issue. For reference:

Compatible with Laravel 4th and 5th versions.

Installation

Begin by installing this package through Composer. Edit your project's composer.json file to require yozaz/laravel-swiftmailer.

"require": {
	"yozaz/laravel-swiftmailer": "~4.0"
}

Next, update Composer from the Terminal:

composer update

Once this operation completes, the next step is to add the service provider. Open app/config/app.php (or config/app.php), and add a new item to the providers array.

'YOzaz\LaravelSwiftmailer\ServiceProvider',

The final step is to replace Laravel's native Mailer Facade with the one, provided in a package. Open app/config/app.php (or config/app.php), and replace "Mail" alias with:

'Mail' => 'YOzaz\LaravelSwiftmailer\Facade',

That's it! You're all set to go.

About

This package works in two possible error-safe modes: sends STOP command after every email is sent, and/or sends RESET/STOP+START commands before every email is sent. As a default, both modes are activated (so called "aggressive" mode). Such approach ensures SMTP connection is closed to avoid timeouts and broken pipes, or maintains it active for whole application living cycle. This is extremely important for long-living applications. E.g. when emails are sent through Beanstalkd + Supervisor + Laravel Queue Daemon Worker architecture, Laravel application never quits - therefore SMTP connection is kept active and timeouts after some time. Stopping, resetting and/or restarting SMTP connection automaticaly solves this problem in general.

N.B. While auto-reset feature is great, sometimes it's not a preferred behaviour. Be sure to check your SMTP server configuration before using this package.

Usage

Package is built in a way, that nothing special needs to be done. It's basically a wrapper, so all Mailer::send() and similar functions will work out of the box.

Auto-reset

Package starts, stops or resets SMTP adapter every time when email is sent. You can manipulate this through special helper functions:

// disable auto reset
Mailer::disableAutoReset();
// enable it back
Mailer::enableAutoReset();
// Set my status
Mailer::setAutoReset(true);
// check if auto-reset is enabled
if ( Mailer::autoResetEnabled() ) { ...

You can switch between STOP or RESET behaviours using native constants as a flag:

// send only STOP after every email
Mailer::setModeStop();
// send only RESET before every email
Mailer::setModeReset();
// aggressive mode - STOP and RESET (default)
Mailer::setModeBoth();

It is possible to stop or reset SMTP adapter explicitly.

Mailer::reset()->send(...);
Mailer::stop();

Silent mode

By default, failed emails will throw an Exception. If that's unexpected behaviour - e.g. because you don't need retry sending it - you can turn this mode on.

Mailer::setSilent(true);

N.B. Even if email will fail, before carrying over an Exception, package will try sending STOP command anyway (if such mode is enabled).

Initialization

Package has separate IoC binding. N.B. This package does not overwrite 'mailer' IoC binding in Laravel for legacy purposes.

var $mailer = App::make('laravel-swiftmailer.mailer');

If you prefer object initialization against Facades, you can instantiate Mailer class by yourself, with additional parameters if required. Package will try to instantiate required objects automatically as defaults.

var $mailer = new \YOzaz\LaravelSwiftmailer\Mailer();

Optinally, if you have custom wrapper for Laravel's Mailer, or want to manipulate with auto-reset functionality, you can pass additional parameters to IoC binding or class instantiation. Take a look at class constructor for details.

var $my_custom_mailer = App::make('mailer');
// pass custom mailer and disable auto-reset
var $mailer = new \YOzaz\LaravelSwiftmailer\Mailer( $my_custom_mailer, false );

Setting custom mailer instance

To set custom mailer instance, call this method:

Mailer::setMailer( $my_custom_mailer );

Credits

All credits go to xdecock, author of Swift Mailer, for providing ready-made solution implemented in this package.

License

Laravel-SwiftMailer package is open-sourced software licensed under the MIT license.

laravel-swiftmailer's People

Contributors

bitdeli-chef avatar yozaz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-swiftmailer's Issues

Errors In Production

[2016-05-03 17:42:05] production.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Fatal error: Cannot access protected property Illuminate\Mail\Mailer::$queue in /var/www/html/prod/*****/vendor/yozaz/laravel-swiftmailer/src/YOzaz/LaravelSwiftmailer/Mailer.php:294

Any idea why this is happening ?

[2016-05-05 17:11:36] production.ERROR: ErrorException: fwrite(): send of 18 bytes failed with errno=32 Broken pipe in /var/www/html/prod/forcebook/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:232
Stack trace:
#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'fwrite(): send ...', '/var/www/html/p...', 232, Array)
#1 /var/www/html/prod/forcebook/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php(232): fwrite(Resource id #533, 'RSET\r\nQUIT\r\nQUI...')
#2 /var/www/html/prod/forcebook/vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/AbstractFilterableInputStream.php(173): Swift_Transport_StreamBuffer->_commit('RSET\r\nQUIT\r\nQUI...')
#3 /var/www/html/prod/forcebook/vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/AbstractFilterableInputStream.php(92): Swift_ByteStream_AbstractFilterableInputStream->_doWrite('RSET\r\nQUIT\r\nQUI...')
#4 /var/www/html/prod/forcebook/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(276): Swift_ByteStream_AbstractFilterableInputStream->write('QUIT\r\n')
#5 /var/www/html/prod/forcebook/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php(270): Swift_Transport_AbstractSmtpTransport->executeCommand('QUIT\r\n', Array, Array)
#6 /var/www/html/prod/forcebook/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(216): Swift_Transport_EsmtpTransport->executeCommand('QUIT\r\n', Array)
#7 /var/www/html/prod/forcebook/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(307): Swift_Transport_AbstractSmtpTransport->stop()
#8 /var/www/html/prod/forcebook/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(151): Illuminate\Mail\Mailer->forceReconnection()
#9 [internal function]: Illuminate\Mail\Mailer->send('pto.email.pto_u...', Array, Object(Closure))
#10 /var/www/html/prod/forcebook/vendor/yozaz/laravel-swiftmailer/src/YOzaz/LaravelSwiftmailer/Mailer.php(372): call_user_func_array(Array, Array)
#11 /var/www/html/prod/forcebook/vendor/yozaz/laravel-swiftmailer/src/YOzaz/LaravelSwiftmailer/Mailer.php(346): YOzaz\LaravelSwiftmailer\Mailer->__call('send', Array)
#12 /var/www/html/prod/forcebook/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(129): YOzaz\LaravelSwiftmailer\Mailer->handleQueuedMessage(Object(Illuminate\Queue\Jobs\DatabaseJob), Array)
#13 /var/www/html/prod/forcebook/vendor/laravel/framework/src/Illuminate/Queue/Jobs/DatabaseJob.php(50): Illuminate\Queue\Jobs\Job->resolveAndFire(Array)
#14 /var/www/html/prod/forcebook/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(207): Illuminate\Queue\Jobs\DatabaseJob->fire()
#15 /var/www/html/prod/forcebook/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(155): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), '3', 0)
#16 /var/www/html/prod/forcebook/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(111): Illuminate\Queue\Worker->pop(NULL, NULL, 0, '10', '3')
#17 /var/www/html/prod/forcebook/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(85): Illuminate\Queue\Worker->runNextJobForDaemon(NULL, NULL, 0, '10', '3')
#18 /var/www/html/prod/forcebook/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(103): Illuminate\Queue\Worker->daemon(NULL, NULL, 0, 128, '10', '3')
#19 /var/www/html/prod/forcebook/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(71): Illuminate\Queue\Console\WorkCommand->runWorker(NULL, NULL, 0, 128, true)
#20 [internal function]: Illuminate\Queue\Console\WorkCommand->fire()

Not working out of the box in Laravel 4.2

This solution doesn't work out of the box. When queueing emails the handler is mailer@handleQueuedMessage which resolves into Illuminate\Mail\Mailer@handleQueuedMessage as we don't replace mailer in the DI container.

And even if I explicitly call Queue::push with laravel-swiftmailer.mailer@handleQueuedMessage YOzaz\LaravelSwiftmailer\Mailer doesn't intercept it and passes payload to the Illuminate\Mail\Mailer

UPD: I use Laravel 4.2

Unable to connect with TLS encryption

I'm getting this error from time to time:

[2015-09-20 19:57:09] prod.ERROR: exception 'Swift_TransportException' with message 'Unable to connect with TLS encryption' in /var/www/html/backend/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php:289
Stack trace:
#0 /var/www/html/backend/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(118): Swift_Transport_EsmtpTransport->_doHeloCommand()
#1 /var/www/html/backend/vendor/yozaz/laravel-swiftmailer/src/YOzaz/LaravelSwiftmailer/Mailer.php(169): Swift_Transport_AbstractSmtpTransport->start()
#2 /var/www/html/backend/vendor/yozaz/laravel-swiftmailer/src/YOzaz/LaravelSwiftmailer/Mailer.php(197): YOzaz\LaravelSwiftmailer\Mailer->resetSwiftTransport()
#3 /var/www/html/backend/bootstrap/compiled.php(3271): YOzaz\LaravelSwiftmailer\Mailer->send('emails.vip-aler...', Array, Object(Closure))
#4 /var/www/html/backend/app/Acme/Services/EmailSenderService.php(18): Illuminate\Support\Facades\Facade::__callStatic('send', Array)
#5 /var/www/html/backend/app/Acme/Services/EmailSenderService.php(18): YOzaz\LaravelSwiftmailer\Facade::send('emails.vip-aler...', Array, Object(Closure))

Even more SSL errors

Hi all,

there might be some more cases with (possibly related) SSL errors. See exception stack trace below. I don't really know what's causing this specific error. Could it be Illuminate/Mail/Mailer trying to force a reconnection while YOzaz/LaravelSwiftmailer already did that?

fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry #0
[internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'fwrite(): SSL o...', '...', 232, Array) #1
[...]vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php(232): fwrite(Resource id #413, 'RSET??QUIT??QUI...') #2
[...]vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/AbstractFilterableInputStream.php(173): Swift_Transport_StreamBuffer->_commit('RSET??QUIT??QUI...') #3
[...]vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/AbstractFilterableInputStream.php(92): Swift_ByteStream_AbstractFilterableInputStream->_doWrite('RSET??QUIT??QUI...') #4
[...]vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(276): Swift_ByteStream_AbstractFilterableInputStream->write('QUIT??') #5
[...]vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php(270): Swift_Transport_AbstractSmtpTransport->executeCommand('QUIT??', Array, Array) #6
[...]vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(216): Swift_Transport_EsmtpTransport->executeCommand('QUIT??', Array) #7
[...]vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(315): Swift_Transport_AbstractSmtpTransport->stop() #8
[...]vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(159): Illuminate\Mail\Mailer->forceReconnection() #9
[internal function]: Illuminate\Mail\Mailer->send('emails.exceptio...', Array, Object(Closure)) #10
[...]vendor/yozaz/laravel-swiftmailer/src/YOzaz/LaravelSwiftmailer/Mailer.php(372): call_user_func_array(Array, Array) #11
[...]vendor/yozaz/laravel-swiftmailer/src/YOzaz/LaravelSwiftmailer/Mailer.php(346): YOzaz\LaravelSwiftmailer\Mailer->__call('send', Array) #12
[...]vendor/yozaz/laravel-swiftmailer/src/YOzaz/LaravelSwiftmailer/Mailer.php(346): YOzaz\LaravelSwiftmailer\Mailer->send('emails.exceptio...', Array, Object(Closure)) #13
[...]vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(129): YOzaz\LaravelSwiftmailer\Mailer->handleQueuedMessage(Object(Illuminate\Queue\Jobs\DatabaseJob), Array) #14 

Laravel 4.2 issue

I am using this package to solve the laravel issue 4573.

Unfortunately it's not working for me. After some time swiftmailer will start to throw "bad write retry" error and I've to keep restarting supervisord to temporarily solve the issue.

Full error:
fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry

I am calling artisan like this from supervisord worker:
php artisan queue:work --queue=my_emails_tube --daemon --env=production

Any hints? Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.