SMTP delivers mail through an authenticated relay configured with TLS and sender credentials.
What It Does
SMTP delivers mail through an authenticated relay configured with TLS and sender credentials. The following examples distinguish a normal use case, a boundary case and a practical application.
Requirements and Compatibility
A maintained SMTP mailer, a verified sender domain and approved SMTP credentials; email delivery is asynchronous.
The code focuses on the core concept. Production implementation may also need routes, validation, persistence, authorization or deployment tooling.
Example 1: Basic Behavior
Use a maintained SMTP library rather than implementing MIME and TLS yourself.
composer require symfony/mailer symfony/mime
This is a command-line example; use it in its appropriate file or shell, not in the PHP source editor.
Example 2: Another Case
Send an HTML/text multipart message with SMTP credentials and TLS configured in MAILER_DSN.
<?php
require __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;
$dsn = getenv('MAILER_DSN');
if (!$dsn) throw new RuntimeException('MAILER_DSN is required');
$email = (new Email())
->from('[email protected]')
->to('[email protected]')
->subject('Report is ready')
->text('Your report is ready.')
->html('<p>Your report is <strong>ready</strong>.</p>');
(new Mailer(Transport::fromDsn($dsn)))->send($email);
This example depends on an installed extension, third-party library or configured service and is not a standalone local test.
Practical Example: Putting It to Use
Attach only approved files; the file must exist and be checked before handing the message to the mailer.
<?php
require __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Mime\Email;
$message = (new Email())->from('[email protected]')
->to('[email protected]')->subject('Monthly report')->text('Attached PDF.')
->html('<p>Attached is the <strong>monthly report</strong>.</p>')
->attachFromPath('/srv/reports/report.pdf', 'report.pdf', 'application/pdf');
// Ensure this file is trusted, size-limited and inaccessible to unapproved users.
This example depends on an installed extension, third-party library or configured service and is not a standalone local test.
Important Behavior and Edge Cases
mail() returning success does not prove delivery, and SMTP credentials must never appear in source code.
Comparison with Related Functions or Approaches
| Option | What it means in practice |
|---|---|
| Sending Email via SMTP in PHP | SMTP delivers mail through an authenticated relay configured with TLS and sender credentials. |
| Related approach | A maintained mailer handles transport negotiation, message composition and errors. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the important boundary for Sending Email via SMTP in PHP | mail() returning success does not prove delivery, and SMTP credentials must never appear in source code. |
| Assuming a related mechanism has the same behavior | A maintained mailer handles transport negotiation, message composition and errors. |
Key Takeaways
- SMTP delivers mail through an authenticated relay configured with TLS and sender credentials.
- mail() returning success does not prove delivery, and SMTP credentials must never appear in source code.
- A maintained mailer handles transport negotiation, message composition and errors.
Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.