Have a Question?

If you have any question you can ask below or enter what you are looking for!

How to Send Emails Using a PHP Script

Spam or a flood of annoying emails is considered one of the biggest nuisances in the worldwide web. Spam messages bother server administrators because substantial spam may generate temporary system resource overload. However, it does not pose a serious threat even as spam brings inconvenience to users. However, a huge volume of outgoing spam can cause the anti-spam organisation to backlist the mail IP producing problems in delivery of messages.

Jolt does not permit sending of emails without appropriate Simple Mail Transfer Protocol (SMTP) verification for these reasons.

  • Spammers will not obtain your email passwords to send spam.
  • Without STMP authentication, the script you upload may have several destination email accounts resulting in system resources overload and IP blacklisting.
  • By sending emails without SMTP authentication, these will be sent from username@server_hostname. Many large email providers will not accept an email from such address and also consider it as spam.

You should employ SMTP authentication so your emails coming from php scripts will be considered clean and legitimate by all mail server providers. All our servers have proper PTR-records and rDNS/fDNS match).

Many CMS solutions provide the option to use SMTP instead of php mail () function (or wp_mail () function). In certain cases, you need to install an additional plugin or extension for this. If you have a self-written website or your CMS/local site builder does not support SMTP, Jolt offers an ideal solution.

The php mail () does not support SMTP authentication. It will not allow you to indicate your SMTP host, username, and password. However, you can use the php PEAR packages instead to deal with this issue. These are pre-installed for most php versions used on our server. Use the code below:

<?php
 require_once "check_the_correct_path_below/Mail.php";
 $from = "Sandra Sender <sender@example.com>";
 $to = "Ramona Recipient <recipient@example.com>";
 $subject = "Hi!";
 $body = "Hi,\n\nHow are you?";
 $host = "mail.example.com";
 $username = "smtp_username";
 $password = "smtp_password";
 $headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));
 $mail = $smtp->send($to, $headers, $body);
 if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent!</p>");
  }
 ?><br>

The path for PEAR Mail.php library:

  • For php native version (if you did not change the version in cPanel >> Select PHP Version) – /usr/local/lib/php/Mail.php
  • For php 5.3 – /opt/alt/php53/usr/share/pear/Mail.php
  • For php 5.4 – /opt/alt/php54/usr/share/pear/Mail.php
  • For php 5.5 – /opt/alt/php55/usr/share/pear/Mail.php
  • For php 5.6 – /opt/alt/php56/usr/share/pear/Mail.php

Please use localhost if your domain utilises our mail server to process emails. For external mail servers, SMTP ports are closed by default on all our servers although we can open them for you. Just contact us via Live Chat or HelpDesk for assistance. For host, use the FQDN coming from your mail service provider.

You can also use of the following code to allow SSL encryption in your emails:

<?php
 require_once "check_the_correct_path_above/Mail.php";
 $from = "Sandra Sender <sender@example.com>";
 $to = "Ramona Recipient <recipient@example.com>";
 $subject = "Hi!";
 $body = "Hi,\n\nHow are you?";
 $host = "ssl://mail.example.com";
 $port = "465";
 $username = "smtp_username";
 $password = "smtp_password";
 $headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'port' => $port,
     'auth' => true,
     'username' => $username,
     'password' => $password));
 $mail = $smtp->send($to, $headers, $body);
 if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent!</p>");
  }
 ?><br>

If you have a VPS or Dedicated Server and opt for such rules (prohibiting mail without SMTP authentication), please contact us via HelpDesk for assistance as it requires a Managed Service for your server.

For additional questions, please contact us through via Live Chat or HelpDesk.