Send mail from laravel PHP framework and configure SMTP

Lots of web server do not allow to directly send email from website .They require SMTP authentication for security purpose.
Below we have created a laravel tutorial for Sending email and configuring SMTP in web server .

send-mail-in-laravel-smtp-configure

Step 1 : Create email

To use SMTP , we need to first create email id of our domain name ex: contact@domain.com .
So, to do this open your website cpanel ex: www.domain.com/cpanel /
Now access link ‘Email Account’ . There mention your email id ,password then click ‘Create Account’ .

Step 2 : Copy SMTP details

Here we have to copy SMTP details i.e

SMTP HOST
SMTP username
SMTP password
SMTP port 

These configuration can find from the same page that we created email .

  • Scroll to bottom of the page . Find your email , on right side click on ‘Set Up Mail Client’
  • Now on newly opened page , scroll quite middle of the page ,find there ‘Mail Client Manual Settings’
  • Find SMTP details on ‘ Secure SSL/TLS Settings ‘ or ‘Non-SSL Settings’ panel

If your website is not using https , you have to use ‘Non-SSL Settings’ .
Here note :

  • SMTP username is your email id
  • SMTP password is your email’s password that we created on 1st step
  • SMTP host is mentioned on ”Outgoing Server:’ ex: mail.domainname.com
  • SMTP port is mentioned on ‘Outgoing Server:’ ex: 25

Step 3: Put SMTP details on laravel’s config/mail.php

  • Put mail driver SMTP ex : driver ‘driver’ => env(‘MAIL_DRIVER’, ‘smtp’)
  • Put SMTP host ex: ‘host’ => env(‘MAIL_HOST’, ‘mail.domainname.com’)
  • Put SMTP port ex : ‘port’ => env(‘MAIL_PORT’,25)
  • Put SMTP username ex : ‘username’ => env(‘contact@domainname.com’)
  • Put SMTP password ex: ‘password’ => env(‘contact@abc’)

Now you will be able to send email from your Laravel application . To send mail , find below example code

Mail::send('emails.contact',$data, function ($message) {
			$message->from('contact@domainname.com','Company Name');
			$message->to('abc123@gmail.com ');
			$message->subject('Contact form submitted on domainname.com ');
 		});

Online Web Development Training,Video Tutorials