| Server IP : 139.59.63.204 / Your IP : 216.73.217.62 Web Server : Apache/2.4.58 (Ubuntu) System : Linux ubuntu-s-1vcpu-1gb-blr1-01 6.8.0-110-generic #110-Ubuntu SMP PREEMPT_DYNAMIC Thu Mar 19 15:09:20 UTC 2026 x86_64 User : root ( 0) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/customerportal/app/Services/ |
Upload File : |
<?php
namespace App\Services;
use Google\Client as GoogleClient;
use Google\Service\Gmail as GoogleServiceGmail;
use Google\Service\Gmail\Message as GoogleGmailMessage;
use Illuminate\Support\Facades\View;
class GmailService
{
protected $client;
protected $service;
public function __construct()
{
$this->client = new GoogleClient();
$this->client->setAuthConfig(storage_path('app/credentials.json'));
$this->client->addScope("https://www.googleapis.com/auth/gmail.send");
$tokenPath = storage_path('app/token.json');
if (!file_exists($tokenPath)) {
throw new \Exception("OAuth token not found. Authenticate first.");
}
$token = json_decode(file_get_contents($tokenPath), true);
$this->client->setAccessToken($token);
if ($this->client->isAccessTokenExpired()) {
$this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken());
file_put_contents($tokenPath, json_encode($this->client->getAccessToken()));
}
$this->service = new GoogleServiceGmail($this->client);
}
/**
* Send email using Gmail OAuth
*
* @param string $to
* @param string $subject
* @param string $view
* @param array $data
*/
public function send(string $to, string $subject, string $view, array $data = [])
{
$html = View::make($view, $data)->render();
$raw = "From: you@gmail.com\r\n";
$raw .= "To: $to\r\n";
$raw .= "Subject: $subject\r\n";
$raw .= "Content-Type: text/html; charset=UTF-8\r\n\r\n";
$raw .= $html;
$message = new GoogleGmailMessage();
$message->setRaw(base64_encode($raw));
$this->service->users_messages->send("me", $message);
}
}