Sending an email from a PHP script might seem like a small task, but it powers everything from contact forms to marketing campaigns. The PHP Send Email Sample we’ll walk through shows how you can quickly add this vital feature to your web projects. By the end of this article, you’ll have multiple reliable methods—simple mail, PHPMailer, SMTP, and bulk sending—ready to use.
Why does this matter? Email remains the backbone of online communication: a 2023 survey found that 74 % of companies rely on email for customer outreach and 61 % use it for internal collaboration. Getting the ball rolling with a robust email solution saves you time, reduces errors, and boosts user satisfaction. Stay tuned as we explore core concepts, practical examples, and best‑practice tips that even a budding PHP developer can implement confidently.
Read also: Php Send Email Sample
Understanding the Basics of PHP Send Email Sample
At its core, PHP’s built‑in mail() function offers a straightforward way to send emails. However, its simplicity also means you’re limited in handling attachments or custom headers. When using just mail(), you typically set the recipient, subject, and message body, then rely on the server’s default mail transfer agent (MTA). While easy, this method often gets flagged by spam filters, especially if you’re sending from shared hosting.
Choosing the right approach depends on your needs:
- Local or low‑volume tasks:
mail()is fine. - Attachments, HTML content, or dynamic headers: use a library like PHPMailer.
- Secure, authenticated SMTP: best for production and higher deliverability.
- Bulk campaigns: rate limiting, BCC, or dedicated email services improve success.
| Method | When to Use | Pros | Cons |
|---|---|---|---|
mail() |
Small scripts, testing | Fast installation, no extra libs | Limited headers, lower deliverability |
| PHPMailer | Production, attachments | Feature rich, easy SMTP | Extra dependency |
| SMTP Authentication | Corporate mail servers | Higher deliverability, security | Setup complexity |
Ultimately, the PHP Send Email Sample you choose should align with your project’s scale, deliverability goals, and the complexity of the emails you plan to send. With that foundation, let’s dive into real‑world examples that demonstrate each technique in action.
Read also: Plant Visit Request Email Sample
PHP Send Email Sample with Basic mail() Function
This example shows a simple, one‑liner email using PHP’s native mail(). It’s great for quick tests but remember to check your server’s mail configuration.
$to = 'recipient@example.com';
$subject = 'Welcome to Our Site';
$message = 'Thank you for signing up!';
$headers = 'From: no-reply@example.com' . "\r\n" .
'Reply-To: support@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
If you want to add HTML or attachments, drop the mail() example and move to PHPMailer for more flexibility.
Read also: Position Inquiry Email Sample
PHP Send Email Sample Using PHPMailer for Attachments
Here’s how you bundle a PDF or image and set up a clean, HTML body with PHPMailer. First, install PHPMailer via Composer:composer require phpmailer/phpmailer (outside this article).
See the PDF below.
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('no-reply@example.com', 'Website');
$mail->addAddress('customer@example.com');
$mail->Subject = 'Your Invoice';
$mail->Body = 'Invoice Attached
{{META_INFO}}
$mail->isHTML(true);
$mail->addAttachment('/path/to/invoice.pdf', 'invoice.pdf');
if(!$mail->send()){
echo 'Error: ' . $mail->ErrorInfo;
}else{
echo 'Message sent!';
}
This pattern ensures that recipients see a rich greeting and receive the attachment without glitches.
Read also: Power Outage Notification Email Sample
PHP Send Email Sample with SMTP Authentication
When your web app lives on a shared host or you simply prefer secure delivery, SMTP authentication is a must. Below is a minimal, reusable function that accepts parameters for dynamic content and handles all the boilerplate.
function sendEmailSMTP($to,$subject,$body){
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myemail@gmail.com';
$mail->Password = 'app_password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('myemail@gmail.com', 'My Site');
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body = $body;
return $mail->send();
}
Now you can call sendEmailSMTP('user@example.com','Test','Hello!'); from anywhere in your code while you keep sensitive credentials out of the public files.
PHP Send Email Sample for Bulk Email Campaign
For marketing teams, sending thousands of emails demands pacing, personalization, or at least efficient looping. Here’s a handy script that loads addresses from a CSV file, uses PHPMailer, and respects a simple rate limit.
$handle = fopen('recipients.csv','r');
$mail = new PHPMailer();
$mail->isSMTP();
mail configuration…
while(($data = fgetcsv($handle,1000,',')) !== FALSE){
list($email,$firstName) = $data;
$mail->clearAddresses();
$mail->addAddress($email,$firstName);
$mail->Subject = 'Hello '.$firstName;
$mail->Body = 'Hi '.$firstName.', check out our new demo!';
$mail->send();
sleep(1); // 1‑second pause to avoid throttling
}
Remember to comply with anti‑spam rules—use clear opt‑in notices and add a visible unsubscribe link to maintain a healthy sender reputation.
Conclusion
With these PHP Send Email Samples in hand, you can confidently tackle anything from a single welcome email to a full‑blown marketing blast. Start simple with mail() for quick setups, then evolve to PHPMailer and SMTP for reliable, safe delivery. And if you’re aiming for scale, just reinforce the logic with loops, rate limiting, and data loads.
Ready to plug one of these snippets into your next project? Reach out to our community, share your implementation, or explore deeper customizations on our developer portal. Happy coding, and may your inbox always contain the right messages, fast and secure!