Skip to content

Sending E‑Mail Using PHPMailer

PHPMailer is a popular PHP library that makes it easier to send e‑mail messages with advanced features such as SMTP authentication, HTML formatting, and attachments. On ruachost.com, PHPMailer is supported and can be installed via Composer or Git.

  • On Shared Hosting and Reseller Hosting, you cannot use external SMTP servers.
  • You must send e‑mail through ruachost.com servers.
  • Other hosting packages (Managed VPS, Dedicated) allow external SMTP servers.
Terminal window
composer require phpmailer/phpmailer
Terminal window
git clone https://github.com/PHPMailer/PHPMailer.git
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = gethostname(); // Use ruachost.com servers for shared/reseller/WordPress hosting
$mail->SMTPAuth = true;
$mail->Username = 'sender@example.com';
$mail->Password = 'password';
$mail->setFrom('sender@example.com', 'Sender Name');
$mail->addAddress('recipient@example.com');
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the body.';
if(!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
  • SMTP authentication for secure delivery.
  • HTML e‑mail support with inline images.
  • Attachments for files.
  • Error handling with detailed feedback.
  • Always set a valid From address.
  • Use SMTP authentication when available.
  • Test with different mail clients to ensure formatting.
  • Avoid sending bulk mail directly from PHP scripts; use mailing services for campaigns.