The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Trying to Send Email from a PHP Script

Discussion in 'E-mail Discussions' started by gecasti, Dec 3, 2013.

  1. gecasti

    gecasti Registered

    Joined:
    Dec 2, 2013
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    In my cPanel domain and account, I am creating a simple HTML and CSS business website that uses PHP to send an email notification to request an estimate or to contact the business.

    I created a cPanel email account to handle these type of emails, which I am using in my php script with the corresponding uid and pwd.

    Currently I have the HTML, CSS, and PHP all together in one file that I call estimate.php

    PHP:
    <?php
        
    [B]require_once "../php/Mail.php";[/B]
        require_once 
    "../php/smtp.php";
        require_once 
    "../php/Net/SMTP.php";
        
        
    $FirstName $_POST['FirstName'];
        
    $LastName $_POST['LastName'];
        
    $company $_POST['company'];
        
    $email $_POST['email'];
        
    $phone $_POST['phone'];
        
    $formMessage $_POST['message'];
        
        
    $to '<username>@<mycpaneldomain.com>';
        
        
    $subject '[TEST] Free Estimate or Quote Request';
        
        
    $headers "From: " strip_tags($_POST['email']) . "\r\n";
        
    $headers .= "Reply-To: "strip_tags($_POST['email']) . "\r\n";
        
    $headers .= "CC: [email]<anotheremail>@yahoo.com[/email]\r\n";
        
    $headers .= "MIME-Version: 1.0\r\n";
        
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        
        
    //Incoming and Outgoing Server <mail.hostname.com>
        
    $host "ssl://<mail.hostname.com>";
        
    $port "465";  //SMTP: Port 465    IMAP: Port 993    POP3: Port 995
        
    $username "<estimate>@<mycpaneldomain.com>";
        
    $password "<password>";
         
         
    $smtp Mail::factory('smtp', array ('host' => $host'port' => $port'auth' => true'username' => $username'password' => $password));
         
    $mail $smtp->send($to$headers$body);
         
         if(
    PEAR::isError($mail)) {
              echo(
    "<p>" $mail->getMessage() . "</p>");
         } else {
              echo(
    "<p>Message successfully sent!</p>");
         }
    ?>
    </head>
         <body>
              <div class="wrapper">
                   <header>
               ........................
                   </header>
                ........................
                        <form method="post" action="estimate.php">
                            <fieldset>
                            <legend><h3>Free Estimate or Quote</h3></legend>
                                <p>
                                    <label class="title" for="FirstName">First Name:</label>
                                    <i>*</i>
                                    <input type="text" name="FirstName" id="FirstName" size="300" maxlength="400" title="Enter your first name.">
                                    <br />
                                    <label class="title" for="LastName">Last Name:</label>
                                    <i>*</i>
                                    <input type="text" name="LastName" id="LastName" size="300" maxlength="400" title="Enter your last name.">
                                    <br />
                                    <label class="title" for="company">Company:</label>
                                    <i>*</i>
                                    <input type="text" name="company" id="company" size="300" maxlength="400" title="What's the name of your company or organization?">
                                    <br />
                                    <label class="title" for="email">Your email:</label>
                                    <i>*</i>
                                    <input type="text" name="email" id="email" size="300" maxlength="400" title="Enter your email address.">
                                    <br />
                                    <label class="title" for="phone">Phone:</label>
                                    <i>*</i>
                                    <input type="text" name="phone" id="phone" size="300" maxlength="400" title="Enter your phone number.">
                                    <br />
                                    <label for="message">Question(s) and/or Comment(s):</label>
                                    <textarea name="message" id="message" cols="72" rows="5" title="Enter your questions and/or comments."></textarea>
                                </p>
                                <div class="submit"><input type="submit" id="submit" value="GET IT NOW" /></div>
                            </fieldset>
                        </form>





    My html, images, php, and css files are in the .../www directory and the installed PHP extensions and apps are in the .../php directory

    Note that my cpanel setup from pear.php.net already has the following Installed PHP Extension(s) and Application(s)
    <> Auth_SASL 1.0.6
    <> Mail 1.2.0
    <> Net_SMTP 1.6.2
    <> Net_Socket 1.0.14


    To me it seems like I have everything I need, I just need to link or glue all the pieces together. When I try to go to the estimate.php page via an IE web browser I get the following error:

    Code:
    Warning: require_once(../php/smtp.php) [function.require-once]: failed to open stream: No such file or directory in ../public_html/estimate.php on line 49
     
    Fatal error: require_once() [function.require]: Failed opening required '../php/smtp.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in <mycpanle_home_root>/public_html/estimate.php on line 49

    Line 49 would be require_once "../php/Mail.php"; It can't find the php/Mail.php file, but is there...

    It seems like the 'ROOT_PATH' is Not defined; I can't find the php.ini file and I don't know how to define the 'ROOT_PATH'.
     
  2. jimlongo

    jimlongo Well-Known Member

    Joined:
    Mar 20, 2008
    Messages:
    145
    Likes Received:
    2
    Trophy Points:
    18
    This isn't really a server issue.
    I would ask this question at the HTML Help forums, you'll get some good help there.
     
  3. HSN-Saman

    HSN-Saman Member

    Joined:
    Jan 11, 2013
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    DataCenter Provider
    You may check the permission and owner of those file that are not accessible by your application.
     
    #3 HSN-Saman, Dec 4, 2013
    Last edited: Dec 4, 2013
  4. gecasti

    gecasti Registered

    Joined:
    Dec 2, 2013
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    Where can I find the php.ini file? How do I set the ROOT_PATH in cPanel?
     
  5. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Hello :)

    You may want to try using the full path to the included files. EX:

    Code:
    /home/$username/public_html/path/to/file.php
    The php.ini file is stored at /usr/local/lib/php.ini by default. If you do not have root access, you can create your own php.ini file in /home/$username if your host uses a PHP handler that supports it.

    Thank you.
     
  6. gecasti

    gecasti Registered

    Joined:
    Dec 2, 2013
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    cPanelMichael,

    I am pass that problem, now I need to configure PHP to use that mail server as its primary mail server. Where can I find the php.ini file so I can set the following options:
    <> SMTP — The hostname of your SMTP server; default is localhost
    <> smtp_port — The port for your SMTP server; default is 25
    <> sendmail_from — The name and email to set for the from header
    <> sendmail_path — Path to sendmail or a sendmail-capable utility

    Currently I am getting the following failure:
    authentication failure [SMTP: Invalid response code received from server (code: 535, response: Incorrect authentication data)]

    Please advise, thanks.
     
  7. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Those values should be configured in your PHP script and not in the php.ini file. I recommend consulting with your web hosting provider for assistance because they can review the existing php.ini file if necessary, or troubleshoot the 535 error.

    Thank you.
     
  8. gecasti

    gecasti Registered

    Joined:
    Dec 2, 2013
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    I got it working, thank you!!
     
Loading...

Share This Page