DEV Community

Cover image for Laravel sendinblue emails
Chetan Rohilla
Chetan Rohilla

Posted on • Edited on • Originally published at w3courses.org

Laravel sendinblue emails

Hi! In this post i will share small code with you, how you can send Laravel Sendinblue emails. We will send emails using the CURL options provided by sendinblue.

public static function sendSendInBlueMail()
{
    $html = view('emails.template', [
        'name' => 'User Name'
    ]);

    $fields = [
        'to' => [
            [
                "email" => 'to@example.com',
                "name" => 'User Name'
            ]
        ],
        "sender" => [
            "name" => 'Website Name',
            "email" => 'from@example.com'
        ],
        "subject" => 'Sendinblue Email',
        "cc" => [
            [
                "email" => 'to@example.com',
                "name" => 'User Name'
            ]
        ],
        "htmlContent" => "<html><head></head>" . $html . "</html>"
    ];

    $fields = json_encode($fields);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.sendinblue.com/v3/smtp/email');
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'accept: application/json',
        'api-key:' . env("SENDINBLUEAPIKEY") . '', 'content-type: application/json'
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $response = curl_exec($ch);

    return $response;
}
Enter fullscreen mode Exit fullscreen mode

Email blade template

<body style="padding:0px;margin:0px;font-family: sans-serif;">
    <h1>{{$name}}</h1>
</body>
Enter fullscreen mode Exit fullscreen mode

Get Sendinblue API here

Please like share and give positive feedback to motivate me to write more.

For more tutorials visit my website.

Thanks:)
Happy Coding:)

Top comments (0)