Skip to content

Instantly share code, notes, and snippets.

@tha-ninja
Last active April 25, 2019 07:47
Show Gist options
  • Select an option

  • Save tha-ninja/6bb9372288f3657d784c5d548686cc21 to your computer and use it in GitHub Desktop.

Select an option

Save tha-ninja/6bb9372288f3657d784c5d548686cc21 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PHPMailer\PHPMailer\PHPMailer;
class MailController extends Controller
{
//
public function send($recepient, $subject, $email_body, $attachments = []){
try {
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPKeepAlive = true;
$mail->SMTPDebug = false;
$mail->Timeout = 20; // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'Email Address here'; // SMTP username
$mail->Password = 'Password Here'; // SMTP password
$mail->SMTPSecure = "ssl"; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('Email Address here', 'Mobifin');
$mail->addAddress($recepient['email'], $recepient['name']);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $email_body;
if(!$mail->send()) {
$email_error = $mail->ErrorInfo;
return [
'status' => FALSE,
'message' => $email_error
];
} else {
return true;
}
$mail->SmtpClose();
} catch (\Exception $e) {
return [
'status' => FALSE,
'message' => $e->getMessage()
];
}
}
public function test_mail()
{
$recepient['name'] = 'test';
$recepient['email'] = 'your email address here';
$subject = 'test larvel';
$message = 'laravel mailer';
$mail_sent = $this->send($recepient, $subject, $message);
dd($mail_sent);
}
public function send_mail(Request $request)
{
$recepient['name'] = $request->name;
$recepient['email'] = $request->email;
$subject = 'Contact form';
$message = $request->message;
$mail_sent = $this->send($recepient, $subject, $message);
return back();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment