March 12, 2012

Email Sending from Local Server in PHP

Question by Samir

I have my file mail.php :

<html>
<body>

<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST['email'] ;
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail("syedsamiruddin29@gmail.com", $subject,
  $message, "From:" . $email);
  echo "Thank you for using our mail form";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='mail.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>

</body>
</html>

but when i run it. i get this error :

Warning: mail() [function.mail]: Failed to connect to mailserver at "127.0.0.1" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:Program FilesEasyPHP-5.3.8.1wwwsafepay247mail.php on line 13
Thank you for using our mail form

what im trying to do is to send an email to my email id : syedsamiruddin29@gmail.com using php to the person’s email written in the input type.

Ive gone through many videos but none help me solve the php.ini problem.

Answer by Starx

Its not that complicated

  1. Open the php.ini.

  2. Search for the attribute called SMTP in the php.ini file. Generally you can find the line SMTP=localhost. change the localhost to the smtp server name of your ISP. And the set the port of 25.

    SMTP = smtp.gmail.com #user your smtp address here
    smtp_port = 25
    
  3. Restart the apache server or wamp or xammp. Whatever you are using.

  4. Now try to send the mail using the mail() function

March 2, 2012

PHP mail function interrupting jquery .post return data

Question by SPI

I don’t really understand what’s going on here. I have a jquery script that sends form post data to a signup script, where upon an email is sent to the user signing up and the users Id is returned back to the calling jquery function where it is forwarded to yet another script for processing.

Problem: The script works great if I leave out the mail function, however, introducing it into the script blocks the variable from being returned to the calling jquery function. Any ideas what’s going on? Here is the Jquery script:

<script>
/* attach a submit handler to the form */
$("#purchaseForm").submit(function(event) {

    var intRegex = /^d+$/;


/* stop form from submitting normally */
event.preventDefault(); 


    $.post("process_signup.php", $("#purchaseForm").serialize(),function(data){
                                                if(intRegex.test(data)){
                    if(data!=0){
                        alert(data);
                    document.getElementById("bag_contents_hidden").value=data;  
                    $.post("process_order.php", $("#bag_contents").serialize(), function(data2){
                    if(data2){
                    window.location.replace("?process=100&success=1");  
                    }else{
                    window.location.replace("?process=100&fail=1"); 
                    }
                });

            }
        }

else{       //Not Important, validation stuff       


}



        });

  });

//

PHP 
                      if($oid){
               if(mail($email,$title,$message,$headers,'O DeliveryMode=b')){

                    echo $oid;
                    unset($_POST['processorder_hidden']);
                    }else{
                    echo 0; 
                          }
                }

Answer by Starx

Since you are expecting a data from the post request any errors generated by mail function is sure to create problems.

Here is a correct way to solve it

$status = @mail($to, $subject, $content); //suppress the error generated from being passed to the view

//instead handle it later
if($status) {
   //handle success
} else {
  //handle unsuccess
}
...

Please fill the form - I will response as fast as I can!