View Categories

How to Send Authenticated SMTP Emails with PHPMailer

🌐This guide is also available in Arabic
← اقرأ بالعربية

Reliable PHP Email Delivery via Authenticated SMTP #

While the standard PHP mail() function is supported on Tarweig servers, sending emails through PHPMailer using authenticated SMTP is strongly recommended. Authenticated SMTP ensures high inbox deliverability, bypasses spam filters, supports DKIM and SPF verification, and handles attachments reliably.

Prerequisites: #

  • An active email account created in your cPanel (e.g., [email protected]).
  • Your Tarweig outgoing mail server hostname (e.g., mail.yourdomain.com or server hostname).
  • Composer or the official PHPMailer library files uploaded to your web directory.

1. Installing PHPMailer via Composer #

If you use Composer, install PHPMailer inside your project directory:

composer require phpmailer/phpmailer

2. Complete PHPMailer SMTP Script Example #

Create a script named send_mail.php and use the following verified configuration:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Server Settings
    $mail->isSMTP();
    $mail->Host       = 'mail.yourdomain.com';      // Your SMTP server hostname
    $mail->SMTPAuth   = true;
    $mail->Username   = '[email protected]';      // Your full email address
    $mail->Password   = 'YourStrongEmailPassword';   // Your email account password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Use SSL on port 465
    $mail->Port       = 465;                        // Port 465 (SSL) or 587 (TLS)

    // Sender & Recipients
    $mail->setFrom('[email protected]', 'Your Name or Brand');
    $mail->addAddress('[email protected]', 'Recipient Name');
    $mail->addReplyTo('[email protected]', 'Support');

    // Email Content
    $mail->isHTML(true);
    $mail->Subject = 'Test Email via Tarweig Authenticated SMTP';
    $mail->Body    = '<h1>Email Delivery Successful!</h1><p>This message was sent securely using PHPMailer and Tarweig SMTP.</p>';
    $mail->AltBody = 'This is the plain text version of the email for non-HTML mail clients.';

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

3. SMTP Configuration Summary #

Setting SSL Encryption (Recommended) TLS Encryption (Alternative)
Outgoing Server mail.yourdomain.com mail.yourdomain.com
Port 465 587
Encryption PHPMailer::ENCRYPTION_SMTPS PHPMailer::ENCRYPTION_STARTTLS
Authentication Required (Username & Password) Required (Username & Password)
Updated on 11/10/2026

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top