linuxfront-v2-logistics/emailTest.php 📧
A simple PHP script designed to:
- Display available stream transports
- Configure SMTP settings for Office 365
- Send a basic HTML test email
Note: By default, the script exits immediately. Remove or comment out the
exit;statements to execute the email-sending logic.
Purpose
This file serves as a quick diagnostics tool for PHP mail configuration on a Windows or Linux server. It helps you:
- Verify which stream transports PHP supports
- Configure
SMTP,smtp_port, andsendmail_fromat runtime - Compose and send an HTML email using
mail()
Execution Flow
Below is the intended flow when you remove the early exit; calls:
flowchart TD
A[Start script] --> B[Display stream transports]
B --> C[Set SMTP server, port, and sender]
C --> D[Compose HTML email]
D --> E[Invoke mail() function]
E --> F[Print success message]
Key Sections & Code Walkthrough
<?php
exit; // ❶ Stops script immediately (comment out to proceed)
// ❷ Show which transports PHP supports
$xportlist = stream_get_transports();
print_r($xportlist);
// ❸ Dynamically set mail configuration
ini_set("SMTP", "smtp.office365.com");
ini_set("smtp_port", 587);
ini_set("sendmail_from", "[email protected]");
exit; // ❹ Stops before sending mail
// exit; // ❺ Redundant exit
// ❻ Email parameters
$to = "[email protected]";
$subject = "from Imtiaz";
$message = "
<html>
<head><title>Tech</title></head>
<body><p>Hi Muffin</p></body>
</html>";
// ❼ Email headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1\r\n";
$headers .= 'From: <[email protected]>' . "\r\n";
// $headers .= 'Cc: [email protected]' . "\r\n";
// ❽ Send the email
mail($to, $subject, $message, $headers);
// ❾ Success message
echo "Mail sent successfully";
?>
Detailed Breakdown
1. Early Exit (❶, ❹, ❺)
- Purpose: Prevents the rest of the script from running.
- Action: Comment out or remove to allow diagnostics and emailing.
2. Stream Transports (❷)
| Function | Returns |
|---|---|
stream_get_transports() |
List of registered transports (e.g., tcp, ssl) |
3. Runtime INI Settings (❸)
| Directive | Value | Description |
|---|---|---|
SMTP |
smtp.office365.com | SMTP server hostname |
smtp_port |
587 | SMTP port (TLS) |
sendmail_from |
[email protected] | Default “From” email address |
🚀 Tip: These overrides apply only during script execution—no need to edit
php.ini.
4. Email Composition (❻–❼)
- Recipient (
$to): Target email address - Subject (
$subject): Simple test label - HTML Body (
$message): Basic HTML structure - Headers (
$headers):- MIME version
- Content-Type for HTML
- From address
5. Sending & Feedback (❽–❾)
mail(): PHP’s native mail function- Echo: Confirms dispatch
Usage Instructions
- Upload to your webroot or call via CLI.
- Ensure PHP has permission to send mail.
- Remove the first two
exit;lines. - Access the script in a browser or run
php emailTest.php. - Verify output and check inbox for test email.
Troubleshooting
-
❌ No transports listed?
Ensure PHP’ssocketsoropensslextensions are enabled. -
❌ Mail not sending?
- Check firewall for outbound port 587.
- Verify Office 365 credentials or relay permissions.
-
❌ Headers ignored?
Some Windows configurations ignoresendmail_from.
Summary
This lightweight script offers a hands-on way to validate and configure PHP’s mail capabilities without editing global settings. Tweak the exits and INI overrides to fit your environment, then send a quick HTML email to confirm everything works as expected. 🚀