[av_codeblock wrapper_element=” wrapper_element_attributes=” codeblock_type=” av_uid=’av-kldo4zxb’ custom_class=’cust-codeblock-ads1 mb-small’]




[/av_codeblock]

[av_textblock size='' font_color='' color='' av-medium-font-size='' av-small-font-size='' av-mini-font-size='' av_uid='av-k2wldpg6' custom_class='' admin_preview_bg='']
[av_breadcrumbs]
[/av_textblock]

[av_one_half first min_height='av-equal-height-column' vertical_alignment='av-align-middle' space='no_margin' custom_margin='aviaTBcustom_margin' margin='0px,50px' padding='0px' padding_sync='true' border='' border_color='' radius='0px' radius_sync='true' background_color='' src='' attachment='' attachment_size='' background_position='top left' background_repeat='no-repeat' animation='' mobile_display='' av_uid='av-19ydox1']
[av_image src='https://gestudio.dev/wp-content/uploads/2018/07/Laravel_send-email-from-localhost-using-PHP-with-PHPMailer_square_1.png' attachment='4532' attachment_size='full' align='center' styling='no-styling' hover='' link='' target='' caption='' font_size='' appearance='' overlay_opacity='0.4' overlay_color='#000000' overlay_text_color='#ffffff' copyright='' animation='no-animation' av_uid='av-17h6j2d' custom_class='' admin_preview_bg=''][/av_image]
[/av_one_half]

[av_one_half min_height='' vertical_alignment='' space='' custom_margin='' margin='0px' padding='0px' border='' border_color='' radius='0px' background_color='' src='' background_position='top left' background_repeat='no-repeat' animation='' mobile_display='' av_uid='av-491rlx']
[av_image src='https://gestudio.dev/wp-content/uploads/2020/11/scriptwriterph-technical-support-1.png' attachment='5957' attachment_size='full' align='center' styling='no-styling' hover='' link='page,3483' target='' caption='' font_size='' appearance='' overlay_opacity='0.4' overlay_color='#000000' overlay_text_color='#ffffff' copyright='' animation='no-animation' av_uid='av-43pib9' custom_class='' admin_preview_bg=''][/av_image]
[/av_one_half]

[av_textblock size='' font_color='' color='' av-medium-font-size='' av-small-font-size='' av-mini-font-size='' av_uid='av-drmcl' custom_class='' admin_preview_bg='']

Laravel and PHPMailer, Send Email from Localhost with Gmail SMTP

[wpb_last_updated_date]

Here we are having another client request — to add email functionality in their Laravel app — hosted on their localhost server. They want a contact form that’ll be capable of sending email with CC (carbon copy) and BCC (blind carbon copy). They also requested an add file attachment feature in case they need to attach file/s to their email. They don’t have any mail server setup on their local machine.

One of the library we can think of (that can do the job) is PHPMAiler. Well, why not right? PHPMailer is 20 years in business, supporting projects, making their library publicly available for anyone. Additionally, integration between Laravel and PHPMailer isn’t new at all.

If you haven’t tried PHPMailer yet (maybe you’re using Laravel’s Swiftmailer, Zend/Mail, ZetaComponents etc.), here’s a quick background. In a nutshell, PHPMailer is a PHP library for sending email, simply as that, nothing complicated. Client requests we’ve stated above such as sending email with CC, BCC, and add file attachments supports by this library. Most importantly, it’ll allow us to send email from a localhost server (even in Windows platform), through the use of Gmail SMTP of course. We can’t list all features of this library here but you can read more about PHPMailer from this page.
[/av_textblock]

[av_codeblock wrapper_element=” wrapper_element_attributes=” codeblock_type=” av_uid=’av-kldo4zxb’ custom_class=’cust-codeblock-ads1′]




[/av_codeblock]

[av_textblock size=” font_color=” color=” av-medium-font-size=” av-small-font-size=” av-mini-font-size=” av_uid=’av-drmcl’ custom_class=” admin_preview_bg=”]
Before we begin, let’s take a look on what will be our output at the end of this tutorial.

Laravel form that sends email with CC, BCC and attachments from localhost Windows through the use of PHPMailer with Gmail SMTP

A simple contact form in Laravel that sends email with CC, BCC and attachments from a localhost server (Windows 10 specifically) through the use of PHPMailer with Gmail SMTP.

[av_hr class=’invisible’ height=’25’ shadow=’no-shadow’ position=’center’ custom_border=’av-border-thin’ custom_width=’50px’ custom_border_color=” custom_margin_top=’30px’ custom_margin_bottom=’30px’ icon_select=’yes’ custom_icon_color=” icon=’ue808′ font=’entypo-fontello’ av_uid=’av-1zc0tx’ custom_class=” admin_preview_bg=”]

Requirements

  • Composer – we’ll use this dependency management in PHP to easily install Laravel and PHPMailer
  • Your Gmail account, plus your email and password (Important: Your Gmail account’s less secure app access must be turned on)

To turn on less secure app access of your Gmail account:

  1. Go to Gmail and sign in.
  2. Go to Manage your Google Account.Enable less secure app access in gmail - Manage your google account
  3. Go to security tab and scroll down until you see Less secure app access panel.Enable less secure app access in gmail - go to security - turn on access (not recommended)
  4. Click Turn on access (not recommended). Gmail will verify your password.
  5. Now toggle on Allow less secure apps.

Enable less secure app access in gmail

[av_hr class=’invisible’ height=’25’ shadow=’no-shadow’ position=’center’ custom_border=’av-border-thin’ custom_width=’50px’ custom_border_color=” custom_margin_top=’30px’ custom_margin_bottom=’30px’ icon_select=’yes’ custom_icon_color=” icon=’ue808′ font=’entypo-fontello’ av_uid=’av-1zc0tx’ custom_class=” admin_preview_bg=”]

Steps to Integrate Laravel and PHPMailer

[av_hr class=’invisible’ height=’25’ shadow=’no-shadow’ position=’center’ custom_border=’av-border-thin’ custom_width=’50px’ custom_border_color=” custom_margin_top=’30px’ custom_margin_bottom=’30px’ icon_select=’yes’ custom_icon_color=” icon=’ue808′ font=’entypo-fontello’ av_uid=’av-1zc0tx’ custom_class=” admin_preview_bg=”]

Step 1: Install Laravel Using Composer

a. Let’s make sure composer works on your end. Open a command line interface. Type the command composer -v; composer version, options and available commands should be displayed.

Check if composer works correctly using composer command

Type cd command to display your current working directory. Make sure you’re inside the folder where you want to install your project. If not then navigate to your preferred location using cd command, cd phpmailer.

Navigate to your preferred directory using cd command

b. Now create your Laravel project using composer create-project –prefer-dist laravel/laravel command.

The composer will download Laravel files.

Create laravel project using composer create-project --prefer-dist laravel-laravel command

It should end with the message “Application key set successfully”.

Install Laravel - Application key set successfully

[av_hr class=’invisible’ height=’25’ shadow=’no-shadow’ position=’center’ custom_border=’av-border-thin’ custom_width=’50px’ custom_border_color=” custom_margin_top=’30px’ custom_margin_bottom=’30px’ icon_select=’yes’ custom_icon_color=” icon=’ue808′ font=’entypo-fontello’ av_uid=’av-1zc0tx’ custom_class=” admin_preview_bg=”]

Step 2: Install PHPMailer Using Composer

Here we knits Laravel and PHPMailer.

a. Again, type cd command to display your current working directory. You must be inside of your project folder.

Install PHPMailer using composer require phpmailer/phpmailer command.

Install phpmailer using composer require phpmailer-phpmailer command

b. Check whether the PHPMailer exist by navigating to vendor folder. A folder named “phpmailer” must be listed together with other vendor files.

Verify if PHPMailer exist inside vendor folder

c. Start your Laravel project by executing php artisan serve command.

Start laravel project using php artisan serve command

Now open the browser of your choice and go to http://127.0.0.1:8000/. You should see Laravel’s welcome page.
[/av_textblock]

[av_codeblock wrapper_element=” wrapper_element_attributes=” codeblock_type=” av_uid=’av-kldo4zxb’ custom_class=’cust-codeblock-ads1′]




[/av_codeblock]

[av_textblock size=” font_color=” color=” av-medium-font-size=” av-small-font-size=” av-mini-font-size=” av_uid=’av-drmcl’ custom_class=” admin_preview_bg=”]
Laravel's welcome page

d. Run composer install command. It’ll download and install all the libraries and dependencies required.

Generate optimized autoload files using composer install command

[av_hr class=’invisible’ height=’25’ shadow=’no-shadow’ position=’center’ custom_border=’av-border-thin’ custom_width=’50px’ custom_border_color=” custom_margin_top=’30px’ custom_margin_bottom=’30px’ icon_select=’yes’ custom_icon_color=” icon=’ue808′ font=’entypo-fontello’ av_uid=’av-1zc0tx’ custom_class=” admin_preview_bg=”]
[/av_textblock]

[av_textblock size=” font_color=” color=” av-medium-font-size=” av-small-font-size=” av-mini-font-size=” av_uid=’av-c3z5zp’ custom_class=” admin_preview_bg=”]

Step 3: Laravel and PHPMailer Source Code for Sending Email

a. First we have to create our controller, it’ll contain our function for sending email.

Run php artisan make:Controller PhpmailerController command.

Create PHPMailer controller using php artisan make-Controller PhpmailerController

Open PhpmailerController.php located at /your-project-folder/app/Http/Controllers and paste the following code.
[/av_textblock]

[av_codeblock wrapper_element=” wrapper_element_attributes=” codeblock_type=’snippet’ av_uid=’av-kicrim8e’ custom_class=”]
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ‘smtp.gmail.com’; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ‘your-email@gmail.com’; // SMTP username
$mail->Password = ‘your-gmail-password’; // SMTP password
$mail->SMTPSecure = ‘tls’; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to

$mail->setFrom(‘your-email@gmail.com’, ‘Your Name’);
$mail->addAddress($_POST[’email’]); // Add a recipient, Name is optional
$mail->addCC($_POST[’email-cc’]);
$mail->addBCC($_POST[’email-bcc’]);
$mail->addReplyTo(‘your-email@gmail.com’, ‘Your Name’);
// print_r($_FILES[‘file’]); exit;

for ($i=0; $i < count($_FILES[‘file’][‘tmp_name’]) ; $i++) { $mail->addAttachment($_FILES[‘file’][‘tmp_name’][$i], $_FILES[‘file’][‘name’][$i]); // Optional name
}

$mail->isHTML(true); // Set email format to HTML

$mail->Subject = $_POST[‘subject’];
$mail->Body = $_POST[‘message’];
// $mail->AltBody = plain text version of your message;

if( !$mail->send() ) {
echo ‘Message could not be sent.’;
echo ‘Mailer Error: ‘ . $mail->ErrorInfo;
} else {
echo ‘Message has been sent’;
}

} catch (Exception $e) {
// return back()->with(‘error’,’Message could not be sent.’);
}
}
}
}
[/av_codeblock]

[av_textblock size=” font_color=” color=” av-medium-font-size=” av-small-font-size=” av-mini-font-size=” av_uid=’av-c3z5zp’ custom_class=” admin_preview_bg=”]
Replace the following line with your Gmail account’s username and password.

$mail->Username = ‘your-email@gmail.com’;
$mail->Password = ‘your-gmail-password’;

Tips: Your PASSWORD shouldn’t be hardcoded in a controller file. Maybe place those kind of data in .env or much better to store it (but hash it first) in your database.

b. We now have the logic of sending email. Now let’s create our contact form.

Open /resources/views/welcome.blade.php and place the following code below.
[/av_textblock]

[av_codeblock wrapper_element=” wrapper_element_attributes=” codeblock_type=’snippet’ av_uid=’av-kicrim8e’ custom_class=”]

@csrf







[/av_codeblock]

[av_textblock size=” font_color=” color=” av-medium-font-size=” av-small-font-size=” av-mini-font-size=” av_uid=’av-c3z5zp’ custom_class=” admin_preview_bg=”]
c. Now we can setup our routing. Open /routes/web.php and paste the following code at the end.
[/av_textblock]

[av_codeblock wrapper_element=” wrapper_element_attributes=” codeblock_type=’snippet’ av_uid=’av-kicrim8e’ custom_class=”]
Route::post(‘/sendmail’,[
‘as’ => ‘sendmail’,
‘uses’ => ‘AppHttpControllersPhpmailerController@sendEmail’
]);
[/av_codeblock]

[av_textblock size=” font_color=” color=” av-medium-font-size=” av-small-font-size=” av-mini-font-size=” av_uid=’av-c3z5zp’ custom_class=” admin_preview_bg=”]
Save and close the file.

Now go to your browser and navigate to http://127.0.0.1:8000/ and test it out. Does it work? Let me know in the comments below.

We hope this tutorial on Laravel and PHPMailer helped you add email feature to your Laravel app sitting on a localhost environment.

To improve user interface of your form and/or page, you can integrate AdminLTE in your Laravel app. Here we’ll help you create a responsive web app template without starting from scratch.
[/av_textblock]

[av_hr class=’default’ height=’50’ shadow=’no-shadow’ position=’center’ custom_border=’av-border-thin’ custom_width=’50px’ custom_border_color=” custom_margin_top=’30px’ custom_margin_bottom=’30px’ icon_select=’yes’ custom_icon_color=” icon=’ue808′ av-desktop-hide=” av-medium-hide=” av-small-hide=” av-mini-hide=” av_uid=’av-5qemud’]

[av_social_share title=’SHARE THIS POST’ style=’minimal’ buttons=” av_uid=’av-ht5jp’ custom_class=” admin_preview_bg=”]

[av_hr class=’invisible’ height=’50’ shadow=’no-shadow’ position=’center’ custom_border=’av-border-thin’ custom_width=’50px’ custom_border_color=” custom_margin_top=’30px’ custom_margin_bottom=’30px’ icon_select=’yes’ custom_icon_color=” icon=’ue808′ font=’entypo-fontello’ av_uid=’av-kicso8st’ custom_class=” admin_preview_bg=”]

[av_comments_list av-desktop-hide=” av-medium-hide=” av-small-hide=” av-mini-hide=” av_uid=’av-1whkk5′]