#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    31
    Rep Power
    0

    Mail and PHP mail function


    Hi

    I have been working on a PHP contact form where the user enters some basic details and the data is sent off as an email.

    So I have made the form and ran it using XAMPP on local host.
    The php runs and I have just put the HTML page and the PHP page in Apache-'htdocs' root folder and ran them there for testing.



    The question is, when I ran the script I added my own email address as a test and did not receive an email.
    So I looked a little at the documentation and some Google results and found that the PHP INI file has to be changed to send mail to your own email on localhost.

    I checked the php.ini file and found this:

    ; 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"
    I checked the C:\mailoutput folder and found my emails.

    So I assumed the script is working properly and there would be no need for me to send an email to myself.

    I have a question regarding the way I should set this up when the time comes to actually deploy the site on someones hosting server,(which would just be some basic business hosting plan).
    Not sure but the plan has enough space,php and some email addresses, this is all I have managed to find out so far so I assume it has everything needed to run the contact form.

    Do I just use the servers SMTP for the email addresses supplied ? What parts of the php.ini file do I use when the page goes live?


    Dan
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    It all depends on how your http server is configured. You don't necessarily need to change php.ini to send mail to yourself. The questions you need to answer are:

    1) Is your http host also running SMTP?
    2) If the answer to 1 is no then are you using a valid SMTP host in your PHP mailer?
    3) If the answer to 1 is yes then what do the SMTP logs say about the email being sent by PHP?
    4) If the answer to 2 is yes then is that host configured to be a mail relay for your http host?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    31
    Rep Power
    0
    Originally Posted by gw1500se

    1) Is your http host also running SMTP?
    Why wouldnt they be? It is a simple hosting plan that offers free emails as part of the package like most do.
    2) If the answer to 1 is no then are you using a valid SMTP host in your PHP mailer?
    I am not using a host as you can see in the lines above xampp just sends the mail to a folder at present.
    3) If the answer to 1 is yes then what do the SMTP logs say about the email being sent by PHP?
    The page is local at present not on the host server.
    4) If the answer to 2 is yes then is that host configured to be a mail relay for your http host?
    This is what I mean do I simply need a valid SMPT address and port to set this up on the server?

    I mean if I added my current SMPT address to the php.ini file (or my clients) then If I uploaded it to a server would it send mail to that person?

    Thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    Where the page resides is not relevant. What matters is where the httpd that runs it resides with respect to the SMTP server. You should not have to make changes to php.ini to send mail. At this point you need to post your PHP code showing how you send the email.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    If you are running windows then you will not have a local mail/SMTP server (unless you have added one yourself) so calling mail() will not send an email.

    Further software and/or configurations will be needed

    XAMPP does not install a local mail/smtp server.

    Possible solution: get a hosted SMTP server (your own or through someone like sendgrid) and use PHPMailer to send out using SMTP
    Last edited by Northie; October 29th, 2012 at 11:10 AM.
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    31
    Rep Power
    0
    Originally Posted by Northie
    If you are running windows then you will not have a local mail/SMTP server (unless you have added one yourself) so calling mail() will not send an email.

    Further software and/or configurations will be needed

    XAMPP does not install a local mail/smtp server.

    Possible solution: get a hosted SMTP server (your own or through someone like sendgrid) and use PHPMailer to send out using SMTP
    I dont have a server myself and I am not want to send mails from my computer.

    This hosting site the client has chosen is using has SMPT and POP3 and also has something called "FormMail", any good?

    Thanks.
  12. #7
  13. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    Originally Posted by BrightIdea
    something called "FormMail", any good?
    No

    =========================

    Originally Posted by BrightIdea
    I dont have a server myself and I am not want to send mails from my computer.
    errr, following your original post,

    Originally Posted by BrightIdea
    did not receive an email
    The point I am trying to make is

    If you do not have a mail server you cannot send emails

    no amount of tweaks to php.ini will solve this
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    31
    Rep Power
    0
    Originally Posted by Northie
    No

    =========================



    errr, following your original post,



    The point I am trying to make is

    If you do not have a mail server you cannot send emails

    no amount of tweaks to php.ini will solve this
    Ok then, I think there is some confusion from am I working server side or client side OR am I a client trying to implement a server side application/script and would like to know how to do it to receive replys from a html form.

    A simple answer would have sufficed, I will have to assume that the server host is running Linux box not windows therefore my home computer setup is on windows totally void.

    I need a mail server yes and have access to two or three, but I don't own one, do you?

    I will look into formmail as it appears to have the basics I need, without having to delve into php scripting too hard at this time, mind you I like php, though my powers are very well stuck between HTML media, CSS2 and 3 Photoshop and XHTML 1.0 strict,

    I am sure you get the picture that I cannot juggle everything with any success.

    Dan
  16. #9
  17. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    I need a mail server yes and have access to two or three, but I don't own one, do you?
    Yes,

    I have one on all of my linux servers (1 dedicated, ~10 production VPSs and 5 dev VPSs), but none on any of my windows machines
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    Did I miss something? How are you running PHP pages on your Windows machine if you don't have an HTTPD running on it?

    Assuming I am mistaken and you do have HTTPD running on your Windows box, you can still send email with PHPMailer if you have access to an SMTP server. I would also guess that your production HTTPD server has access to an SMTP server if it is not running one itself. You can still use PHPMailer to send mail by using that SMTP server instead of 'localhost', as I tried to suggest in my earlier post. This is not really that complicated.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  20. #11
  21. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    He has a web server running locally, but not a local mail server

    As we've both suggested, he could use PHP Mailer an any remote SMTP server he has authority to use/access

    For whatever reason, he is ignoring this suggestion
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    31
    Rep Power
    0
    Thanks

    There are a few lines in the php.ini file I have to change.

    Like adding the ISP SMPT address (which could be mine or the one provided by the hosting site)

    I assumed if I get hosting, I will be able to log into the sites Cpanel? and make the desired changes there?

    Here is the code, it is not mine, I pulled it off the Internet where it was freely available on a website for use by visitors.

    I do not necessarily set out to use this code, I just downloaded it to test my form on LocalHost.
    As you can see there is no SMTP configuration in the script itself.

    So this leads me to believe that this is either not the sort of function I need OR I have to configure the php.ini file.

    I am thinking that a person might not have much access to the php.ini file on a host server (one that is hosted by a hosting business on remote server).

    So this is what I was wondering.
    PHP Code:

    <?php
    if(isset($_POST['email'])) {
         
        
    // CHANGE THE TWO LINES BELOW
        
    $email_to "premiunconnectors@bigpond.com";
         
        
    $email_subject "Product Enquiry";
         
         
        function 
    died($error) {
            
    // your error code can go here
            
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo 
    "These errors appear below.<br /><br />";
            echo 
    $error."<br /><br />";
            echo 
    "Please go back and fix these errors.<br /><br />";
            die();
        }
         
        
    // validation expected data exists
        
    if(!isset($_POST['first_name']) ||
            !isset(
    $_POST['last_name']) ||
            !isset(
    $_POST['email']) ||
            !isset(
    $_POST['telephone']) ||
            !isset(
    $_POST['comments'])) {
            
    died('We are sorry, but there appears to be a problem with the form you submitted.');      
        }
         
        
    $first_name $_POST['first_name']; // required
        
    $last_name $_POST['last_name']; // required
        
    $email_from $_POST['email']; // required
        
    $telephone $_POST['telephone']; // not required
        
    $comments $_POST['comments']; // required
         
        
    $error_message "";
        
    $email_exp '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
      if(!
    preg_match($email_exp,$email_from)) {
        
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
      }
        
    $string_exp "/^[A-Za-z .'-]+$/";
      if(!
    preg_match($string_exp,$first_name)) {
        
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
      }
      if(!
    preg_match($string_exp,$last_name)) {
        
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
      }
      if(
    strlen($comments) < 2) {
        
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
      }
      if(
    strlen($error_message) > 0) {
        
    died($error_message);
      }
        
    $email_message "Form details below.\n\n";
         
        function 
    clean_string($string) {
          
    $bad = array("content-type","bcc:","to:","cc:","href");
          return 
    str_replace($bad,"",$string);
        }
         
        
    $email_message .= "First Name: ".clean_string($first_name)."\n";
        
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
        
    $email_message .= "Email: ".clean_string($email_from)."\n";
        
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
        
    $email_message .= "Comments: ".clean_string($comments)."\n";
         
         
    // create email headers
    $headers 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' phpversion();
    @
    mail($email_to$email_subject$email_message$headers); 
    ?>
     
    Thank you
     

     
    <?php
    }
    die();
    ?>
    Thanks
  24. #13
  25. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    Please forget about the php ini settings.

    I really think you're barking up the wrong tree looking for the solution to your problem

    if you buy hosting, the host will have a mail server so the mail() function will just work.

    By default, the mail() function adds messages directly to the mail queue. There are no smtp settings in any of my php ini files, but there is the path to sendmail (or equivalent)

    I would also recommend not using PHP's built in mail function, but to get the PHPMailer library (its free, consists of 3 files plus some demo files) which will default to mail() but can also be easity configured to use SMTP for your local testing
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    31
    Rep Power
    0
    Originally Posted by Northie
    Please forget about the php ini settings.

    I really think you're barking up the wrong tree looking for the solution to your problem

    if you buy hosting, the host will have a mail server so the mail() function will just work.

    By default, the mail() function adds messages directly to the mail queue. There are no smtp settings in any of my php ini files, but there is the path to sendmail (or equivalent)

    I would also recommend not using PHP's built in mail function, but to get the PHPMailer library (its free, consists of 3 files plus some demo files) which will default to mail() but can also be easity configured to use SMTP for your local testing
    Cheers Ill have a look.

    Why do you not recommend the php mail() default?

    Dan
  28. #15
  29. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    Because using mail() leads to problems - as you've found and PHPMailer has been around for years and has been thoroughly worked on, improved, re-factored to produce a library that works very reliably and takes out all the headaches associated with trying to send emails from PHP and meeting your SLAs

    Because configuring PHPMailer to send out over SMTP is easier than configuring mail() to send out through such an SMTP connection

    Because PHPMailer adds all the appropriate headers to make mail look less automates, less spammy

    Because using PHPMailer makes it easy to send multi part messages

    Because using PHPMailer makes it easy to send attachments
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

IMN logo majestic logo threadwatch logo seochat tools logo