Select a topic:

Choose from one of the topics below to browse other articles

Articles & Information.

Sending emails using a php script

Last updated by Oleg C. on December 08, 2016 16:26

As you may know, spam is considered to be one of the biggest problems of the modern Internet. People who just use emails hate to receive these annoying letters. And people who administer mail servers hate it when someone is using their servers to send spam. Apart from the fact that massive spam may cause system resource overload (it is momentary and quite easy to fight with, but nevertheless it causes inconveniences to the customers), high volume of outgoing spam always causes the mail IP to be blacklisted by anti-spam organization, causing deliverability issues.

For this reason here in Jolt we do not allow to send emails without proper SMTP authentication, and here is why:

  • spammers will not need to have your email account password to send spam through you;
  • it is possible to simply upload a script which will have a list of destination email accounts and make it to be executed - this results in a huge mail queues, system resources overload and IPs blacklisting;
  • if you send an email without SMTP authentication, it will be sent from username@server_hostname address. Many big email providers will just not accept an email with such FROM address, and will consider it spam as well (so you can send 'spam' even not knowing about it).

It is good to make use of SMTP authentication, since this way all your emails from php scripts will be considered valid and 'clean' by all mail server providers (all our servers have proper PTR-records and rDNS/fDNS match).

Most of CMS solutions provide an in-built possibility to use SMTP instead of php mail() function (or wp_mail() function), and in some cases you will just need to install an additional plugin or extension for this.

Though in case you use a self-written site, or your CMS/local site builder does not support SMTP - we have a good solution for you.

php mail() does not support SMTP authentication, and does not let you indicate your SMTP host, username and password. But it is possible to overcome it using php PEAR packages. They are already installed for most of the php versions used on our server. Please make use of 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>");
  }
 ?>

The path for PEAR Mail.php library is as follows:

  • for php native version (in case 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

For host - please use localhost in case your domain uses our mail server to process emails. In case you use an external mail server - SMTP ports are closed by default on all our servers, but we can surely open them for you. Just contact us via Live Chat or HelpDesk and we will assist. For host in this case use the FQDN which was given by your mail service provider.

You can also make use of the following code to enable 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>");
  }
 ?>

In case you have a VPS or Dedicated Server, and you wish to have such rules (prohibiting mail without SMTP authentication) - please contact us via HelpDesk and we will assist (these rules creation will require a Managed Service for your server.

In case you have any additional questions - please do not hesitate to contact us via Live Chat or HelpDesk, and we will gladly assist you with your questions.