Skip to content

How to send Email through PHP?

Updated 1 min read

Updated: 28 September 2026 · Applies to: PHPMailer 6 and PHP 8.1 to 8.4

PHP's built-in mail() function is simple but unreliable: it sends without authentication, so messages often land in spam. Sending through an SMTP mailbox with a library such as PHPMailer works much better.

Install PHPMailer

composer require phpmailer/phpmailer

Example

<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host       = 'mail.yourdomain.com';   // see Email Accounts > Connect Devices
$mail->SMTPAuth   = true;
$mail->Username   = 'noreply@yourdomain.com';
$mail->Password   = 'mailbox-password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port       = 465;

$mail->setFrom('noreply@yourdomain.com', 'Your Site');
$mail->addAddress('customer@example.com');
$mail->Subject = 'Test';
$mail->Body    = 'Hello from PHPMailer';
$mail->send();

Note: Keep passwords out of files inside public_html where possible, and respect the sending limits of your plan (100 emails per hour per account on shared hosting). For newsletters, use a dedicated email service.

Related services

Web hosting

shared hosting on Virtualmin with NVMe storage, free SSL and email.

SSL certificates

DV, OV and wildcard, with free installation help.

Reseller hosting

white-label cPanel hosting for your own clients.

Was this answer helpful?

Still stuck? Open a ticket