"I am Saqib Jahangir. A passionate vlogger, software engineer, trainer and avid traveler with a deep love for exploring the hidden gems of our beautiful planet. With a strong foundation in Application Development, Application Architecture & Database Design and Product Management, I bring over a decade of hands-on experience building secure, scalable, and resilient web applications for a diverse range of industries."

Sending Emails in PHP

 

Email functionality is essential for most web applications — from sending welcome messages to handling password resets and notifications. PHP provides built-in functions as well as libraries for sending emails.


Method 1: Using PHP mail() Function

PHP has a simple built-in mail() function that works if your server is properly configured with an SMTP server.

Example: Sending a Basic Email

<?php

$to = "user@example.com";

$subject = "Welcome to My Website!";

$message = "Hello, thank you for registering on our site.";

$headers = "From: admin@mywebsite.com";

 

if (mail($to, $subject, $message, $headers)) {

    echo "Email sent successfully!";

} else {

    echo "Failed to send email.";

}

?>

Easy to use, but it depends on the server configuration. On many shared hosting or local setups, it may not work without extra setup.


Method 2: Sending Emails with PHPMailer (Recommended)

PHPMailer is a popular library for sending emails in PHP. It supports SMTP authentication, HTML emails, and attachments, making it much more reliable than the default mail() function.

Install PHPMailer via Composer

composer require phpmailer/phpmailer

Example: Sending an Email with PHPMailer

<?php

use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

 

require 'vendor/autoload.php';

 

$mail = new PHPMailer(true);

 

try {

    // Server settings

    $mail->isSMTP();

    $mail->Host       = 'smtp.gmail.com'; // SMTP server

    $mail->SMTPAuth   = true;

    $mail->Username   = 'your_email@gmail.com';

    $mail->Password   = 'your_password'; // Use App Passwords for Gmail

    $mail->SMTPSecure = 'tls';

    $mail->Port       = 587;

 

    // Recipients

    $mail->setFrom('your_email@gmail.com', 'My Website');

    $mail->addAddress('user@example.com', 'John Doe');

 

    // Content

    $mail->isHTML(true);

    $mail->Subject = 'Welcome to My Website!';

    $mail->Body    = '<h2>Thanks for signing up!</h2><p>We are glad to have you.</p>';

    $mail->AltBody = 'Thanks for signing up! We are glad to have you.';

 

    $mail->send();

    echo 'Message has been sent successfully';

} catch (Exception $e) {

    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

}

?>

Works with Gmail, Outlook, or any SMTP server.
More secure and flexible than mail().


Method 3: Sending Attachments with PHPMailer

$mail->addAttachment('/path/to/file.pdf');

$mail->addAttachment('/path/to/image.jpg', 'photo.jpg'); // custom file name


🎯 Best Practices for Sending Emails in PHP

  • Use SMTP authentication (don’t rely on mail() in production).
  • Always validate email addresses before sending.
  • Use HTML emails for better design but provide plain-text fallback.
  • Secure credentials using environment variables (.env).
  • For bulk mailing, consider services like SendGrid, Mailgun, or Amazon SES.

👉 Conclusion:
"Sending emails in PHP can be done with the built-in
mail() function, but for reliability and advanced features, libraries like PHPMailer are recommended. With SMTP authentication, HTML formatting, and attachment support, PHPMailer makes it easy to integrate email functionality into any PHP project."

 

Popular Posts

Operators (Arithmetic, Comparison, Logical)

Functions (Built-in & User-defined)