Send Email From PHP Using SMTP Authentication (Guide)

WhmSonic

Well-Known Member
Mar 19, 2007
46
0
156
Hello,
This is useful article. In this article I am showing how to send email from PHP using SMTP Authentication for WHM/cPanel Servers.

Everybody knows that normal php mail functions sending emails as a nobody and this is cause to spam emails.

Login to your root SSH and copy/use following command line to your SSH line.
Code:
pear install Mail;pear install Net_SMTP;pear install Auth_SASL;
After above command, you can use the following php script and send real emails. Below script is sample, you may create your own email form scripts using pear.

ATTENTION: Following script using your dedicated IP for sending emails, do not create multiple email send scripts otherwise your server will be black listed. Use it for your user signup scripts, email verification scripts ...

PHP:
<?php
require_once "Mail.php";
$from = "MyName <[email protected]>";
$to = "TargetName <[email protected]>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "mail.mailserver.com";
$username = "[email protected]";
$password = "my_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>");
}
?>
 
Last edited:

cPanelDavidG

Technical Product Specialist
Nov 29, 2006
11,214
13
313
Houston, TX
cPanel Access Level
Root Administrator
Note, if you're doing this via the cPanel interface - installing the PHP PEAR package "Mail" will install all of that for you :).

Just giving the standard head's up that using SuPHP will avoid the user nobody issue, but I understand not everyone wants to use SuPHP on their servers :).
 

Lawrence89

Registered
Jan 31, 2008
3
0
51
Could you explain a little more how to install PEAR via the CPanel? I am looking at the CPanel interface and am not seeing what I can use to install the package. I've tried logging into my host using Putty to do it the first way that was suggested, but apparently this type of access has not been activated for my account.
 

cPanelDavidG

Technical Product Specialist
Nov 29, 2006
11,214
13
313
Houston, TX
cPanel Access Level
Root Administrator
Could you explain a little more how to install PEAR via the CPanel? I am looking at the CPanel interface and am not seeing what I can use to install the package. I've tried logging into my host using Putty to do it the first way that was suggested, but apparently this type of access has not been activated for my account.
In the cPanel 11/X3 interface, go to the Software/Services section and click on PHP PEAR Packages. On that page, you can install PHP PEAR packages.

If you are not using the X3 theme or not using cPanel 11, please contact your web hosting provider and encourage them to use cPanel 11 with the X3 theme as that is recommended at this time.
 

octeto

Well-Known Member
Nov 9, 2002
49
0
156
Thanks very much!

Regards,
octeto

Hello,
This is useful article. In this article I am showing how to send email from PHP using SMTP Authentication for WHM/cPanel Servers.

Everybody knows that normal php mail functions sending emails as a nobody and this is cause to spam emails.

Login to your root SSH and copy/use following command line to your SSH line.
Code:
pear install Mail;pear install Net_SMTP;pear install Auth_SASL;
After above command, you can use the following php script and send real emails. Below script is sample, you may create your own email form scripts using pear.

ATTENTION: Following script using your dedicated IP for sending emails, do not create multiple email send scripts otherwise your server will be black listed. Use it for your user signup scripts, email verification scripts ...

PHP:
<?php
require_once "Mail.php";
$from = "MyName <[email protected]>";
$to = "TargetName <[email protected]>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "mail.mailserver.com";
$username = "[email protected]";
$password = "my_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>");
}
?>