How to Send Email with PHPMailer (Gmail SMTP)

Sending email from a PHP application is a common requirement — for registration confirmations, password resets, order notifications, and much more. PHP has a built-in mail() functi...

How to Send Email with PHPMailer (Gmail SMTP)
Advertisement

Sending email from a PHP application is a common requirement — for registration confirmations, password resets, order notifications, and much more. PHP has a built-in mail() function, but it is very limited and often breaks with modern server configurations. PHPMailer is a popular library that makes it easy to send email through SMTP, with support for authentication, TLS/SSL, attachments, and HTML email.

Installing PHPMailer with Composer

The easiest way to install PHPMailer is through Composer. If you don't have Composer yet, download it from getcomposer.org. Run this command inside your project folder:

composer require phpmailer/phpmailer

Once finished, Composer will create a vendor/ folder and a composer.json file. You simply need to include vendor/autoload.php in your PHP file.

Advertisement

Configuring Gmail SMTP

To use Gmail as the sender, you need to enable an App Password on your Google account (not your regular Gmail password). Here's how: go to your Google Account → Security → 2-Step Verification (must be enabled) → App passwords → create a new password.

Sending a Simple HTML Email

Here is a complete example of sending an HTML email using Gmail SMTP:

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true); // true = enable exceptions

try {
    // SMTP Server Configuration
    $mail->isSMTP();
    $mail->Host       = 'smtp.gmail.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'youremail@gmail.com';  // your Gmail address
    $mail->Password   = 'xxxx xxxx xxxx xxxx';  // App Password (not your regular password)
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;

    // Sender and Recipient
    $mail->setFrom('youremail@gmail.com', 'Sender Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');
    $mail->addReplyTo('reply@example.com', 'Support');

    // Email Content
    $mail->isHTML(true);
    $mail->CharSet = 'UTF-8';
    $mail->Subject = 'Welcome to Our App!';
    $mail->Body    = '<h1>Hello!</h1><p>Thank you for signing up.</p>';
    $mail->AltBody = 'Hello! Thank you for signing up.'; // Plain text version

    $mail->send();
    echo 'Email sent successfully!';

} catch (Exception $e) {
    echo "Email could not be sent. Error: {$mail->ErrorInfo}";
}

Adding Attachments

PHPMailer makes it easy to attach files to an email:

<?php

// ... (same SMTP configuration as above)

$mail->Subject = 'Order Invoice #12345';
$mail->Body    = '<p>Your order invoice is attached.</p>';

// Add an attachment from a file path
$mail->addAttachment('/path/to/file/invoice_12345.pdf', 'Invoice.pdf');

// Add an attachment from a string (e.g. a PDF generated on-the-fly)
// $mail->addStringAttachment($pdf_content, 'report.pdf');

$mail->send();

Building a Clean Email Template

For a more professional email, separate the HTML content into its own variable:

Advertisement
<?php

$user_name = htmlspecialchars('Budi Santoso');
$verification_link = 'https://example.com/verify?token=abc123xyz';

$html_body = <<<HTML
<!DOCTYPE html>
<html>
<body style="font-family: Arial, sans-serif; background: #f4f4f4; padding: 20px;">
  <div style="background: white; padding: 30px; border-radius: 8px; max-width: 600px; margin: 0 auto;">
    <h2 style="color: #333;">Verify Your Email</h2>
    <p>Hi <strong>$user_name</strong>,</p>
    <p>Click the button below to verify your account:</p>
    <a href="$verification_link"
       style="background: #4CAF50; color: white; padding: 12px 24px;
              text-decoration: none; border-radius: 4px; display: inline-block;">
      Verify Email
    </a>
    <p style="color: #999; font-size: 12px;">This link is valid for 24 hours.</p>
  </div>
</body>
</html>
HTML;

$mail->Body    = $html_body;
$mail->AltBody = "Hi $user_name, verify your email at: $verification_link";

Tips for Sending to Multiple Recipients

PHPMailer supports several ways to add recipients:

<?php

// Primary recipient (To)
$mail->addAddress('user1@example.com', 'User One');
$mail->addAddress('user2@example.com'); // name is optional

// CC (Carbon Copy) - all recipients can see it
$mail->addCC('manager@example.com', 'Manager');

// BCC (Blind Carbon Copy) - other recipients cannot see it
$mail->addBCC('archive@example.com');

Conclusion

PHPMailer is the best email-sending solution for PHP. With support for SMTP, TLS/SSL, HTML email, attachments, and solid error handling, PHPMailer is far more reliable than PHP's built-in mail() function. Install it via Composer, configure Gmail SMTP with an App Password, and you can start sending professional emails from your PHP application in a matter of minutes.

Advertisement
phpmailer kirim email php phpmailer smtp gmail php kirim email phpmailer composer email php native
Share this article
Back to Blog
🚀 Partner Recommendation

Need Premium Source Code & Business Apps?

Access Laravel applications, POS systems, School Management, Clinic Software, ERP solutions, and ready-to-use premium source code at GudangCode.

GudangCode
  • ✔ Premium Source Code
  • ✔ Ready-to-Use Systems
  • ✔ Lifetime Updates
  • ✔ Lifetime Membership
  • ✔ Daily App Updates
Join Membership →
Advertisement
Latest Articles
Oct 15, 2026
Oct 14, 2026
Oct 13, 2026
Oct 11, 2026
Advertisement