Send Mail with PHP and SMTP

If you are using Cpanel first install the Pear Mail. To do so go to Software/Service and find PHP Pear Packages Like so:

Once You have clicked on PHP Pear Packages you will type in Mail:

This will allow you to search for the PHP Pear Mail package. You will see it listed like so:

Click on the Install button to install Pear Mail. Once it is installed you can use the following Code:

$subject = “This mail is sent from SMTP.”;
$mail_body = “This is the body of the mail which is sent using SMTP.”;
$from = “From: From Name <fromaddress@xpertdeveloper.com>”; 
$to = “To: To Name <toaddress@xpertdeveloper.com>”; 
$receiver = “toaddress@xpertdeveloper.com”; 

// Setting up the headers
$headers[“From”] = $from; 
$headers[“To”] = $to; 
$headers[“Subject”] = $subject; 
$headers[“Reply-To”] = “reply@address.com”; 
$headers[“Content-Type”] = “text/plain; charset=ISO-2022-JP”; 
$headers[“Return-path”] = “returnpath@address.com”; 

// Setting up the SMTP setting
$smtp_info[“host”] = “smtp.server.com”; 
$smtp_info[“port”] = “25”; 
$smtp_info[“auth”] = true; 
$smtp_info[“username”] = “smtp_user”; 
$smtp_info[“password”] = “smtp_password”; 

// Creating the PEAR mail object :
$mail_obj =& Mail::factory(“smtp”, $smtp_info); 

// Sending the mail now
$mail_sent = $mail_obj->send($receiver, $headers, $mail_body); 

// If any error the see for that here:
if (PEAR::isError($mail_sent)) { print($mail_sent->getMessage());}

Leave a Reply

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