PHP Mail Function Disabled on Shared Web Hosting

We had previously decided to disable the mail function because the the mail() function was typically employed by malicious users on our network to send spam and alternative uninvited e-mails. we tend to suggest our customers to use an e-mail library so as to send e-mails, there are many in which to choose from.

CodeIgniter Framework.

If you make use of the CodeIgniter framework, you can use the built-in Email class in order to send e-mails. Here is sample code using the CodeIgniter’s Email class:

$this->load->library(’email’);
$this->email->from(‘[email protected]’, ‘ownwebservers Limited (No Reply)’);
$this->email->to($email); // $email = “email_id”
$this->email->subject(‘Welcome to ownwebservers Limited.’);
$this->email->message(‘<html>
<head>
</head>
<body>
<p><b>Message here</b></p>
</body>
</html>’);
$this->email->send();

You can also autoload the Email class within the config/autoload.php file which means you do not need to use the first line of code to instantiate the Email class before making use of it.
For more information on the Email class in CodeIgniter, see the documentation.

PEAR Library.

The PEAR library also has a built-in Mail class for sending e-mails, including e-mails over SMTP authentication with an already-existing e-mail account. Here’s an example:

include(‘Mail.php’); // includes the PEAR Mail class
$headers = array (‘From’ => $from, ‘To’ => $to, ‘Subject’ => $subject); // the email headers
$smtp = Mail::factory(‘smtp’, array (‘host’ => “localhost”, ‘auth’ => true, ‘username’ => $username, ‘password’ => $password, ‘port’ => ‘587’)); // SMTP protocol with the username and password of an existing email account in your hosting account
$mail = $smtp->send($to, $headers, $body); // sending the email

For more information, see the page on the Mail class on the PEAR website.
Note: The code above does not catch any errors so we’d recommend you check the documentation for more elaborative examples.

To find the SMTP port, go to “Configure Email Client” under the “More” menu within “Email Accounts” of cPanel. It may be port 587, but check to be sure.

SwiftMailer Library.

The SwiftMailer library is another popular choice for sending e-mails. Here’s an example:
$transport = Swift_SmtpTransport::newInstance(‘mail.example.com’, 587); // your mail server address and port. If you don’t know what yours is, go to cPanel -> E-mail Settings and for the specific e-mail account, More -> Configure E-mail Client – it will be displayed there.

$mailer = Swift_Mailer::newInstance($transport); // creates new instance of an SMTP transport specifically

$transport->setUsername(‘[email protected]’);
$transport->setPassword(‘your_password_here’);

$message = Swift_Message::newInstance();

$message->setSubject(‘Set the subject of the e-mail’);
$message->setFrom(array(‘[email protected]’ => ‘Your Name/Company Name’));
$message->setTo(array($email));

$message->addPart(‘<p>If you want <b>HTML in your e-mail use addPart()</b></p>’, ‘text/html’);

$result = $mailer->send($message); // returns FALSE boolean on failure

if(!$result)
{
echo ‘failure’;
}
else
{
echo ‘success’;
}

Feel free to contact our staff member for any help.