How to add PHPMailer in Laravel?

by jordane.crist , in category: PHP , 2 years ago

How to add PHPMailer in Laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by aboutthenerd , 2 years ago

@jordane.crist you can send using the laravel PHPMailer package composer require phpmailer/phpmailer and use the following code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php

//Required Data
$sendTo = "myuser@example.com";
$subject = 'New EMAIL !';
$body = "Hello World ! My Email.";
$isHTML = false;

try {
    //Creating PHPMailer instance
    $mail = new PHPMailer(true);// Passing `true` enables exceptions


    // Settings
    $mail->IsSMTP();
    $mail->CharSet = 'UTF-8';
    $mail->Host = env('SMTP_HOST', "");
    $mail->SMTPDebug = 0;
    $mail->SMTPAuth = true;
    $mail->Port = env('SMTP_PORT', "");
    $mail->Username = env('SMTP_USERNAME', "");
    $mail->Password = env('SMTP_PASS', "");
    $mail->setFrom(env('SMTP_USERNAME', ""), "MY EMAIL");
    $mail->addAddress($sendTo);
    $mail->SMTPSecure = 'ssl';

    // Content
    $mail->isHTML($isHTML);
    $mail->Subject = $subject;
    $mail->Body = $body;

    // $mail->AltBody = plain text version of email body;

    if (!$mail->send()) {
        return back()->with("failed", "Email not sent.")->withErrors($mail->ErrorInfo);
    } else {
        return back()->with("success", "Email has been sent.");
    }

} catch (Exception $e) {
    return back()->with('error', 'Message could not be sent.');
}

Member

by freddy , a year ago

@jordane.crist 

Related Threads:

How to add CC in PHPMailer?
How to add BCC in PHPMailer?
How to add an image to PHPMailer?
How to add PHPMailer to WordPress?
How to add CKEditor in Laravel?