Laravel. Mail Without Views

Here is a little helper class that allows to easily send email with Laravel without using views.

 

class EmailHelper{

    /**
     * Sends an email
     *
     * EmailHelper::sendMail(array(
     *      'From' => 'sender@email.com',
     *      'To' => 'recipient@email.com',
     *      'Cc' => 'cc@email.com',
     *      'Bcc' => 'bcc@email.com',
     *      'Text' => 'Here is the text part',
     *      'Html' => '

Here is the HTML part

',
     *      'Subject' => 'This is a test email at ' . date('Y-m-d H:i:s'),
     * ));
     *

     * @return boolean
     */
    public static function sendMail($parameters, $debug = false) {
        \Mail::send([], [], function ($message) use ($parameters) {
            if (isset($parameters['From']) AND $parameters['From'] != "") {
                $message->from($parameters['From']);
            }
            if (isset($parameters['From']) AND $parameters['From'] != "") {
                $message->sender($parameters['From']);
            }
            if (isset($parameters['To']) AND $parameters['To'] != "") {
                $message->to($parameters['To']);
            }
            if (isset($parameters['Cc']) AND $parameters['Cc'] != "") {
                $message->cc($parameters['Cc']);
            }
            if (isset($parameters['Bcc']) AND $parameters['Bcc'] != "") {
                $message->bcc($parameters['Bcc']);
            }
            if (isset($parameters['From']) AND $parameters['From'] != "") {
                $message->replyTo($parameters['From']);
            }
            if (isset($parameters['Subject']) AND $parameters['Subject'] != "") {
                $message->subject($parameters['Subject']);
            }
            if (isset($parameters['Html']) AND $parameters['Html'] != "") {
                $message->setBody($parameters['Html'], 'text/html');
            }
            if (isset($parameters['Text']) AND $parameters['Text'] != "") {
                $message->addPart($parameters['Text'], 'text/plain');
            }
        });

 

        if (count(\Mail::failures()) > 0) {
            return false;
        }
        
        return true;

}

Please Do Not Reprint This Article

This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form. Links to this article are welcome

Other Related Articles