120 likes | 466 Views
Sending Emails with SendGrid and Laravel. Sayed Ahmed Computer Engineering, BUET, Bangladesh (2001) MSc. Computer Science, U of Manitoba, Canada Exploring Computing from 1996. Requirements. A SendGrid Account Cloud based email delivery services http://sendgrid.com/ Beanstalkd
E N D
Sending Emails with SendGridand Laravel Sayed Ahmed Computer Engineering, BUET, Bangladesh (2001) MSc. Computer Science, U of Manitoba, Canada Exploring Computing from 1996
Requirements • A SendGrid Account • Cloud based email delivery services • http://sendgrid.com/ • Beanstalkd • Email queue • http://kr.github.io/beanstalkd/ • Laravel go mail worker • Laravel Mail sender using Sendgrid [as an example] • https://github.com/zackkitzmiller/laravel-go-mail-worker • https://github.com/zackkitzmiller/laravel-go-mail-worker/blob/master/laravel-go-mail-worker/app/commands/QueueEmailCommand.php • https://github.com/zackkitzmiller/laravel-go-mail-worker/blob/master/go/src/zackkitzmiller/laravel-go-mail-worker/main.go
Must Watch • Mail Queue configuration for Laravel • https://github.com/laravel/laravel/blob/master/app/config/queue.php • Basic Mail Configuration for Laravel [Not a Must] • https://github.com/laravel/laravel/blob/master/app/config/mail.php
How These All Will Work • You need to queue the message in Beanstalkd • You need to configure Laravel and Laravel Go Mailer for that • From your code, you need to use methods to queue the message • $message = "This is a queue test job. We won't really need to pass data for this demo."; • Queue::push('QueueDemo', array('message' => $message)); • This apparently looks like will also work • Mail::queueOn('queue-name', 'emails.welcome', $data, function($message) { $message • ->to('foo@example.com', 'John Smith') • ->subject('Welcome!'); }); • Then the GO application will send this email • It’s Laravel go mail worker • Then Go mailer will use SendGrid to send the mail
How These All Will Work • Configuration for Go mail worker • BEANSTALKD: location of the beanstalk installation (e.g. localhost:11300) • SENDGRID_USER: username for sendgrid account • SENDGRID_PASS: password for sendgrid account
Installation • Install Go Mailer • Artisan command email:send. Will do that for you • Not sure yet that it will work; after working with it, I will update • Though you can get the copy of the Laravel-go-mailer and use that for sure
Miscellaneous • How to Download and Install Beanstalkd • http://kr.github.io/beanstalkd/download.html • Src: git clone git://github.com/kr/beanstalkd.git • Install: • Mac: sudo port install beanstalkd • Debian: sudo apt-get install beanstalkd • Ubuntu: sudo apt-get install beanstalkd • Fedora: su -c 'yum install beanstalkd' • Sending emails from Laravel using Mail Servers such as SwiftMailer • http://laravel.com/docs/mail • Mail Queue tutorial for Laravel • http://vimeo.com/64779946
Miscellaneous • Beanstalkd listed as the default in Laravel-go-mailer queue config file • https://github.com/zackkitzmiller/laravel-go-mail-worker/blob/master/laravel-go-mail-worker/app/config/queue.php • These are the environment variables • Set SENDGRID_USER, SENDGRID_PASS, and BEANSTALKD envvars • Can be set in Linux profile type files • Also from within PHP you can set
Environment Variables and PHP • echo 'My username is ' .$_ENV["USER"] . '!'; • // Example use of getenv()$ip = getenv('REMOTE_ADDR'); • putenv("UNIQID=$uniqid");
Beanstalkd Configuration File Content • You can know but you will not need to work with it much for basic functionality • [program:qlistener] • command=php/var/www/laravel/artisan queue:listenprocess_name=%(program_name)s • numprocs=1 • numprocs_start=0 • autostart=true • autorestart=true • startsecs=1 • startretries=3 • exitcodes=0,2 • stopsignal=TERM • stopwaitsecs=10
Beanstalkd Configuration File Content • user=ubuntu • redirect_stderr=false • stdout_logfile=/var/log/supervisor/qlistener-stdout.log • stdout_logfile_maxbytes=50MB • stdout_logfile_backups=10 • stdout_capture_maxbytes=0 • stdout_events_enabled=false • stderr_logfile=/var/log/supervisor/qlistener-stderr.log • stderr_logfile_maxbytes=50MB • stderr_logfile_backups=10 • stderr_capture_maxbytes=0 • tderr_events_enabled=false environment=LARAVEL_ENV="production" • serverurl=AUTO
Reference • http://b.z19r.com/post/sending-email-with-laravel-sendgrid-beanstalkd-and-go