SitePoint Sponsor

User Tag List

Results 1 to 25 of 27

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2013
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    HTML5 form using PHP, email not sending

    Hello All,

    I have created the following HTML5 form (some wording changed for security reasons), I am posting to test.php. I go over to XAMPP and launch my Form which in turn goes to PHP code. I receive message 'Thank you, your request has been sent!' but no email is being received. I am not sure what the problem is - my server is setup for SMTP port = 25 and my domain is company name. Any thoughts? Code below...

    HTML Code:
    <head>
    <meta charset="UTF-8">
    <title>Transfer Form</title>
    <link href="css/goods.css" rel="stylesheet" type="text/css">
    
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    
    <script type="text/javascript">
    $(document).ready(function() {
        $('a').click(function() {
            $('#myTable tbody').append('<tr class="child"><td><input type="text" name="stockNumber" class="stocktext"  id="stockNumber" /></td><td><input type="text" name="serialNumber" class="serialtext" id="serialNumber" /></td><td><input type="text" name="description" class="descripttext" id="description"/></td></tr>');
        
    });
    });
    </script>
    
    <script>
      $(function() {
        $( "#datepicker" ).datepicker();
      });
      </script>
    </head>
    
    <body>
    
    <form method="POST" action="test.php"> 
    
    <fieldset>
    <legend class="legend"> Transfer Form</legend>
    
    <p class="currentdate">Current Date: 
    <script type="text/javascript">
    <!--
    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    document.write(month + "/" + day + "/" + year)
    //-->
    </script></p>
    
    <p>
    <table class="table">
        <tr>
          <td><span class="requiredtable">*</span> <span class="fieldtext">Stock Number</span></td>
          <td><span class="requiredtable">*</span> <span class="fieldtext">Serial Number</span></td>
          <td class="descripttd"><span class="descriptiontxt"> Description</span></td>
        </tr>
      </table>
      
      <table id="myTable">
      <tbody>
        <tr>
          <TD> <input type="text" name="stockNumber" required class="stocktext"  id="stockNumber" tabindex="1" /> </TD>
                
          <TD> <input type="text" name="serialNumber" required class="serialtext" id="serialNumber" tabindex="2" /> </TD>
                
          <TD> <input type="text" name="description" class="descripttext" id="description" tabindex="3" /> </TD>
        </tr>
      </tbody>
    </table> 
        
     <p><span class="required">*
        </span><span class="fieldtext">Requested by:
        </span>
        <input type="text" name="Requested_by" required id="Requested_by" size="50" maxlength="500" tabindex="4">
            
          <span class="requestdate"><span class="required">*</span> Requested Date:
    <input type="text" name="requestedDate" required id="datepicker" value="Click to Select Date" tabindex="5"></span>
      </p>
        
      <p><span class="required">*</span>
      <span class="fieldtext">Customer Info:</span>
            <input type="text" name="Cust Info" required id="Cust_Info" size="50" maxlength="500" tabindex="6">
      </p>
    
     <label for="RequestingBranch"> 
         <span class="required">*</span>
         <span class="fieldtext"> Requesting Branch:</span>
        </label>
        
    <select class="select" name="R_branch" id="R_branch">
    <option value="">Select One:
    <option value="R_boston">Boston
    <option value="R_Buffalo">Buffalo
    <option value="R_Cinncinatti">Cinncinatti
    <option value="R_columbia">Columbia
    <option value="R_Dallas">Dallas
    <option value="R_Fairfax">Fairfax
    <option value="R_Kansas City">Kansas City
    <option value="R_Los Angeles">Los Angeles
    <option value="R_Orlando">Orlando
    <option value="R_Raleigh">Raleigh
    <option value="R_Toledo">Toledo
    <option value="R_Topeka">Topeka
    </select>
     
        <label for="ShippingBranch">
         <span class="required">*</span>
         <span class="fieldtext"> Shipping Branch:</span>
        </label>
      
       <select class="select" name="S_branch" id="S_branch">
    <option value="">Select One:
    <option value="S_boston">Boston
    <option value="S_Buffalo">Buffalo
    <option value="S_Cinncinatti">Cinncinatti
    <option value="S_columbia">Columbia
    <option value="S_Dallas">Dallas
    <option value="S_Fairfax">Fairfax
    <option value="S_Kansas City">Kansas City
    <option value="S_Los Angeles">Los Angeles
    <option value="S_Orlando">Orlando
    <option value="S_Raleigh">Raleigh
    <option value="S_Toledo">Toledo
    <option value="S_Topeka">Topeka
      </select>
        <br> <br>
     <input type="submit"> 
          
        <input name="resetbutton" type="reset" id="resetbutton" value="Clear"></p>
        
      </fieldset>
    </form>
    
    </body>
    PHP Code:
     <?php 
    sleep
    (2);

    //Sanitize incoming data and store in variable

    $stockNumber =  trim(stripslashes(htmlspecialchars ($_POST['stockNumber'])));        
    $serialNumber =  trim(stripslashes(htmlspecialchars ($_POST['serialNumber'])));    
    $description =  trim(stripslashes(htmlspecialchars ($_POST['description'])));    
    $requestedBy trim(stripslashes(htmlspecialchars ($_POST['requestedBy'])));
    $requestedDate =  trim(stripslashes(htmlspecialchars ($_POST['requestedDate'])));    
    $customerInfo =  trim(stripslashes(htmlspecialchars ($_POST['customerInfo'])));    
        
    // Array for the R_emails option from form
    $R_emails = array(
        
    'R_boston' => 'boston@test.com'
        
    'R_buffalo' => 'buffalo@emailhere.com',
        
    'R_cinncinatti' => 'cinncinatti@email.com'
        
    'R_columbia' => 'columbia@test.com',
        
    'R_dallas' => 'dallas@emailhere.com',  
        
    'R_fairfax' => 'fairfax@emailhere.com'
        
    'R_kansas' => 'kansas@email.com'
        
    'R_la' => 'la@emailhere.com',
        
    'R_orlando' => 'orlando@email.com'
        
    'R_raleigh' => 'raleigh@emailhere.com',
        
    'R_toledo' => 'toledo@emailhere.com',
        
    'R_topeka' => 'topeka@emailhere.com'
    );

    // get receiving email and turn in the the R_email variable
    $R_email $R_email$_POST['R_branch'] ];    

    // Array for the S_emails option from form
    $S_emails = array(
        
    'S_boston' => 'boston@test.com'
        
    'S_buffalo' => 'buffalo@emailhere.com',
        
    'S_cinncinatti' => 'cinncinatti@email.com'
        
    'S_columbia' => 'columbia@test.com',
        
    'S_dallas' => 'dallas@emailhere.com',  
        
    'S_fairfax' => 'fairfax@emailhere.com'
        
    'S_kansas' => 'kansas@email.com'
        
    'S_la' => 'la@emailhere.com',
        
    'S_orlando' => 'orlando@email.com'
        
    'S_raleigh' => 'raleigh@emailhere.com',
        
    'S_toledo' => 'toledo@emailhere.com',
        
    'S_topeka' => 'topeka@emailhere.com'
            
    );

    // get receiving email and turn in the the S_email variable
    $S_email $S_emails$_POST['S_branch'] ];    

    //Prepare information from form to be sent 
    $to 'sparkymom78@gmail.com';
    $from 'sharepoint@company.com';
    $subject 'Online Order Request';
    $headers 'MIME-VERSION: 1.0' '\n';
    $headers .= 'From: $from' '\n';
    "CC: testing123@company.com";
    $body 'Stock Number: ' .$stockNumber PHP_EOL;
    $body .= 'Serial Number: ' .$serialNumber PHP_EOL;
    $body .= 'Description: ' .$description PHP_EOL;
    $body .= 'Requested By: ' .$requestedBy PHP_EOL;
    $body .= 'Requested Date: ' .$requestedDate PHP_EOL;
    $body .= 'Customer Info: ' .$customerInfo PHP_EOL;

    // Form data was successful so we will now send admin email and return message to the user
    $success mailimplode(',', array( $R_email$S_email )), $subject$body$headers '-f user123@company.com' ); 
    echo 
    'Thank you, your request has been sent!';    

    ?>
    Thanks in advance,
    SparkyMom

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,395
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi SparkyMom,

    I've got a recent copy of XAMMP on my machine, and it seems that by default it's setup to save emails to disk instead of sending them. Check for a folder, inside your XAMPP folder, called mailoutput.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2013
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do you get them out of Apache/Output email to recipient?

    Quote Originally Posted by fretburner View Post
    Hi SparkyMom,

    Check for a folder, inside your XAMPP folder, called mailoutput.
    Yeah, I just found them there. Now the question is how do I get them out of there? And have them actually email to correct recipients?

    Thanks,
    Sparkymom

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,395
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Open up php/php.ini and comment out this line:
    Code:
    sendmail_path = "\xampp\mailtodisk\mailtodisk.exe"
    and uncomment this one:
    Code:
    sendmail_path = "\"\xampp\sendmail\sendmail.exe\" -t"
    Then open up sendmail/sendmail.ini and fill in the details for an email account that you can use to send the email.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2013
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    Open up php/php.ini and comment out this line:
    Code:
    sendmail_path = "\xampp\mailtodisk\mailtodisk.exe"
    and uncomment this one:
    Code:
    sendmail_path = "\"\xampp\sendmail\sendmail.exe\" -t"
    Then open up sendmail/sendmail.ini and fill in the details for an email account that you can use to send the email.
    I have done what is suggested here and my emails are still getting hung up in the Apache/mailoutput folder.

    Enclosed are my php.ini and sendmail.ini - What do I need to do to get the emails to actually send?


    php.ini

    [mail function]
    ; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
    SMTP = ip address for exchange server here
    smtp_port = 25

    ; For Win32 only.
    ; http://php.net/sendmail-from
    sendmail_from = sharepoint@companyname.com

    ; XAMPP IMPORTANT NOTE (1): If XAMPP is installed in a base directory with spaces (e.g. c:\program filesC:\xampp) fakemail and mailtodisk do not work correctly.
    ; XAMPP IMPORTANT NOTE (2): In this case please copy the sendmail or mailtodisk folder in your root folder (e.g. C:\sendmail) and use this for sendmail_path.

    ; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
    sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

    ; XAMPP: Comment out this if you want to work with mailToDisk, It writes all mails in the C:\xampp\mailoutput folder
    ;sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"

    sendmail.ini

    [sendmail]

    ; you must change mail.mydomain.com to your smtp server,
    ; or to IIS's "pickup" directory. (generally C:\Inetpub\mailroot\Pickup)
    ; emails delivered via IIS's pickup directory cause sendmail to
    ; run quicker, but you won't get error messages back to the calling
    ; application.

    smtp_server=ip address for exchange server here

    ; smtp port (normally 25)

    smtp_port=25

  6. #6
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,395
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    If your emails are still ending up in the mailoutput folder, it sounds like you'll need to restart XAMPP for the changes in php.ini to take effect.

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    The standard mail function in php really sucks. In that it is quite limited in its capabilities and error handling. I would recommend using a library like SwiftMailer that provides an stmp transport. The stmp transport provides increasingly portable and predictable funcionality across different environments. Also, it provides very good feedback for errors and such.
    The only code I hate more than my own is everyone else's.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2013
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since I couldn't get email to send through PHP via SMTP by making changes to my php.ini and sendmail.ini, I decided to setup PHPMailer.

    After the install, I was able to successfully receive the test mail from PHPMailer BUT I'm not sure what all I need to do to get the email to send properly via my send.php code.

    ** I shouldn't have to have the form setup in the PHPMailer folder to work properly - right?

    My error messages:

    Warning: require(class.phpmailer.php): failed to open stream: No such file or directory in C:\xampp\htdocs\transferform\send.php on line 3

    Fatal error: require(): Failed opening required 'class.phpmailer.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\transferform\send.php on line 3

    Not sure what PEAR has to do with PHPMailer. I have PHPMailer installed in htdocs\PHPMailer

    PHP Code:
    //Top of my send.php code:

    <?php 
    sleep
    (2)/
    require(
    "class.phpmailer.php");

    $mail = new PHPMailer();

    $mail->IsSMTP();  // telling the class to use SMTP
    $mail->Host     "ip address"// SMTP server

    //bottom of code:
    $success mailimplode(',', array( $R_email$S_email )), $subject$body$headers'-f [email]user123@company.com[/email]' ); 
    echo 
    'Thank you, your request has been sent!';    
        
    ?>
    ** I tried to add the following statement
    require("php\PEAR\class.phpmailer.php");

    ** As a test, I copied class.phpmailer.php into my folder that contains my html form and send.php files and I didn't receive any error messages but I also didn't get the email.
    ARRRGGGHHH...


    Thanks

  9. #9
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,395
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    If your form is in the file C:\xampp\htdocs\transferform\send.php and you've got PHPMailer in C:\xampp\htdocs\PHPMailer, try changing the require call at the top of you form like this:
    PHP Code:
    require("../PHPMailer/class.phpmailer.php"); 
    Edit: What this does it tell require "go up one level from my current directory (transferform) and into the PHPMailer directory, and include the file class.phpmailer.php from there".
    Last edited by fretburner; Jul 3, 2013 at 07:30. Reason: Add an explanatory note

  10. #10
    SitePoint Enthusiast
    Join Date
    Jun 2013
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    If your form is in the file C:\xampp\htdocs\transferform\send.php and you've got PHPMailer in C:\xampp\htdocs\PHPMailer, try changing the require call at the top of you form like this:
    PHP Code:
    require("../PHPMailer/class.phpmailer.php"); 
    Edit: What this does it tell require "go up one level from my current directory (transferform) and into the PHPMailer directory, and include the file class.phpmailer.php from there".
    I have tried the following and keep getting errors:

    require("xampp/htdocs/PHPMailer/class.phpmailer.php");
    require("/PHPMailer/class.phpmailer.php");
    require("../PHPMailer/class.phpmailer.php");

    Do I need to make edits to the class.phpmailer?

    OR is this code supposed to go into my html form at the beginning?

    <?php
    sleep(2)/
    require("../class.phpmailer.php");

    $mail = new PHPMailer();

    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host = "ip address"; // SMTP server

    ?>

  11. #11
    SitePoint Enthusiast
    Join Date
    Jun 2013
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have this working without any errors

    require("../PHPMailer/class.phpmailer.php");

    but I'm still not receiving emails, they are going into the Apache/mailoutput folder

  12. #12
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,395
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by sparkymom7 View Post
    After the install, I was able to successfully receive the test mail from PHPMailer BUT I'm not sure what all I need to do to get the email to send properly via my send.php code.
    What did you do to receive the test email? Is it a script that's included with PHPMailer? If so, what folder did you run it from?

  13. #13
    SitePoint Enthusiast
    Join Date
    Jun 2013
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I created a test file in the PHPMailer folder.

    PHP Code:
    ?php

    require("class.phpmailer.php");

    $mail = new PHPMailer();

    $mail->IsSMTP();  // telling the class to use SMTP
    $mail->Host     "ipaddress"// SMTP server

    $mail->From     "sharepoint@company.com";
    $mail->FromName "user123";
    $mail->AddAddress("user123@company.com");

    $mail->Subject  "First PHPMailer Message";
    $mail->Body     "Hi! \n\n This is my first e-mail sent through PHPMailer.";
    $mail->WordWrap 50;

    if(!
    $mail->Send()) {
      echo 
    'Message was not sent.';
      echo 
    'Mailer error: ' $mail->ErrorInfo;
    } else {
      echo 
    'Message has been sent.';
    }
    ?> 

  14. #14
    SitePoint Enthusiast
    Join Date
    Jun 2013
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From everything I have Googled and read about you cannot have 2 php actions in a form. I don’t want my HTML code to be any longer than it currently is so I thought it might be a great idea to incorporate the original send.php with processform.php (validation code) .

    The problem is if my fields are empty when I submit the form I don’t receive any error messages and emails go out blank like so - and I receive Thank you message.
    Stock Number:
    Serial Number:
    Description:
    Etc.

    I tested my processform.php by itself for the first 5 fields, which are text fields and did receive errors when blank. However, when I added the drop downs I didn’t get errors.

    I thought why not try to merge the two php files together but that isn’t working either. Hmmmm – I’m at a loss and I can't seem to attach code, I keep getting 500 Internal error code.

  15. #15
    SitePoint Enthusiast
    Join Date
    Jun 2013
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php
    /*
    * BEGIN CONFIG - processform validation code
    */

    // The page you want the user to be redirected if there are no errors.
    $thankYouPage = 'thanks.html';

    // Define which values we are to accept from the form. If you add additional
    // fields to the form, make sure to add the form name values here.
    $allowedFields = array(
    'stockNumber',
    'serialNumber',
    'description',
    'requestedBy',
    'requestedDate',
    'custInfo',
    'R_branch',
    'S_branch',
    );

    // Specify the required form fields. The key is the field name and the value
    // is the error message to display.
    $requiredFields = array(
    'stockNumber' => 'Stock Number is required.',
    'serialNumber' => 'Serial Number is required.',
    'description' => 'Description is required.',
    'requestedBy' => 'Requested By is required.',
    'requestedDate' => 'Please select a Requested Date.',
    'custInfo' => 'Customer Information is required.',
    'R_branch' => 'Please select Requesting Branch.',
    'S_branch' => 'Please select Shipping Branch.',

    );

    /*
    * BEGIN FORM VALIDATION
    */

    $errors = array();

    // We need to loop through the required variables to make sure they were posted with the form.
    foreach($requiredFields as $fieldname => $errorMsg)
    {
    if(empty($_POST[$fieldname]))
    {
    $errors[] = $errorMsg;
    }
    }

    // Loop through the $_POST array, to create the PHP variables from our form.
    foreach($_POST AS $key => $value)
    {
    // Is this an allowed field? This is a security measure.
    if(in_array($key, $allowedFields))
    {
    ${$key} = $value;
    }
    }

    /*
    * END FORM VALIDATION
    */

    // Were there any errors?
    if(count($errors) > 0)
    {
    $errorString .= '<ul>';
    foreach($errors as $error)
    {
    $errorString .= "<li>$error</li>";
    }
    $errorString .= '</ul>';

    // display the errors on the page
    ?>
    <html>
    <head>
    <title>Error Processing Form</title>
    </head>
    <body>
    <h2>Error Processing Form</h2>
    <p>There was an error processing the form.</p>

    <p><a href="transfer.html">Go Back to the Form</a></p>
    </body>
    </html>
    Last edited by sparkymom7; Jul 8, 2013 at 08:13. Reason: Unable to add PHP code


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •