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 = "[email protected]"; // SMTP username
$mail->Password = "zzz"; // SMTP password
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;//587;
$mail->AddAddress("xxx", "xx");
$mail->SetFrom('[email protected]','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,
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 = '[email protected]'; // SMTP username
$mail->Password = 'mandrilapp_will_give_you_a_password'; // SMTP password
$mail->Port = 587; // TCP port to connect to
$mail->From = '[email protected]';
$mail->FromName = 'Test phpmailer';
$mail->addAddress('[email protected]'); // 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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With