Linux – The PHP Mail function is disabled on shared hosting

In this tutorial we see how to overcome the disabled PHP Mail function on shared hosting with similar solutions. The PHP Mail function is often used to generate uncontrolled spam, especially on sites created with CMS such as WordPress or Joomla without the appropriate verification precautions on sending emails, however, the LAMP stacks of dedicated servers or virtual servers are not involved.

Using CodeIgniter Framework

If the application uses the CodeIgniter Framework you can remedy the email problem with this simple code. For complete functionalities, consult the documentation on www.codeigniter.com/docs

$this->load->library(’email’);
$this->email->from(‘noreply@momit.eu’, ‘Momit SRL’);
$this->email->to($email); // $email = “email_id”
$this->email->subject(‘Welcome to Momit.’);
$this->email->message(‘<html>
<head>
</head>
<body>
<p><b>Here write the content of the email..</b></p>
</body>
</html>’);
$this->email->send();

Using the PEAR library

The PEAR library has also integrated the management function for sending mail and supports SMTP authentication using an existing account defined on the domain.

include(‘Mail.php’); // PEAR Mail function

$headers = array (‘From’ => $from, ‘To’ => $to, ‘Subject’ => $subject); 
// mail header 

$smtp = Mail::factory(‘smtp’, array (‘host’ => “localhost”, ‘auth’ => true,
‘username’ => $username, ‘password’ => $password, ‘port’ => ‘587’)); 
// SMTP parameters

$mail = $smtp->send($to, $headers, $body); // sending mail

Using SwiftMailer

SwiftMailer is another method of sending emails from your site. Again it supports SMTP authentication using an existing account defined on the domain.

$mailer = Swift_Mailer::newInstance($transport);

$transport->setUsername(‘no_reply@momit.eu’);
$transport->setPassword(‘password_account’);

$message = Swift_Message::newInstance();

$message->setSubject(‘E-mail subject’);
$message->setFrom(array(‘noreply@momit.eu’ => ‘Sender Name’));
$message->setTo(array($email));

$message->addPart(‘<p>If <b>HTML is necessary, use addPart()</b> in the body of the e-mail </p>’, ‘text/html’);
$result = $mailer->send($message); // returns FALSE if email sending fails

Using SendGrid

SendGrid is an external service that allows you to send thousands of emails via HTTP simply by creating an account and integrating an API Key. For complete documentation, visit www.sendgrid.com

$sendgrid = new SendGrid($api_user, $api_key);
$email = new SendGridEmail();
 
$email->addTo("no_reply@momit.eu")
 ->setFrom("no_reply@momit.eu")
 ->setSubject("Email subject")
 ->setHtml("Email body, can contain HTML");
 
$sendgrid->send($email);

Do you have any issue configuring mail senders in your PHP website ?

Please contact our technical support by chat or contact us by using the customer area

Keep up to date

Check out our blog to know our case studies or industry news.

Need support?

In our support page you will find all the information you need


Viale Enrico Forlanini, 23 20134 Milano - P.IVA / C.F IT07634600964 - Tel. +39 02 87187306
Copyright © 2015 - 2020 MOMIT - All Rights Reserved