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.

Revisions

  1. tha-ninja revised this gist Apr 25, 2019. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions MailController.php
    Original file line number Diff line number Diff line change
    @@ -61,6 +61,18 @@ public function test_mail()

    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();

    }

    }
  2. tha-ninja created this gist Apr 25, 2019.
    66 changes: 66 additions & 0 deletions MailController.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    <?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);

    }

    }