<?php
class EmailController extends \BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
return View::make('emails.create');
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$user_id = Auth::id();
$from_address = Auth::user()->email;
$to_address = Input::get('to_address');
$title = Input::get('title');
$message = Input::get('message');
$valid = Validator::make(array($to_address, $title, $message), SentEmail::$rules);
if($valid->fails())
{
return Redirect::route('emails/create')
->withInput(Input::all())
->withErrors($valid);
}else
{
$recepient = User::where('email', '=', $to_address)->get();
$response = Mail::queue('emails.email', array('name'=> $recepient->first), function($message) use ($recepient)
{
$message->to($to_address, $recepient->first . ' '. $recepient->last)->subject($title);
});
if(!$response)
{
Session::flash('message', 'We were unable to send your email. Please try again later');
return Redirect::route('emails.create')->withInput(Input::all());
}
$sentEmail = new SentEmail;
$sentEmail->user_id = $user_id;
$sentEmail->from_address = $from_address;
$sentEmail->to_address = $to_address;
$sentEmail->title = $title;
$sentEmail->message = $message;
if($sentEmail->save())
{
return Redirect::route('user');
}else
{
Session::flash('message', 'Something bad happened.')
return Redirect::route('emails/create')
->withInput(Input::all());
}
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$email = SentEmail::find($id);
return View::make('emails.show')->with('email', $email);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//cannot edit emails
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
//cannot update emails
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
$email = SentEmail::find($id);
$email->delete();
}
}