Sending Emails With PHP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Congiure XAMPP to send mail from localhost using PHP

To configure XAMPP server to send mail from localhost we have to make changes
to two files
1) sendmail.ini and 2) php.ini.

Open the xampp folder. By the name of “sendmail.ini” is present in sendmail file
and “php.ini” is present in php file in the xampp folder.

Step 1:
 Go to C:\xampp\sendmail: open sendmail.ini file in notepad or any
text editor and make the changes as follows.

change smtp_server=mail.yourdomain.com to smtp_server=smtp.gmail.com


change smtp_port to smtp_port=587
change smtp_ssl=auto to smtp_ssl=tls


uncomment ;error_logfile=error.log to error_logfile=error.log
uncomment ;debug_logfile=debug.log to debug_logfile=debug.log
write your gmail id in auth_username: auth_username=*****@gmail.com
write your gmail password in auth_password: auth_password=*****
write your gmail id in force_sender: *****@gmail.com
change hostname to hostname=localhost

Step:2
 Go to C:\xampp\php: open php.ini file in notepad or any text editor
goto [mail function] part and make the changes as follows.

comment SMTP=localhost by putting semicolon infront=>;SMTP=localhost


comment smtp_port=25 by putting semicolon infront=>;smtp_port=25
comment sendmail_from= by putting semicolon infront=>;sendmail_from=

specify path of file in sendmail_path to


sendmail_path=C:\xampp\sendmail\sendmail.exe

check if extension=php_openssl.dll is enabled=>If there is semicolon


in front then
un-comment it by removing that semicolon

After following the given steps if the mail is not sent by calling the mail function,
then goto C:\xampp\sendmail open error.log to see the error occurred.
Note: Here the procedure is shown for gmail, but it can be extended to other
mails by changing the smtp server, port number. When using gmail take care to
enable the option to allow access to less secure webapp.

Set your Email Account(gmail.com):-


The password for email will not work here directly. We have to enable 2 step authentication in Gmail
account settings and generate a password for Apps.

Goto Account Settings.


Then Select Security Option and Set 2-Step Verification

And then generate an App Password to use with PHP code to send emails.

Program Code to Send Emails:- Using native mail() function of PHP


<?php
$to = "[email protected]";
$subject = "Testing Email function in PHP";
$txt = "This is my message sent to Tannuj";
//$headers = "From: [email protected]" . "\r\n" ."CC:
[email protected]";

//mail($to,$subject,$txt); //,$headers);
if (mail($to, $subject, $txt)) {
echo "Email successfully sent to $to ...";
} else {
echo "Email sending failed...";
}
?>

Program Code to Send Emails(Using PHPMailer Library of PHP):- You have to


install PHPMailer Library using Composer in XAMPP environment.

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com;';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'qzzspjdrseuibnda';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]');
//$mail->addAddress('[email protected]', 'Name');
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = 'HTML message body in <b>bold</b> ';
$mail->AltBody = 'Body in plain text for non-HTML mail clients';
$mail->send();
echo "Mail has been sent successfully!";
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

?>
How to configure XAMPP to send mail from localhost using PHP ? - GeeksforGeeks

How to send an email using PHPMailer ? - GeeksforGeeks

Send Mail From Localhost in PHP Using XAMPP - PHPCODER.TECH

How To Send Email From Localhost Using PHP (phpflow.com)