
There are several different methods for sending emails on traditional servers. In PHP-based applications, two common options stand out: PHP’s built-in mail() function and a more advanced library called PHPMailer. Both methods handle email sending but have different advantages and use cases.
1. PHP mail() Function
PHP’s built-in mail() function is the most basic method for sending emails. However, it relies on the server’s local mail service, and if the configuration is not correct, emails may be marked as spam or fail to send. The mail() function is limited in terms of security and configuration.
<?php
$to = "recipient@example.com";
$subject = "Email Subject";
$message = "Hello, this is a test email!";
$headers = "From: sender@example.com";
if (mail($to, $subject, $message, $headers)) {
echo "Email sent successfully!";
} else {
echo "Failed to send email.";
}
?>
Advantages:
- Easy to use and works directly with PHP.
- No need for extra installations.
Disadvantages:
- Potential security weaknesses.
- High risk of emails being marked as spam.
- No support for attachments or SMTP authentication.
2. PHPMailer Library
PHPMailer is a more advanced and reliable method. It supports sending emails via SMTP (Simple Mail Transfer Protocol), which reduces the likelihood of emails being marked as spam. It also allows sending HTML emails, attachments, and authenticated SMTP emails.
How to Install PHPMailer?
You can install PHPMailer in two ways:
- Via Zip File: Download the zip file from PHPMailer’s GitHub page, extract it to a folder on your server, and include it in your project like this:
- Via Composer: Use Composer to install PHPMailer from the terminal on your server:
PHPMailer Email Sending Example
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php'; // If using Composer
$mail = new PHPMailer(true);
try {
// SMTP settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com'; // Your SMTP server address
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com'; // SMTP username
$mail->Password = 'your_password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Sender and recipient details
$mail->setFrom('your_email@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
// Email content
$mail->isHTML(true);
$mail->Subject = 'Test Email';
$mail->Body = '<h1>Hello, this is a test email!</h1>';
$mail->AltBody = 'Hello, this is a test email!';
$mail->send();
echo 'Email sent successfully';
} catch (Exception $e) {
echo "Email could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Real-World Example: Sending an Activation Link via PHPMailer
In a real-world scenario, PHPMailer can be used to send an activation link after user registration. Here is an example:
<?php
// PHPMailer manually loaded into the relevant project directory
require 'PHPMailer/PHPMailerAutoload.php';
function send_activation_email($user_email, $password, $activation_code) {
$host_url = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]";
$activation_link = $host_url . "/app2/app_backend/activate.php?email=$user_email&activation_code=$activation_code";
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.hostinger.com'; // Specify SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'YOUR_USER_NAME'; // SMTP username
$mail->Password = 'YOUR_PASSWORD'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable SSL encryption
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('YOUR@EMAIL.SERVICE', 'YOUR BUSINESS NAME');
$mail->addAddress($user_email); // Add recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Your Activation Details';
$mail->Body =
'<img src="cid:logo_img" alt="Logo" />' .
'<p>You can activate your account by clicking <a href="' . $activation_link . '">here</a>.</p>' .
'<p>Email: ' . $user_email . '</p>' .
'<p>Password: ' . $password . '</p>' .
'<p>Best regards, <br> YOUR BUSINESS NAME</p>';
$mail->AltBody = 'Activation link: ' . $activation_link . "\n\nBest regards,\nYOUR BUSINESS NAME";
if(!$mail->send()) {
return ['status' => 'error', 'message' => 'Activation email could not be sent.'];
} else {
return ['status' => 'success', 'message' => 'New registration successful and activation email sent.'];
}
}
?>
PHPMailer Advantages
- More reliable email delivery with SMTP support.
- Supports attachments, HTML content, and multiple recipients.
- Offers more flexible and secure configurations.
- Less likely to be marked as spam.
Which Method to Choose?
For small and simple projects, the mail() function may be sufficient. However, ensure that the server configurations are correct.
For larger projects or professional applications, a library like PHPMailer should be preferred. With SMTP, you can send emails more securely and without issues.

