The Google Mail service adapter provides a fluent interface for sending emails through Gmail API with support for attachments, CC, BCC, and Laravel Mailables.
First, ensure you have authorized your application with Gmail scopes in your config/google-api.php:
'service_scopes' => [
Google\Service\Gmail::GMAIL_SEND,
],use TomShaw\GoogleApi\Facades\GoogleApi;
$gmail = GoogleApi::gmail();Sets both the sender's email and name.
$gmail->from('sender@example.com', 'John Doe');Sets only the sender's email address.
$gmail->setFromEmail('sender@example.com');Sets only the sender's name.
$gmail->setFromName('John Doe');Sets both the recipient's email and name.
$gmail->to('recipient@example.com', 'Jane Smith');Sets only the recipient's email address.
$gmail->setToEmail('recipient@example.com');Sets only the recipient's name.
$gmail->setToName('Jane Smith');Adds carbon copy recipient(s).
// Single recipient
$gmail->cc('cc@example.com');
// Multiple recipients
$gmail->cc(['cc1@example.com', 'cc2@example.com']);Adds blind carbon copy recipient(s).
// Single recipient
$gmail->bcc('bcc@example.com');
// Multiple recipients
$gmail->bcc(['bcc1@example.com', 'bcc2@example.com']);Sets the email subject.
$gmail->subject('Welcome to our service');Sets the email body content (supports HTML).
$gmail->message('<h1>Welcome!</h1><p>Thank you for signing up.</p>');Sets the message content using a Laravel Mailable.
use App\Mail\OrderConfirmation;
$gmail->mailable(new OrderConfirmation($order));Adds a single file attachment.
$gmail->attachment(storage_path('app/public/invoice.pdf'));Adds multiple file attachments.
$attachments = [
storage_path('app/public/invoice.pdf'),
storage_path('app/public/receipt.pdf'),
];
$gmail->attachments($attachments);Note: Maximum total attachment size is 25MB (Gmail limit).
Sends the email and returns the message object.
Returns: Google\Service\Gmail\Message
$message = $gmail->send();use TomShaw\GoogleApi\Facades\GoogleApi;
$gmail = GoogleApi::gmail();
$gmail->from('sender@example.com', 'Company Name')
->to('customer@example.com', 'Customer Name')
->subject('Order Confirmation')
->message('<h1>Thank you for your order!</h1>')
->send();use TomShaw\GoogleApi\Facades\GoogleApi;
use App\Mail\OrderMailable;
$order = Order::find($orderId);
$gmail = GoogleApi::gmail();
$gmail->from('sales@example.com', 'Sales Team');
$gmail->to($order->user->email, $order->user->name);
$gmail->cc('manager@example.com');
$gmail->subject('Your Order #' . $order->id);
$gmail->mailable(new OrderMailable($order));
$gmail->send();use TomShaw\GoogleApi\Facades\GoogleApi;
$attachments = [
storage_path('app/invoices/invoice-123.pdf'),
storage_path('app/receipts/receipt-123.pdf'),
];
$gmail = GoogleApi::gmail();
$gmail->from('billing@example.com', 'Billing Department');
$gmail->to('customer@example.com', 'Customer Name');
$gmail->subject('Your Invoice and Receipt');
$gmail->message('<p>Please find attached your invoice and receipt.</p>');
$gmail->attachments($attachments);
$gmail->send();use TomShaw\GoogleApi\Facades\GoogleApi;
use TomShaw\GoogleApi\Exceptions\GoogleApiException;
use App\Mail\WelcomeEmail;
class EmailController extends Controller
{
public function sendWelcome(User $user)
{
try {
$gmail = GoogleApi::gmail();
$gmail->from(config('mail.from.address'), config('mail.from.name'))
->to($user->email, $user->name)
->bcc('admin@example.com')
->subject('Welcome to ' . config('app.name'))
->mailable(new WelcomeEmail($user))
->send();
return response()->json(['message' => 'Email sent successfully']);
} catch (GoogleApiException $e) {
return response()->json(['error' => $e->getMessage()], 422);
} catch (\Exception $e) {
return response()->json(['error' => 'Failed to send email'], 500);
}
}
public function sendInvoiceEmail(Order $order)
{
$attachments = [
storage_path("app/invoices/invoice-{$order->id}.pdf"),
];
try {
$gmail = GoogleApi::gmail();
$gmail->from('billing@example.com', 'Billing Department')
->to($order->user->email, $order->user->name)
->cc('sales@example.com')
->subject("Invoice for Order #{$order->id}")
->message($this->buildInvoiceEmailBody($order))
->attachments($attachments)
->send();
$order->update(['invoice_sent_at' => now()]);
return response()->json(['message' => 'Invoice sent']);
} catch (\Exception $e) {
return response()->json(['error' => $e->getMessage()], 500);
}
}
private function buildInvoiceEmailBody(Order $order): string
{
return "
<h2>Invoice for Order #{$order->id}</h2>
<p>Dear {$order->user->name},</p>
<p>Please find attached your invoice for order #{$order->id}.</p>
<p>Total Amount: \${$order->total}</p>
<p>Thank you for your business!</p>
";
}
}The service automatically validates:
- Required fields (from, to, subject, message)
- Email address formats
- CC and BCC email formats
- Attachment file existence and readability
- Total attachment size (25MB limit)
Validation errors throw GoogleApiException with descriptive messages.
- All emails are sent as HTML by default
- The sender email must be authorized in your Google account
- Attachments are automatically base64 encoded
- Multiple CC and BCC recipients are supported
- Laravel Mailables are rendered before sending