Skip to content

Commit

Permalink
feat: done
Browse files Browse the repository at this point in the history
  • Loading branch information
itsrafsanjani committed May 7, 2022
0 parents commit b802da4
Show file tree
Hide file tree
Showing 14 changed files with 376 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.idea
/.vscode
/tools
.php-cs-fixer.cache
.php-cs-fixer.php
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
![preview image](https://raw.githubusercontent.com/itsrafsanjani/contact/master/preview.png)

# Contact

[![Latest Stable Version](https://poser.pugx.org/itsrafsanjani/contact/v/stable)](https://packagist.org/packages/itsrafsanjani/contact)
[![Total Downloads](https://poser.pugx.org/itsrafsanjani/contact/downloads)](https://packagist.org/packages/itsrafsanjani/contact)
[![License](https://poser.pugx.org/itsrafsanjani/contact/license)](https://packagist.org/packages/itsrafsanjani/contact)

A Laravel Package to create Contact Us Form Easily

## Features
- Send Email
- Save Message to Database

# Requirements
Laravel 5.7+
Tested on Laravel 8

## Installation Steps

### 1. Require the Package

Run the following command:

```bash
composer require itsrafsanjani/contact
```

### 2. Add the service provider to your config/app.php providers array:

> If you're installing on Laravel 5.5+ skip this step
```bash
ItsRafsanJani\Contact\ContactServiceProvider::class,
```

### 3. Run Migration

Run the following command:

```bash
php artisan migrate
```

### 4. Edit `.env` File

Add variable `CONTACT_TO_MAIL` to `.env` and then define the email to receive email

For example

```bash
CONTACT_TO_MAIL="[email protected]"
CONTACT_PHONE_REQUIRED=false
```
***Finally*** you can access your contact page by access `http://your-site.com/contact` to show the result


## Customize Your Form

If you want to create your own contact us page, you must follow below instructions:

- Form action = `{{ route('contact.submit') }}`
- Name field = input `name="name"`
- Email field = input `name="email"`
- Message field = textarea `name="message"`

> Don't forget to add `{{ csrf_field() }}`
You can create the contact page with your own Route and/or Controller

## Bugs and Issues

If you found bugs or issues just write to [Issues](https://github.com/itsrafsanjani/contact/issues)
27 changes: 27 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "itsrafsanjani/contact",
"description": "Laravel contact us form package to send email and save to database",
"keywords": ["laravel", "contact", "contact form", "contact us", "form", "email", "itsrafsanjani", "contact"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Md Rafsan Jani Rafin",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": {
"ItsRafsanJani\\Contact\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"ItsRafsanJani\\Contact\\ContactServiceProvider"
]
}
}
}
Binary file added preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions src/ContactServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace ItsRafsanJani\Contact;

use Illuminate\Support\ServiceProvider;

class ContactServiceProvider extends ServiceProvider
{
public function boot()
{
$this->loadRoutesFrom(__DIR__ . '/routes/contact.php');
$this->loadViewsFrom(__DIR__ . '/views', 'contact');
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
$this->mergeConfigFrom(__DIR__ . '/config/contact.php', 'contact');
}

public function register()
{
//
}
}
31 changes: 31 additions & 0 deletions src/Http/Controllers/ContactController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace ItsRafsanJani\Contact\Http\Controllers;

use App\Http\Controllers\Controller;
use ItsRafsanJani\Contact\Models\Contact;
use ItsRafsanJani\Contact\Requests\ContactRequest;
use Illuminate\Support\Facades\Mail;
use ItsRafsanJani\Contact\Mail\ContactMailable;

class ContactController extends Controller
{
public function showForm()
{
return view('contact::contact_form');
}

public function send(ContactRequest $request)
{
$message['name'] = $request->name;
$message['phone'] = $request->phone;
$message['email'] = $request->email;
$message['message'] = $request->message;

Contact::create($message);

Mail::to(config('contact.mail_contact_to'))->send(new ContactMailable($message));

return back()->withSuccess('Message Sent!');
}
}
37 changes: 37 additions & 0 deletions src/Mail/ContactMailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace ItsRafsanJani\Contact\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ContactMailable extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;

protected $content;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct($content)
{
$this->content = $content;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject("Email from " . $this->content['name'])
->replyTo($this->content['email'])
->markdown('contact::contact.email')->with('content', $this->content);
}
}
12 changes: 12 additions & 0 deletions src/Models/Contact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace ItsRafsanJani\Contact\Models;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
protected $fillable = [
'name', 'phone', 'email', 'message'
];
}
33 changes: 33 additions & 0 deletions src/Requests/ContactRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace ItsRafsanJani\Contact\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ContactRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => ['required', 'max:255'],
'phone' => [config('contact.is_phone_required') ? 'required' : 'sometimes', 'max:15'],
'email' => ['required', 'email', 'max:255'],
'message' => ['required'],
];
}
}
6 changes: 6 additions & 0 deletions src/config/contact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'mail_contact_to' => env('CONTACT_TO_MAIL', '[email protected]'),
'is_phone_required' => env('CONTACT_PHONE_REQUIRED', false),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateContactsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('phone', 15)->nullable();
$table->string('email');
$table->text('message');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contacts');
}
}
6 changes: 6 additions & 0 deletions src/routes/contact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

Route::group(['namespace' => 'ItsRafsanJani\Contact\Http\Controllers', 'middleware' => 'web'], function () {
Route::get('contact', 'ContactController@showForm')->name('contact.form');
Route::post('contact-submit', 'ContactController@send')->name('contact.submit');
});
11 changes: 11 additions & 0 deletions src/views/contact/email.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@component('mail::message')
# {{ $content['name'] }} send you a message

**Email :**

{{ $content['email'] }}

**Message :**

{{ $content['message'] }}
@endcomponent
79 changes: 79 additions & 0 deletions src/views/contact_form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>Contact Us</title>
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>

<body>
<div class="container">
<div class="row justify-content-center p-2">
<div class="col-md-8">
@if (session('success'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{ session('success') }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
@endif
<div class="card">
<div class="card-header">Contact Us</div>

<div class="card-body">
<form method="POST" action="{{ route('contact.submit') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" value="{{ old('name') }}" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" id="name" placeholder="Your Name">
@if ($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
@if(config('contact.is_phone_required'))
<div class="form-group">
<label for="phone">Phone</label>
<input type="tel" name="phone" value="{{ old('phone') }}" class="form-control{{ $errors->has('phone') ? ' is-invalid' : '' }}" id="phone" placeholder="Your Phone">
@if ($errors->has('phone'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('phone') }}</strong>
</span>
@endif
</div>
@endif
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" value="{{ old('email') }}" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" id="email" aria-describedby="emailHelp" placeholder="Your Email">
@if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control{{ $errors->has('message') ? ' is-invalid' : '' }}" name="message" id="message" rows="10">{{ old('message') }}</textarea>
@if ($errors->has('message'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('message') }}</strong>
</span>
@endif
</div>
<button type="submit" class="btn btn-primary">Send</button>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
<script src="{{ asset('js/app.js') }}"></script>
</html>

0 comments on commit b802da4

Please sign in to comment.