How to send email in php from localhost?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amit08
    New Member
    • Mar 2015
    • 3

    How to send email in php from localhost?

    i am a beginner in php. i am making a small email sending script in php but no success till now. i am using XAMPP server. tried all the configurations settings and also with mercury email server in xampp but no success. Please help!!
  • Exequiel
    Contributor
    • Jul 2012
    • 288

    #2
    can you post your code on how you made your email sending?

    Comment

    • amit08
      New Member
      • Mar 2015
      • 3

      #3
      Actually my concern is of sending mail to any of email client(e.g. yahoo or gmail) through my localhost. I am using xampp server. I did settings in php.ini and sendmail.ini but no success. Any how i am sending you my php code and i expect if you know any configuration settings of php.ini and sendmail.ini please suggest me.
      Code:
      <?php
      $customeremail = $_POST['customeremail'];
      $message = $_POST['message'];
      $replywanted = false;
      if(isset($_POST['replywanted']))
      	$replywanted = true;
      	
      //build the text of email
      
      $t = "you have received the message from ".	$customeremail . ":\n";
      $t = $t. $message. "\n";
      
      if($replywanted)
      	$t = $t . "A reply was requested";
      else
      	$t = $t . "No reply was requested";
      	
      $headers = "From: sender@gmail.com";	
      
      //send an email 
      $isSent =  mail("receiver@gmail.com", "Customer Message", $t, $headers);
      
      if($isSent)
      	echo "thanks for sending the message";	
      else
      	echo "not sent";	
      
      ?>

      Comment

      • jaroc
        New Member
        • Apr 2015
        • 1

        #4
        Some trick I have summarize

        I am sorry, but some comments are in french.
        Also, I have tested it with gmail and not yahoo.
        I
        Code:
        <?php
        /* Setup email
        
        Under PHP in XAMPP tool
        We must configure XAMPP parameters
        
        The email server used to generate email is: gmail
         You have to open and activate a parameter in google gmail, https://www.google.com/settings/security/lesssecureapps and select "activate"
         (WARNING: this is not dramatic, because to use this server you must define the password in XAMPP.
         
         In the XAMPP tool
        C: \ xampp \ sendmail edit "sendmail.ini"
        Add two fields to the email address of your gmail account and password (WARNING: If this is not your PC that is dangerous)
        	auth_username=xxxx@gmail.com
        	AUTH_PASSWORD = yyyy
        Change the address and port for SMTP "smtp.gmail.com" and "587"
        	smtp_server = smtp.gmail.com
        	smtp_port = 587
        
        C: \ xampp \ php edit "php.ini"
        Remove the ";" before "sendmail_path =" \ "C: \ xampp \ sendmail \ sendmail.exe \" -t "
        Add the ";" before sendmail_path = "C: \ xampp \ mailtodisk \ mailtodisk.exe"
         
        !! it is mandatory to define a header "FROM" format xxx@yyy.zzz it is not necessary that this be your gmail address
         
         The appeal of this test is: http: // http: //localhost/yourdirectory/testemail.php
        
        */ 
        ?>
        
        <h1> test d'émission d\'email sous XAMPP </h1>
        <div>
        	<form method="post" accept-charset="utf-8" >  
                <label for="to">Email</label>
        			<input type="email" name="to" id="to" value="<?php if (isset($_POST['to'])) echo $_POST['to']; ?>" />
        		<div>
        		<label for="subject">Sujet</label>
        			<input type="text" name="subject" id="subject"  value="<?php if (isset($_POST['subject'])) echo $_POST['subject']; ?>" />
                </div>
        		<div>
        		<label for="message">Message (format HTML)</label>
        			<div>
                    <textarea name="message" id="message" rows="20" cols="50"><?php if (isset($_POST['message'])) echo $_POST['message']; ?></textarea>
        			</div>
        		</div>
        		<div>
        		<input type="submit" name="action" value="Envoyer" />
        		</div>
            </form>
        </div>
        
        <?php
        //print_r($_POST);
        
        if (isset($_POST['action']) && $_POST['action'] == 'Envoyer') {
            $send_error = "";
            $required_fields = array('to', 'subject', 'message');
        	
            foreach ($required_fields AS $v) {
        	//	echo 'champs : '.$v. ' valeur : ' .$_POST[$v].'<br>';
                if (empty($_POST[$v])) { $send_error = "Un ou plusieurs champs sont vides, veuillez vérifier le formulaire."; }
            }
            if (empty($send_error)) {
                $headers = "FROM:a@b.c";
                //$headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL;
        		extract($_POST);					// pas nécessaire ici , cela vérifie le contenu de $_POST 
        		echo 'Email bien formulée et soumise avec les paramètres : <br>TO : ' .$to. '<br>SUBJECT : ' .$subject. '<br>HEADER : '. $headers. '<br>MESSAGE : '  .$message. '<br>';
                if (!mail($to, $subject, $message, $headers)) { $send_error = "Erreur lors de l'envoi de l'email : voir les détails sous C:\xampp\sendmail\error.log";}	
                //if (!true) { $send_error = "Erreur lors de l'envoi de l'email : voir les détails sous C:\xampp\sendmail\error.log";}	
        	}
        	else { 
        		echo '<div>Erreur : ' . $send_error . '</div>';
        	}
        }
        ?>
        [/QUOTE]

        Comment

        • amit08
          New Member
          • Mar 2015
          • 3

          #5
          The email is still not received by the recipient. The error in error log file was Connection Closed Gracefully. I got this error many times before but failed to resolve. Please help.

          Comment

          Working...