Problems sending email via smtp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bcanter
    New Member
    • Mar 2007
    • 9

    Problems sending email via smtp

    I'm trying to setup a Form and send it to our helpdesk so that we don't have to email forms all day long. The problem is that I have very little knowledge of PHP and I cannot seem to find the same thing that I am tring to do on the internet so I can compare what exactly is going wrong. Here is my code(leaving a lot of html out to save space):
    <?php
    if ($_SERVER['REQUEST_METHOD '] != 'POST'){
    $me = $_SERVER['PHP_SELF'];
    ?>
    </div>
    <form method="post" name="form1" id="form1" action="<?=$me? >">
    <div align="center">
    <?
    $name = $_POST["name"];
    $position = $_POST["position"];
    $location = $_POST["location"];
    $department = $_POST["department "];
    $user_phone = $_POST["user_phone "];
    $user_email = $_POST["user_email "];
    $manager = $_POST["manager"];
    $manager_phone = $_POST["manager_ph one"];
    $manager_email = $_POST["manager_em ail"];
    $time_zone = $_POST["time_zone"];
    $start_date = $_POST["start_date "];
    $printer = $_POST["printer"];
    $request_type = $_POST["request_ty pe"];
    $access_type = $_POST["access_typ e"];
    $model_after_us er1 = $_POST["model_after_us er1"];
    $model_after_1 = $_POST["model_afte r_1"];
    $model_after_us er2 = $_POST["model_after_us er2"];
    $model_after_2 = $_POST["model_afte r_2"];
    $notes = $_POST["notes"];
    ?>
    (htmlcodehere)
    </form>
    <?php
    } else {
    error_reporting (0);
    $errors = array();
    if (!$_POST['name'])
    $errors[] = "Name is required";
    if (count($errors) >0) {
    foreach($errors as $err)
    echo "$err<br>\n ";
    echo "<br>Please use your browser's Back button to fix.";
    } else {
    $recipient = 'aerosmith_frea k_03@yahoo.com' ;
    $from = stripslashes($_ POST['name']);
    $subject = "User Request";
    $msg = "Message sent by $from\n";
    $msg.="\nName: ".$_POST['name'];
    $msg.="\nPositi on: ".$_POST['position'];
    $msg.="\nLocati on: ".$_POST['location'];
    $msg.="\nDepart ment: ".$_POST['department'];
    $msg.="\nUser's Phone: ".$_POST['users_phone'];
    $msg.="\nUser's E-mail: ".$_POST['users_email'];
    $msg.="\nManage r: ".$_POST['manager'];
    $msg.="\nManage r's Phone: ".$_POST['managers_phone '];
    $msg.="\nManage r's Email: ".$_POST['managers_email '];
    $msg.="\nTime Zone: ".$_POST['time_zone'];
    $msg.="\nStart Date: ".$_POST['start_date'];
    $msg.="\nPrinte r: ".$_POST['printer'];
    $msg.="\nReques t Type: ".$_POST['request_type'];
    $msg.="\nPositi on: ".$_POST['position'];
    $msg.="\nAccess Type: ".$_POST['access_type'];
    $msg.="\nModel After User1: ".$_POST['model_after_us er1'];
    $msg.="\nModel After 1: ".$_POST['model_after_1'];
    $msg.="\nModel After User2: ".$_POST['model_after_us er2'];
    $msg.="\nModel After 2: ".$_POST['model_after_2'];
    $msg.="\nNotes: ".$_POST['notes'];
    $msg.="\nColor: ".$_POST['color'];
    if (mail($recipien t,$subject,$msg )){
    echo "<p>Your request has been sent</p>";
    echo nl2br($msg);
    } else
    echo "";
    }
    }
    ?>
    </body>
    </html>
    If this matters, model_after_1, model_after_2, and access_type come from checkbox groups. location and time_zone come from drop down menus. request_type is a radio button group. The rest come from text boxes.
    Thanks
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    what is the Error here you are getting.
    in your mail() function some headers missing.
    Please use [PHP] tags around your coding next time, its hard to read.

    Comment

    • bcanter
      New Member
      • Mar 2007
      • 9

      #3
      Well, that's part of the problem, when I submit the form I don't recieve any errors. The page refreshes like it sent something but I never recieve an email or any real indication that it did something. I added "error_reportin g(E_ALL);" to see if that would tell me anything and I get nothing but "Notice: Undefined index: name in F:\intranet\php \index.php on line 27" thru to the "notes" variable. I can send you the whole page if you would like, I just couldn't post it all here Here is a link to it http://docs.google.com/Doc?id=d773v34_ 0g28tv6
      Thanks, Brad

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        First of all we'll check whether this one is working or not. change $to variable here in to your email (gmail/yahoo). if you got the "Mail Sent." message then we'll try to fix the original coding.

        Check Your Spam box if the mail is not there in your mail inbox.

        [PHP]<?php

        $to = "your_Mail_id_@ gmail.com";//Change this to your Mail Address
        $subject = "Test mail";
        $message = "Hello! This is a simple email message.";
        $from = "some1@gmail.co m";
        $headers = "From: $from";
        if(mail($to,$su bject,$message, $headers))
        {
        echo "Mail Sent.";
        }else{
        echo "Error Occured";
        }
        ?>[/PHP]

        if this one is working in your host, send me the HTML form and PHP script to the this forum.
        if this one is not working the problem is in your SMTP settings.
        Where did you test the application localhost or ??

        Comment

        • bcanter
          New Member
          • Mar 2007
          • 9

          #5
          This didn't work, here is the error I got: "Warning: mail() [function.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 F:\intranet\php \test.php on line 14" [if(mail($to,$su bject,$message, $headers))] I have set the php.ini file to point to our mail server (different location) and use the correct ports. This I know is right, I don't agree with this but I was told that I didn't need to specify a username and password for the mail server. I have to specify one when I log on, why wouldn't I have to for this? If that is the problem then where do I specify a username and password?

          Comment

          • bcanter
            New Member
            • Mar 2007
            • 9

            #6
            I found my problem, I thought setting the smtp server in the php.ini file was enough. Apparently not, I added ini_set "("SMTP","XXX.X XX.com");
            ini_set("sendma il_from","php@X XX.com");" and that got it. Thanks, without knowing where to start on this one it would have taken me forever to find out what was going on.

            Comment

            • ak1dnar
              Recognized Expert Top Contributor
              • Jan 2007
              • 1584

              #7
              Originally posted by bcanter
              I found my problem, I thought setting the smtp server in the php.ini file was enough. Apparently not, I added ini_set "("SMTP","XXX.X XX.com");
              ini_set("sendma il_from","php@X XX.com");" and that got it. Thanks, without knowing where to start on this one it would have taken me forever to find out what was going on.
              If you configured your php.ini file correctly again no need to go for ini_set().
              I did not mentioned your O/S, this is for Windows.
              by default there is a ";" of each and every line. that means php.ini file will ignore this line.so we have to remove that.

              Eg:
              Code:
              ;smtp_port = 25
              Settings:
              Code:
              [mail function]
              ; For Win32 only.
              SMTP = smtp_or_mail.your_ISP.com 
              smtp_port = 25
              This is the default from Address, but we can change it from mail() function.

              Code:
              ; For Win32 only.
              sendmail_from = whatever@your_ISP.com

              Comment

              Working...