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.
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();
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
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
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);