Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(php) - sending emails #2903

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/_posts/languages/php/2000-01-01-sending-emails.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: Sending emails from PHP
nav: Sending Emails
modified_at: 2024-12-10 10:45:00
tags: php email phpmailer smtp mail
index: 100
---

For sending emails from your PHP application you will need to send via an external SMTP server.
Several options are listed on our [dedicated section]({% post_url platform/app/2000-01-01-sending-emails %}).

You can use a mailing library such as [PHPMailer](https://github.com/PHPMailer/PHPMailer) (or equivalent) to send from your PHP application, via an external SMTP server.

### Example using PHPMailer via Brevo

```php
<?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-relay.brevo.com';
$mail->SMTPAuth = true;
$mail->Username = getenv('BREVO_USERNAME');
$mail->Password = getenv('BREVO_PASSWORD');
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;

// Recipients
$mail->setFrom('[email protected]', 'Sender Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->addReplyTo('[email protected]', 'Reply Name');

// Content
$mail->isHTML(true);
$mail->Subject = 'Test Email from PHPMailer using Brevo';
$mail->Body = '<p>This is a test email sent using PHPMailer with Brevo SMTP.</p>';
$mail->AltBody = 'This is a test email sent using PHPMailer with Brevo SMTP.';

$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
```