We had previously decided to disable the mail function because the mail() function was often used by malicious users on our network to send spam and other unsolicited e-mails. We recommend our customers to use an e-mail library in order to send e-mails, and 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]’, ‘Dream Line IT Solution (No Reply)’);
$this->email->to($email); // $email = “email_id”
$this->email->subject(‘Welcome to Dream Line IT Solution.’);
$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’;
}

Some of our staff members are experienced with PHP so if you require any assistance with your PHP code to send e-mail using any one of these libraries or a different library you may want to use, you are most welcome to post your query on our Web Hosting Forum where a staff member or customer on the forum will be happy to assist you.

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *