• Page:
  • 1

send mail through php script on localhost


send mail through php script on localhost

2 years, 7 months ago
Hiii All,
I wanted to send mail through PHP script. It was not happening from localhost. But Today finally I found a code using that we can send e-mail with our 'gmail' account through PHP script from localhost. Below code is working well in 'joomla'. The code is-

$mail = new PHPMailer;
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';                       // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'yourname@gmail.com';               // SMTP username
$mail->Password = 'yourpassword';                     // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
$mail->Port = 587;
                                                      //Set the SMTP port number - 587 for authenticated TLS
$mail->setFrom('yourid@gmail.com', 'Yourname');       //Set who the message is to be sent from

//$mail->addReplyTo('labnol@gmail.com', 'First Last');//Set an alternative reply-to address

$mail->addAddress('to@gmail.com', 'yourname');        // Add a recipient

//$mail->addAddress('ellen@example.com');             // Name is optional

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the message body in bold!';
if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
echo 'Message has been sent';

Last Edit: 2 years, 7 months ago by Vicky Jain.

Re: send mail through php script on localhost

2 years, 7 months ago

In case of Joomla websites, you have this in the global configuration page too under Email settings, just configure there and you will be able to send emails from localhost through gmail

Alternatively, you can also set these in your php.ini file so that whenever you call mail() function, it will automatically pickup the settings from php.ini and send the email through gmail. Refer this link for more information:


Error in sending mail through php script using gmail account

2 years, 5 months ago
Hi All,

I was trying to send mail using php code with gmail account credentials. I was getting error mentioned in screenshot-


For overcome this problem goto below mentioned link when you are login in your gmail account-


And turn on the access for less secure apps


Now you can send mail through your gmail credentials with php script.

The following user(s) said Thank You: Farhan Sahibole, Pawan Priyadarshi
  • Page:
  • 1

Upcoming Events

Who's Online