how to send email in php using localhost in mail configuration by XAMPP/LAMP/WAMP server?

how to send email in php using localhost – This is a very important part of the mail() function in PHP. PHP’s mail() function sends plain text, HTML or some attachments messages.

We can send mail from our localhost using some mail configuration by XAMPP/LAMP/WAMP server,

how to send email in php using localhost?

Hello friends, in today’s post, you are being told about php email sending in hindi, what happens, how it works, so let’s start

If you try to send email from local host then you will show failed to connect mail server error as I told you earlier that to send email only live server is required so you have to send emails from php To do this only a live server will be required

If after you connect to the live server, you will not send the email due to any error, then the echo statement of the else part will also be executed, otherwise the echo statement of the if part will be executed.

how to send email in php using localhost?

first fo all Import PHPMailer classes into the global namespace, and then Set mailer to use SMTP, Specify main and backup SMTP servers, Enable SMTP authentication, SMTP username & password, Enable TLS encryption, `ssl` also accepted , TCP port to connect to , set Sender info, Add a recipient, Set email format to HTML, Mail subject, Mail body content and Send email for how to send email in php using localhost.

Code for sending email from localhost : Here is the Full code with HTML Form and PHP Code

index.php

<?php 
  
use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\Exception; 
 
require 'PHPMailer/Exception.php'; 
require 'PHPMailer/PHPMailer.php'; 
require 'PHPMailer/SMTP.php'; 
 
$sendMail = new PHPMailer; 
 
$sendMail->isSMTP();                      
$sendMail->Host = 'smtp.gmail.com';       
$sendMail->SMTPAuth = true;               
$sendMail->Username = '[email protected]';    
$sendMail->Password = 'gmail_password';  
$sendMail->SMTPSecure = 'tls';            
$sendMail->Port = 587;                     
 
$sendMail->setFrom('[email protected]', 'Pakainfo'); 
$sendMail->addReplyTo('[email protected]', 'Pakainfo'); 
 
$sendMail->addAddress('[email protected]_name.com'); 
 
//$sendMail->addCC('[email protected]_name.com'); 
//$sendMail->addBCC('[email protected]_name.com'); 
 
$sendMail->isHTML(true); 
 
$sendMail->Subject = 'Email from Localhost by Pakainfo'; 
 
$bodyContent = '<h1>How to Send Email from Localhost using PHP by Pakainfo</h1>'; 
$bodyContent .= '<p>This HTML email is sent from the localhost server using PHP by <b>Pakainfo</b></p>'; 
$sendMail->Body    = $bodyContent; 
  
if(!$sendMail->send()) { 
    echo 'Message could not be sent. Mailer Error: '.$sendMail->ErrorInfo; 
} else { 
    echo 'Message has been sent.'; 
} 
 
?>

Add attachments

Set file path to addAttachment() method.

Read Also:  Get Table name From Model using Laravel 5.7 Example

$sendMail->addAttachment('/var/tmp/file.tar.gz');
$sendMail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name

Introduction to PHP email sending

PHP provides you many important features which is one such important feature of php is that you can also send emails through php, email is a simple and effective medium to interact with customers.

Through email, you are personally connected to brands and any customers in the organization, as well as you are used only to send any kind of information to the people directly related to the email.

This feature of php is very useful to you, you can use it in many ways, for example, you create a form in your website, which along with other information receives their email id from the users.

After submitting the user’s form, you can dynamically send an email through php, in which the user can be called welcome only for providing their information.

Similarly, suppose that you have data of more than 10000 customers and your organization has launched a new product, then if you want to tell all the customers about this product through email, then you do not need it. You can send email in one go only by accessing the email id database of all customers through php.

how to send email in php using localhost
how to send email in php using localhost

Through php, you can send not only text emails but also HTML emails

A live server is required to send email through php. You cannot send email from local host. Before sending email, you should also check that your php.ini file is sent to emails. configured to do or not

To send email, you must set smtp server and sendmail_from attribute in [mail function] section of php.ini file smtp server tells your email server and sendmail_from tells that email id from which to send email will go

You can set these attribute through int_set() function, in this function two arguments are passed, first argument is the name of the attribute and the second argument is of its value, through this function to set the attribute of sendmail_from The example is being given to you below

Read Also:  limit excerpt length in wordpress Example

int_set(“sendmail_from”,”[email protected]”);

php mail() function

To send mail from localhost, php provides you the build in main() function, which is given below in the general syntax of this function.

bool main(string $to,string $subject,string 
$message,string $additional_header,string 
$additional parameters)

As you can see in the above syntax that the mail() function returns a bool value, you can use it in the if statement and find out whether the mail was sent or not.

In php mail() function 5 arguments are passed, you can pass these arguments directly in mail() function or you can store them in variable and variable can also be passed in main() function

You are being given below about the argument to be passed in the php mail() function

to

This is the email id of the receiver to which you can send mail.

subject
This is the subject of the email being sent

message
This is the message that you want to send via email.

additional_headers(optional)

This argument is used to add additional headers. Additional headers are shown below the email header that you pass information like FROM, CC and BCC through additional headers.

additional_parameters(optional)
Through this argument additional flags are passed to the program sending the mail.

example
The simple example of sending you email through php is being given to you below.

What is Cc and Bcc ?

Cc: Message is sent to more than one email id from Cc(Carbon copy). The one who is the receiver sees all the email ids of Cc.

Bcc: Message is sent from Bcc(Blind carbon copy) to more than one email id. But the one who is the receiver does not see the email id of Bcc.

Send Email using HTML Message

<?php
$from = "[email protected]"
$to = "[email protected]";
$subject = "Welcome To Pakainfo.com";  
$message = "<h1>Great, Thanks for sharing</h1>";  
$header = "From : ".$from."\r\n";  
$header .= "MIME-Version : 1.0 \r\n";  
$header .= "Content-type : text/html \r\n";  
  
if(mail ($to,$subject,$message,$header)){
    echo "Good Luck, Email sent successfully.";  
}
else{  
    echo "Sorry, Email cannot sent.";  
}  
?>  

In the above example, the mail() function has been called in the if statement that in this example the variable has been passed by not passing the values directly in the mail() function, in these variables you will have the values which the user will fill in the form.

Read Also:  How to make the number pyramid program in php?

Example for Sending Email Without headers From, Cc and Bcc with Form

<?php
if (isset($_POST['email'])){

$from = "[email protected]";
$to = $_POST['email'];
$subject = $_POST['subject'];
$comment = $_POST['comment'];
$headers = 'From: <'.$from.'>'. "\r\n";
$headers .= 'Cc: '.$_POST['Cc']. "\r\n";
$headers .= 'Bcc: '.$_POST['Bcc']. "\r\n";

mail($to, $subject, $comment, $headers);
	echo "Email sent successfully.";
}
else{
?>
<form method="post">
Email: <input name="email" type="text" /><br />
Subject: <input name="subject" type="text" /><br />
Cc : <input name="Cc" type="text" /><br />
Bcc : <input name="Bcc" type="text" /><br />
Message:<br />
<textarea name="comment" rows="20" cols="50"></textarea><br />
<input type="submit" value="Submit" />
</form>  
<?php
}
?>

Note: Every single email in Cc and Bcc is separated by , (comma).

Example for Sending Email Without headers

<?php
$from = "[email protected]";
$to = "[email protected]";
$subject = "Great Pakainfo.com";
$message = "Free Download Source code";

if(mail($to, $subject, $message)){
	
	echo "Email sent successfully.";
}
else{
	echo "Email cannot sent.";
}
?>

I hope you get an idea about how to send email in php using localhost?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.