File: D:/HostingSpaces/SBogers10/ridderstee.komma.pro/app/Komma/Mailers/MailService.php
<?php
namespace Komma\Mailers;
use Illuminate\Support\Facades\Schema;
use Komma\Mailers\Models\FormData;
use Komma\Mailers\Models\Mail;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class MailService extends Mailer
{
/**
* This method will send a form to the site admin
* after an user filled in the contact form
*
* @param $input
* @return bool
*/
public function sendContactForm($input)
{
$view = 'emails.contactForm';
//Get the to addres from the config
$to = \Config::get('mail.sendTo.address');
//Set the subject
$subject = \Lang::get('mail.contact.subject');
//Set the email addres as replyTo
$input['replyTo'] = $input['email'];
//Send the email
$this->sendTo($to, $subject, $view, $input);
return true;
}
/**
* This method will send a form to the site admin
* after an user filled in the contact form
*
* @param $input
* @return bool
*/
public function sendClientMail($input)
{
$view = 'emails.clientMail';
//Get the to addres from the config
$to = $input['email'];
//Set the subject
$subject = \Lang::get('mail.contact.clientMail');
//Send the email
$this->sendTo($to, $subject, $view, $input);
return true;
}
/**
* This method will send a form to the site admin
* after an user filled in the contact form
*
* @param $input
* @return bool
*/
public function sendNewsletterForm($input)
{
$view = 'emails.newsletter';
//Get the to addres from the config
$to = \Config::get('mail.sendTo.address');
//Set the subject
$subject = \Lang::get('mail.newsletter.subject');
//Send the email
$this->sendTo($to, $subject, $view, $input);
return true;
}
/**
* This method will send a the lead of the previous week
*
* @param $excel
* @return bool
*/
public function sendMailWithLeads($file, $date)
{
$view = 'emails.leads';
//Get the to addres from the config
$to = \Config::get('mail.sendTo.address');
// $to = 'pascal@komma.pro';
//Set the subject
$subject = 'Ridderstee - Leads week '.$date->format('W') .'-'.$date->year;
//Send the email
$this->sendToWithAttachment($to, $subject, $view, ['date' => $date], [], $file);
return true;
}
/**
* This method will send a the mail address of the previous month
*
* @param $excel
* @return bool
*/
public function sendMailWithMailAddresses($file, $date)
{
$view = 'emails.mailaddresses';
//Get the to addres from the config
$to = \Config::get('mail.sendTo.address');
// $to = 'pascal@komma.pro';
//Set the subject
$subject = 'Ridderstee - Mailadressen '. $date->month .'-' . $date->year;
//Send the email
$this->sendToWithAttachment($to, $subject, $view, ['date' => $date], [], $file);
return true;
}
public function saveMailToDatabase($data, $name = 'contact-form'){
array_forget($data, '_token');
array_forget($data, 'secretCode');
$data['language'] = \App::getLocale();
//check if all keys of data is in mail table else add them
foreach ($data as $key => $value){
if(!Schema::hasColumn('form_data', $key)){
Schema::table('form_data', function ($table) use ($key){
$table->text($key)->nullable();
});
}
}
//save new mail to DB
$mail = new FormData();
$mail->ip = \Request::ip();
$mail->browser = $_SERVER['HTTP_USER_AGENT'];
$mail->form_name = $name;
foreach ($data as $key => $value){
$mail->$key = $value;
}
$mail->save();
}
public function sendLeads()
{
$date = Carbon::today();
$heading = [
'ID nr.',
'Datum aanvraag',
'Weeknummer',
'Accountmanager',
'Geslacht',
'Aanhef 1',
'Aanhef 2',
'Voornaam',
'Achternaam',
'Mail 1 wenst wel nieuwsbrief',
'Mail 2 wenst geen nieuwsbrief',
'Adres',
'Postcode',
'Woonplaats',
'Land',
'Telefoon',
'Bron',
];
$data = $this->getLeadForMail($date, $heading);
$excel = \Excel::create($date->format('W'). '-' . $date->year . '-leadlist', function ($excel) use ($data) {
$excel->sheet('mySheet', function ($sheet) use ($data) {
$sheet->fromArray($data);
$sheet->row(1, function ($row){
$row->setFontWeight('bold');
});
});
})->store('xlsx');
$storagePath = storage_path('exports/'.$excel->filename.'.xlsx');
// dde($storagePath);
$this->sendMailWithLeads($storagePath, $date);
}
public function sendNewsLetterAddress(){
$date = Carbon::today();
$heading = [
'Datum',
'Email',
'Taal',
];
$data = $this->getMailAddressesForMail($date, $heading);
$excel = \Excel::create($date->month. '-' . $date->year . '-mailing', function ($excel) use ($data) {
$excel->sheet('mySheet', function ($sheet) use ($data) {
$sheet->fromArray($data);
$sheet->row(1, function ($row){
$row->setFontWeight('bold');
});
});
})->store('xlsx');
$storagePath = storage_path('exports/'.$excel->filename.'.xlsx');
// dde($storagePath);
$this->sendMailWithMailAddresses($storagePath, $date);
}
private function getLeadForMail($date)
{
$leads = DB::table('form_data')
->where('form_name', '=', 'newsletter')
->where('created_at', '>=', $date->subWeek()->toDateTimeString())
->get();
$formattedLeads = [];
foreach ($leads as $lead)
{
$name = explode(' ', $lead->name, 2);
if(!isset($name[1])){
$name[1] = $name[0];
$name[0] = '';
}
$createdDate = Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $lead->created_at);
if(isset($lead->acceptMailing) && $lead->acceptMailing == 'on' ){
$mail1 = $lead->email;
$mail2 = '';
}
else{
$mail1 = '';
$mail2 = $lead->email;
}
$formattedLeads[] = [
'ID nr.' => '',
'Datum aanvraag' => $createdDate->format('d-m-Y'),
'Weeknummer' => $createdDate->format('W'),
'Accountmanager' => '',
'Geslacht' => '',
'Aanhef 1' => '',
'Aanhef 2' => '',
'Voornaam' => $name[0],
'Achternaam' => $name[1],
'Mail 1 wenst wel nieuwsbrief' => $mail1,
'Mail 2 wenst geen nieuwsbrief' => $mail2,
'Adres' => '',
'Postcode' => '',
'Woonplaats' => '',
'Land' => '',
'Telefoon' => $lead->phone,
'Bron' => $lead->foundBy.$lead->foundByAlternative,
];
}
return $formattedLeads;
}
private function getMailAddressesForMail($date){
$leads = DB::table('form_data')
->where('form_name', '=', 'newsletter')
->where('acceptMailing', '=', 'on')
->where('created_at', '>=', $date->subMonth()->toDateTimeString())
->orderBy('language')
->orderBy('created_at')
->get();
$formattedLeads = [];
foreach ($leads as $lead)
{
$createdDate = Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $lead->created_at);
$formattedLeads[] = [
'Datum' => $createdDate->format('d-m-Y'),
'Email' => $lead->email,
'Taal' => $lead->language,
];
}
return $formattedLeads;
}
}