Sending Email Via PHP

1 min read

Dear Friends

Today we are going to learn how to

send email via php

So lets us assume a scenario where this problem might occur. suppose you are having a very popular website which even gives an option to user where user can communicate with you send messages to you.

As a owner of website you can have a backend database where you can store those received messages or you can transfer the message to concerned person in mail.

 <?php
ini_set("include_path", '/home/relsellglobal/php:' . ini_get("include_path"));
ini_set("error_log", '/home/relsellglobal/php:' . ini_get("include_path"));
// Report all errors
error_reporting(E_ALL);
require_once "Mail.php";
require_once 'Mail/mime.php';
$recipients = $_POST['recipient'];
$subject = $_POST['formname'];
$body_name = $_POST['nameperson'];
$body_email = $_POST['emailperson'];
$body_phone = $_POST['phoneperson'];
$body_message = $_POST['messageperson'];


 
 if (isset($_FILES) && move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadfile)) {
 $file = $uploadfile;
 $headers = Array();
 $headers['From'] = 'trustworthy@relsellglobal.in';
 $headers['To'] = $recipients;
 $headers['Subject'] = $subject;
 $headers["Cc"] = "relsellglobal@gmail.com";
 
 $body = "Person Name : " . $body_name . "\nEmail : " . $body_email . "\nPhone : " . $body_phone . "\nMessage : " . $body_message;
 $html = "<html><body>" . $body . "</body></html>";
 $mime = new Mail_mime;
 //$mime->setTXTBody($body);
 $mime->setHTMLBody($html);
 $mime->addAttachment($file, 'application/octet-stream');
 
 
 $body = $mime->get();
 $hdrs = $mime->headers($headers);
 $mail = Mail::factory('smtp', array(
 'host' => 'localhost',
 'port' => '25',
 'sendmail_path' => '/usr/lib/sendmail',
 'ssl' => false
 ));
 
 $result = $mail->send($recipients, $hdrs, $body);
 $caller = $_POST['caller'];
 if (PEAR::isError($mail)) {
 echo ("<p>" . $mail->getMessage() . "</p>");
 } else {
 header("Location: $caller");
 }
 } else {
 
 $headers = Array();
 $headers['From'] = 'trustworthy@relsellglobal.in';
 $headers['To'] = $recipients;
 $headers['Subject'] = $subject;
 $headers["Cc"] = "relsellglobal@gmail.com";
 
 $body = "Person Name : " . $body_name . "\nEmail : " . $body_email . "\nPhone : " . $body_phone . "\nMessage : " . $body_message;
 $html = "<html><body>" . $body . "</body></html>";
 $mime = new Mail_mime;
 //$mime->setTXTBody($body);
 $mime->setHTMLBody($html);
 
 
 $body = $mime->get();
 $hdrs = $mime->headers($headers);
 $mail = Mail::factory('smtp', array(
 'host' => 'localhost',
 'port' => '25',
 'sendmail_path' => '/usr/lib/sendmail',
 'ssl' => false
 ));
 
 $result = $mail->send($recipients, $hdrs, $body);
 $caller = $_POST['caller'];
 if (PEAR::isError($mail)) {
 echo ("<p>" . $mail->getMessage() . "</p>");
 header("Location: $caller");
 } else {
 header("Location: $caller");
 }
 
}


?>

So if we study line by line we will find the above code snippet depends on PHP Pear Package Link

Download them to your system and use there functionality to send mail.

To see the live form in action , take some of your crucial time to visit Link

At last but not the least if you have problem in understanding blog content kindly post your query in simple google form : at Blog Enquiry Form

Adding Custom Template to WordPress Page For your Website

Dear Friends This blog post is targeted for the individuals who aspire to become an entrepreneur and want to develop their own web site...
user
59 sec read

FCM Push notification [ Server side] HTTP via PHP

Dear friends we will be having php code that can make http post request via curl to Firebase Push servers. this post request will...
user
35 sec read

Accessing data from custom DB Table

Dear Developer Accessing database to make a site dynamic is something that every PHP developer encounters with. But every time there are game changer...
user
25 sec read