How to send email using php?

0 votes
I have a website where i use php  and have WAMPSERVER installed. Now how can i have email functionality enable in my website?

So how can i do that?

thanxx in advanced!
Apr 1 in PHP by kartik
• 26,540 points
80 views

1 answer to this question.

0 votes

Hello @kartik

1.) Download PHPMailer, open the zip file and extract the folder to your project directory.

2) Rename the extracted directory to PHPMailer and write the below code inside of your php script (the script must be outside of the PHPMailer folder)

<?php
// PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\Exception;
// Base files 
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
// create object of PHPMailer class with boolean parameter which sets/unsets exception.
$mail = new PHPMailer(true);                              
try {
    $mail->isSMTP(); // using SMTP protocol                                     
    $mail->Host = 'smtp.gmail.com'; // SMTP host as gmail 
    $mail->SMTPAuth = true;  // enable smtp authentication                             
    $mail->Username = 'sender@gmail.com';  // sender gmail host              
    $mail->Password = 'password'; // sender gmail host password                          
    $mail->SMTPSecure = 'tls';  // for encrypted connection                           
    $mail->Port = 587;   // port for SMTP     

    $mail->setFrom('sender@gmail.com', "Sender"); // sender's email and name
    $mail->addAddress('receiver@gmail.com', "Receiver");  // receiver's email and name

    $mail->Subject = 'Test subject';
    $mail->Body    = 'Test body';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) { // handle error.
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

Hope this works 

Thank You!

answered Apr 1 by Niroj
• 57,260 points

Related Questions In PHP

0 votes
1 answer

How to zip a whole folder using PHP?

Hello @kartik, You can Zip a whole folder using ...READ MORE

answered Aug 24 in PHP by Niroj
• 57,260 points
27 views
0 votes
1 answer

How to post data in PHP using file_get_contents?

Hello @kartik, Sending an HTTP POST request using file_get_contents is ...READ MORE

answered Aug 24 in PHP by Niroj
• 57,260 points
39 views
0 votes
1 answer

How to get name of current directory using php?

Hello @kartik, To get only the name of ...READ MORE

answered Aug 27 in PHP by Niroj
• 57,260 points
59 views
0 votes
1 answer

How to delete all files from a folder using PHP?

Hello @kartik, Use this: $files = glob('path/to/temp/*'); // get ...READ MORE

answered Sep 13 in PHP by Niroj
• 57,260 points
17 views
0 votes
1 answer

How can we Create Multiple Where Clause Query Using Laravel Eloquent?

Hii, You can use Conditions using Array: $users = User::where([ ...READ MORE

answered Mar 30 in Laravel by Niroj
• 57,260 points
2,181 views
0 votes
1 answer

Where to register Facades & Service Providers in Lumen?

Hello, To register a facade with an alias, ...READ MORE

answered Apr 6 in Laravel by Niroj
• 57,260 points
387 views
0 votes
1 answer

How can I update NodeJS and NPM to the next versions?

Hello @kartik, First check your NPM version npm -v 1).Update ...READ MORE

answered May 5 in Java-Script by Niroj
• 57,260 points
74 views
0 votes
1 answer

Switch statement for string matching in JavaScript

Hello @kartik, Just use the location.host property switch (location.host) ...READ MORE

answered Jun 2 in Java-Script by Niroj
• 57,260 points
60 views
0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27 in PHP by Niroj
• 57,260 points
90 views
0 votes
1 answer

How can I connect to a Tor hidden service using CURL in PHP?

Hello @kartik, I use Privoxy and cURL to scrape Tor ...READ MORE

answered May 19 in PHP by Niroj
• 57,260 points
493 views