4

Here is code for sending email from localhost after i referred a lot from online.

html form:

<form method="post" action="email.php">
  Email: <input name="email" id="email" type="text" /><br />

  Message:<br />
  <textarea name="message" id="message" rows="15" cols="40"></textarea><br />

  <input type="submit" value="Submit" />
</form>

email.php:

<?php

// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

require_once('class.phpmailer.php');
require 'PHPMailerAutoload.php';
require 'class.smtp.php';

$mail = new PHPMailer();
$body='hellooooo';
$mail->IsSMTP();

$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server

$mail->SMTPAuth = true; // turn on SMTP authentication

$mail->Username = "xxxx@gmail.com"; // SMTP username
$mail->Password = "zzz"; // SMTP password
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;//587;
$mail->AddAddress("xxx", "xx");
$mail->SetFrom('xxx@gmail.com','xxxx');
$mail->WordWrap = 50;

$mail->IsHTML(true);

$mail->Subject = "You have received feedback from your website!";

$mail->MsgHTML($body);

if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

echo "Message has been sent";
?>

so when i run my code it shows error like this,

Message could not be sent.

Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

And i removed semicolon in this line ;extension=php_openssl.dll from the following files, and restart the xampp.

c/xampp/apache/bin/php.ini and c/xampp/php/php.ini

still stays same error..

Note: I m new to php, but i want to know particular this one and fix the problem. and I referred similar questions in stack, but it didn't help me,

Can anybody help me to fix this?

Thanks,

Community
  • 1
  • 1
pcs
  • 1,864
  • 4
  • 25
  • 49

1 Answers1

3

It looks like your credentials for connecting to your authentication has failed. I often send mail from my local and I found it that it's a LOT easier to use another SMTP than gmail, like mandrillapp, free until 12,000 mails. There are a lot of things that I don't understand in your code so I will share mine.

<?php 

require 'PHPMailer-master/PHPMailerAutoload.php';

$mail = new PHPMailer;
//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'your@username.com';                 // SMTP username
$mail->Password = 'mandrilapp_will_give_you_a_password';                           // SMTP password
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = 'your@email.com';
$mail->FromName = 'Test phpmailer';
$mail->addAddress('who_are_you_sending@to.com');               // Name is optional

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

Be sure to have PHPMailer-master folder (you can download it from here) at the same level as your php file. This is how I link phpmailer. Hope it helps, if you have any questions, ask me!

Matthieu Boisjoli
  • 1,057
  • 2
  • 11
  • 17
  • 1
    The only file you need is this one : PHPMailerAutoload.php. This file will autoload every file you need. – Matthieu Boisjoli May 30 '15 at 04:27
  • Did you sign up to mandrillapp and put your credentials (username, password) according to your account? Don't forget to active a API key, which is your password. The only thing to change in my code is Username, Password and addAddress to your recipient (test with your own e-mail) – Matthieu Boisjoli May 30 '15 at 04:31
  • Sure, it's because I never get it work with gmail smtp. I had same problem as you, so I tried mandrillapp which is free until you send 12,000 mails and give you a smtp, username and password. It's working great to test mails on local. – Matthieu Boisjoli May 30 '15 at 04:35
  • When you are in mandrillapp, go in settings, click on "new API key", then put it to "on" or activate, whatever it is, and your API key is your password to put in $mail->Password = 'you_api_key'; – Matthieu Boisjoli May 30 '15 at 04:42
  • okay thanks, it works.. – pcs May 30 '15 at 08:39
  • hi ..@Matthieu Boisjoli... – pcs Jun 01 '15 at 09:53