0

Error shown in the result is

Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\elvisstore\sendemail.php on line 17

sendmail.php file is like this:

<?php
$from = 'something@gmail.com';
$subject =$_POST['subject'];
$text =$_POST['elvismail'];
$dbc= mysqli_connect( 'localhost' , 'smile' ,'password','elvis_store') 
or die('error in connecting to database');

$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc,$query) or die('error in querying data');
//$row = mysqli_fetch_array($result);
while($row=mysqli_fetch_array($result))
{
   $first_name = $row['first_name'];     
   $last_name = $row['last_name']; 
   $msg = "Dear $first_name $last_name, \n  $text" ;
   $to =$row['email']; 
   mail($to, $subject, $msg, 'FROM:' . $from);
   echo "Email sent to $to <br>";
}
  mysqli_close($dbc);
?>

sendemail.html file

<form method="post" action="sendemail.php"> 

    <label for="subject">Subject of email:</label><br /> 
        <input type="text" id="subject" name="subject" size="60" /><br />     
        <label for="elvismail">Body of email:</label><br />     
        <textarea id="elvismail" name="elvismail" rows="8" cols="60"></textarea><br />     
        <input type="submit" name="submit" value="Submit" />   
    </form> 
</body> 

I think this error came due to i am using localhost as my server.please tell me what's going on?

  • Please refer to this other post, this should give you your answer. http://stackoverflow.com/questions/24644436/php-mail-form-doesnt-complete-sending-e-mail – Scriptable Dec 31 '14 at 11:27

1 Answers1

0

Do you have any mail server installed on your localhost or are you trying to send through some external mail server?

You need to add this piece of code to the top of your php page:

ini_set('SMTP','myserver');
ini_set('smtp_port',25);

Where you replace 'myserver' with the ip-address of the SMTP server you want to use, as well as use the correct port for the given server.

mediasurface
  • 47
  • 1
  • 11
  • I am doing all these in offline mode with Wamp software bundle. According to you , I have to paste these code and replace myserver with localhost but don't know the port no. –  Dec 31 '14 at 11:33
  • The port number depends on your SMTP server. If you want to run this locally, I'd recommend using something like http://mailcatcher.me/ – mediasurface Dec 31 '14 at 12:53