PHP provides several methods for sending e‑mail messages from scripts. On ruachost.com, you can use the built‑in mail() function, the PEAR Mail class, or PHPMailer depending on your hosting package and requirements.

 

Important Hosting Restrictions

  • If you are on Shared Hosting, Reseller Hosting, or Managed WordPress Hosting, you cannot use external SMTP servers.

  • You must send e‑mail through ruachost.com servers.

  • Other hosting packages (Managed VPS, Dedicated) allow more flexibility with external SMTP servers.

 

Method 1: Using the PHP mail() Function

The simplest way to send e‑mail:

<?php
mail(
  "recipient@example.com",
  "This is the message subject",
  "This is the message body",
  "From: sender@example.com\r\nContent-Type: text/plain; charset=utf-8",
  "-fsender@example.com"
);
?>
Always set the From address correctly.
  • If not set, messages may appear as nobody@example.com.

  • Limitations: no SMTP authentication, limited header control.

 

Method 2: Using the PEAR Mail Class

More powerful and supports SMTP authentication.

<?php
require 'Mail.php';

$recipient = 'recipient@example.com';
$headers['From'] = 'sender@example.com';
$headers['To'] = $recipient;
$headers['Subject'] = 'This is the message subject';
$headers['Content-Type'] = 'text/plain; charset=utf-8';
$body = 'This is the message body';

$smtp_params['host'] = 'ssl://server_name';
$smtp_params['port'] = '465';
$smtp_params['auth'] = true;
$smtp_params['username'] = 'username';
$smtp_params['password'] = 'password';

$mail_object = Mail::factory('smtp', $smtp_params);
$mail_object->send($recipient, $headers, $body);
?>
  • Requires installing the PEAR Mail package.

  • Supports authentication and advanced headers.

 

Method 3: Using PHPMailer

PHPMailer is a popular library with robust features.

<?php
use PHPMailer\PHPMailer\PHPMailer;

require 'vendor/autoload.php';

$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.server.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

$mail->setFrom('sender@example.com', 'Sender Name');
$mail->addAddress('recipient@example.com');
$mail->Subject = 'This is the message subject';
$mail->Body    = 'This is the message body';

if(!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
?>
​
  • Easier formatting of HTML e‑mails.

  • Better error handling.

  • Widely used in CMS platforms like WordPress.

Best Practices

  • Always set proper headers (From, Reply‑To, Content‑Type).

  • Use SMTP authentication when possible to reduce spam filtering.

  • Test with different mail clients to ensure formatting.

  • Avoid sending bulk mail directly from PHP scripts; use mailing services.

 
Was this answer helpful? 0 Users Found This Useful (0 Votes)

Powered by WHMCompleteSolution