How to send email in Laravel using HTML template.

Emails Using Laravel Mail:
Laravel provides a simple way to send email.

Configure Settings For Email :
Go to app/config/mail.php to configure the settings for sending emails. There are three different email drivers: smtp (default), PHP mail function and sendmail driver.Let’s start with  mail driver.

‘driver’ = ‘smtp’,

Default driveris smtp, we need to change it to PHP mail funciton:

‘driver’ = ‘mail’.

from = array(‘address’ = null, ‘name’ = null),

Change to:

‘from’ = array(‘address’ = ‘app@awesomeapp.com’, ‘name’ = ‘Awesome Laravel 4’),

Save it. Now you should able to send emails using PHP built in mail driver!

Email With Gmails:

Have a gmail account, great !! you can use it to send email. Open your app/config/mail.php. This time use smtp driver:

<?php

return array(

    'driver' = 'smtp',

    'host' = 'smtp.gmail.com',

    'port' = 587,

    'from' = array('address' = 'app@awesomeapp.com', 'name' = 'Awesome Laravel 4'),

    'encryption' = 'tls',

    'username' = 'your gmail id',

    'password' = 'your gmail password',

    'sendmail' =  '/usr/sbin/sendmail -bs',

    'pretend' = false,
);
?>

Great job!

HTML Template:

Lets create data for emails. Use HTML template for emails. Laravel
has a folder for this purpose. Go to app/views/emails/auth/reminder.blade.php. Below is the content of the file.

<!DOCTYPE html>
<html lang=&quot;en-US&quot;>
	<head>
	<meta charset=&quot;utf-8&quot;>
	</head>
	<body>
<h2>Password Reset</h2>
<div>
Reset your password, complete this form: {{ URL::to('password/reset', array($token)) }}.
This link will expire in {{ Config::get('auth.reminder.expire', 120) }} minutes.</div>
</body>
</html>

Above is a password reset HTML email template, provided by Laravel. We are dealing with
contact emails, sl create a new template. Name it ContactEmail.blade.php, place it in app/view/emails folder. content:

<!DOCTYPE html>
<html lang=&quot;en-US&quot;>
<head>
<meta charset=&quot;utf-8&quot;>
</head>
<body>
<h2>Contact Form</h2>
<div>
Someone just contacted us:</div>
<div> Subject: {{ $subject }}</div>
<div> Message: {{ $emailmessage }}</div>
</body>
</html>

we create a basic HTML template, and then we use {{ $subject }} and {{ $emailmessage
}} to output mail subject and mail message.

First Email:

As soon as user submit the form, Laravel will check the form input, if validation satisfies, the template will be sent out to our email address. Lets back to our app/routes.php file and use Mail::send function to send emails:

Route::get('/contact', function()
{
return View::make('contact');
});

Route::post('contact', function()
{
$data1 = Input::all();
$rules = array(
'subject' => 'required',
'message' => 'required'
);
$validator = Validator::make($data1, $rules);
if($validator->fails()) {
return Redirect::to('contact')->withErrors($validator)->withInput();
}
$emailcontent = array (
'subject' => $data1['subject'],
'emailmessage' => $data1['message']
);
Mail::send('emails.contactemail', $emailcontent, function($message)
{
$message->to('arjita.mitra@yahoo.com','Learning Laravel Support')
->subject('Contact using Our Contact Form');
});

return 'Message sent!!';
});

In my last blog I already explained- ” How to create a blade template for contact page in laravel. ”

Please visit the link below-

How to create a Blade template for contact page in Laravel.

Let me explain the above code for better understanding:

$emailcontent = array (
'subject' = $data1['subject'],
'emailmessage' = $data1['message']
);

We created an array called $emailcontent, array contains subject and emailmessage from the
form.

Mail::send('emails.contactemail', $emailcontent, function($message)
{
$message->to('arjita.mitra@yahoo.com','Learning Laravel Support')
->subject('Contact using Our Contact Form');
});

After that, we use Mail::send Laravel method to send mails. The first parameter is our con-
tactemail.blade.php and the emails folder where the file is located.

The second parameter is our mail content.

The third parameter is a function helps to send our email. you need to fill in where the mail will be sent to, name of the receiver and the subject of our email.

If everything goes ok, go to our contact page, fill in the contact form.

contactpageAfter that hit the submit button, we will get the message “Your message has been sent”!

oow! Fantastic . Now, let’s check your email inbox. you should see:

yahoomailIf you face any issue like –

  • Laravel Connection could not be established with host smtp.gmail.com.
  • Expected response code 250 but got code “535”, with message 535-5.7.8 Username and Password not accepted.
  • Failed to authenticate on SMTP server with username “example@yahoo.com” using 2 possible authenticators.

one solution is to click the below link –

https://www.google.com/settings/security/lesssecureapps

click on enable and save it
Then try senidng email again.
It may work.

Hope this post was helpful.

How to create a Blade template for contact page in Laravel.

Laravel provides multiple shortcuts for creating forms. Labels, buttons, text inputs and everything we need to make forms are easy to create. Still we can use normal HTML
to make forms, but it’s easier to do using Laravel method.

Let’s make a new file
named contactmail.blade.php in our app/views directory.

Then place the below code to create a simple contact form –

@extends('layout')
@section('content')
<h1>Contact Us.</h1>
Send a message using the form below:

{{ Form::open(array('url' => 'contactmail')) }}
{{ Form::label('Subject') }}
{{ Form::text('subject','Enter your subject') }}

{{ Form::label('Message') }}
{{ Form::textarea('message','Enter your message') }}

{{ Form::submit() }}
{{ Form::close() }}
@stop

@extends(‘layout’)
This tells Blade which layout we will be using to render our content. In this situation I am referring to the ‘layout.blade.php’ file within ‘app/views’ directory.

The content of ‘layout.blade.php’ is-

<html lang=&quot;en&quot;>
<head>
<meta charset=&quot;UTF-8&quot;>
<title>Learning Laravel</title>
</head>
<body>
<ul>
	<li><a href=&quot;./contact&quot;>Contact</a></li>
</ul>
@yield('content')
</body>
</html>

We use @yield(‘content’) to tell Blade create a section here, we can fill the content later. Like what we have done in contactmail.blade.php.

Finally update your routes.php file inside app directory.

Route::get('/contact', function()
{
return View::make('contact');
});

When we put View::make(‘contact’), it will render contact.blade.php within
our ‘app/views’ directory.

To run your app you can start artisan from your terminal using the command –

php artisan serve

Then it will show – Laravel development server started on http://localhost:8000

copy the link paste in your browser you should able to see contact link . on click of that you can see contact form.