[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2258
  • Last Modified:

contact form (PHP) - SMTP authentication

Hi,
I downloaded web page template and there is contact.php file what is form for email contact. Problem is this form does not support SMTP authentication which I need for it.
Is there a way how to simply update this piece of code to work with auth? Just update contact.php without need to change anything else?

Thanks
contact.php
0
helpfinder
Asked:
helpfinder
  • 6
  • 3
  • 2
  • +1
4 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
Not really.  The PHP mail() function does not support authentication.  The most common recommendation is PHPMailer but it is not a dropin but it does work.  http://code.google.com/a/apache-extras.org/p/phpmailer/  It comes with samples that work and documentation.
0
 
helpfinderAuthor Commented:
damn, any idea how to use that contact.php? any service which could be used for this? so 100% working email service with no need to set SMTP auth?

thanks
0
 
Julian HansenCommented:
It depends on your ISP - wherever your site is hosted - if they don't support it - as Dave says it aint gonna work. Option

1. Move to phpMailer or similar
2. Move to host that allows local unauthorised SMTP sending

One option you might try is finding out if your Host supports custom PHP.INI files - in which case you can setup your own PHP.INI file (or try set through .htaccess) with SMTP info.

Find out if this is possible first then we can look at how to do this.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
Ray PaseurCommented:
Here is the contact form.  This appears to be a very old script and it is no longer a technically incompetent solution.  On line 93, the function will fail to validate many perfectly acceptable email addresses.  PHP has a filter_var() function that should be used instead.  On line 109 the deprecated function eregi() needs to be replaced.  It looks like the code uses the $answer variable as some kind of a nonce, passing it through the form  and checking it after the form is submitted.  That's not really useful; the current technology would be to use a CAPTCHA test instead.

You might want to go back to the source and see if there is an updated version available.

In my experience, most hosting companies allow some number of unauthenticated mail() commands (perhaps a few hundred per day).  If you can live with that, you might not have any problem.  Just a thought.

Best of luck with it, ~Ray

<!DOCTYPE HTML>
<html>

<head>
  <title>CSS3_design_one</title>
  <meta name="description" content="website description" />
  <meta name="keywords" content="website keywords, website keywords" />
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <link rel="stylesheet" type="text/css" href="css/style.css" />
  <!-- modernizr enables HTML5 elements and feature detects -->
  <script type="text/javascript" src="js/modernizr-1.5.min.js"></script>
</head>

<body>
  <div id="main">
    <header>
      <div id="logo">
        <!-- class="logo_colour", allows you to change the colour of the text -->
        <h1><a href="index.html">CSS3<span class="logo_colour">design</span>_one</a></h1>
      </div>
      <nav>
        <ul class="sf-menu" id="nav">
          <li><a href="index.html">Home</a></li>
          <li><a href="examples.html">Examples</a></li>
          <li><a href="page.html">A Page</a></li>
          <li><a href="another_page.html">Another Page</a></li>
          <li><a href="#">Example Drop Down</a>
            <ul>
              <li><a href="#">Drop Down One</a></li>
              <li><a href="#">Drop Down Two</a>
                <ul>
                  <li><a href="#">Sub Drop Down One</a></li>
                  <li><a href="#">Sub Drop Down Two</a></li>
                  <li><a href="#">Sub Drop Down Three</a></li>
                  <li><a href="#">Sub Drop Down Four</a></li>
                  <li><a href="#">Sub Drop Down Five</a></li>
                </ul>
              </li>
              <li><a href="#">Drop Down Three</a></li>
              <li><a href="#">Drop Down Four</a></li>
              <li><a href="#">Drop Down Five</a></li>
            </ul>
          </li>
          <li><a href="contact.php">Contact Us</a></li>
        </ul>
      </nav>
    </header>
    <div id="site_content">
      <div id="sidebar_container">
        <div class="sidebar">
          <h1>Latest News</h1>
          <h2>New Website Launched</h2>
          <p>We've redesigned our own website. Take a look around and let us know what you think.</p>
        </div>
        <div class="sidebar">
          <h1>Special Offers</h1>
          <h2>10% Discount</h2>
          <p>For the month of February 2012, we are offering a 10% discount for all new visitors.</p>
        </div>
        <div class="sidebar">
          <h1>Contact Us</h1>
          <p>We'd love to hear from you. Call us on <strong style="color: #A50303;">01000 111 111</strong>, <a href="#">email us</a> or complete our <a href="contact.php">contact form</a>.</p>
        </div>
      </div>
      <div id="content">
        <ul class="slideshow">
          <li class="show"><img width="706" height="316" src="images/1.jpg" alt="image one" /></li>
          <li><img width="706" height="316" src="images/2.jpg" alt="image two" /></li>
          <li><img width="706" height="316" src="images/3.jpg" alt="image three" /></li>
          <li><img width="706" height="316" src="images/4.jpg" alt="image four" /></li>
          <li><img width="706" height="316" src="images/5.jpg" alt="image five" /></li>
        </ul>
        <div id="content_item">
          <h1>Contact Us</h1>
          <p>Say hello, using this contact form.</p>
          <?php
            // This PHP Contact Form is offered &quot;as is&quot; without warranty of any kind, either expressed or implied.
            // David Carter at www.css3templates.co.uk shall not be liable for any loss or damage arising from, or in any way
            // connected with, your use of, or inability to use, the website templates (even where David Carter has been advised
            // of the possibility of such loss or damage). This includes, without limitation, any damage for loss of profits,
            // loss of information, or any other monetary loss.

            // Set-up these 3 parameters
            // 1. Enter the email address you would like the enquiry sent to
            // 2. Enter the subject of the email you will receive, when someone contacts you
            // 3. Enter the text that you would like the user to see once they submit the contact form
            $to = 'enter email address here';
            $subject = 'Enquiry from the website';
            $contact_submitted = 'Your message has been sent.';

            // Do not amend anything below here, unless you know PHP
            function email_is_valid($email) {
              return preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i',$email);
            }
            if (!email_is_valid($to)) {
              echo '<p style="color: red;">You must set-up a valid (to) email address before this contact page will work.</p>';
            }
            if (isset($_POST['contact_submitted'])) {
              $return = "\r";
              $youremail = trim(htmlspecialchars($_POST['your_email']));
              $yourname = stripslashes(strip_tags($_POST['your_name']));
              $yourmessage = stripslashes(strip_tags($_POST['your_message']));
              $contact_name = "Name: ".$yourname;
              $message_text = "Message: ".$yourmessage;
              $user_answer = trim(htmlspecialchars($_POST['user_answer']));
              $answer = trim(htmlspecialchars($_POST['answer']));
              $message = $contact_name . $return . $message_text;
              $headers = "From: ".$youremail;
              if (email_is_valid($youremail) && !eregi("\r",$youremail) && !eregi("\n",$youremail) && $yourname != "" && $yourmessage != "" && substr(md5($user_answer),5,10) === $answer) {
                mail($to,$subject,$message,$headers);
                $yourname = '';
                $youremail = '';
                $yourmessage = '';
                echo '<p style="color: blue;">'.$contact_submitted.'</p>';
              }
              else echo '<p style="color: red;">Please enter your name, a valid email address, your message and the answer to the simple maths question before sending your message.</p>';
            }
            $number_1 = rand(1, 9);
            $number_2 = rand(1, 9);
            $answer = substr(md5($number_1+$number_2),5,10);
          ?>
          <form id="contact" action="contact.php" method="post">
            <div class="form_settings">
              <p><span>Name</span><input class="contact" type="text" name="your_name" value="<?php echo $yourname; ?>" /></p>
              <p><span>Email Address</span><input class="contact" type="text" name="your_email" value="<?php echo $youremail; ?>" /></p>
              <p><span>Message</span><textarea class="contact textarea" rows="5" cols="50" name="your_message"><?php echo $yourmessage; ?></textarea></p>
              <p style="line-height: 1.7em;">To help prevent spam, please enter the answer to this question:</p>
              <p><span><?php echo $number_1; ?> + <?php echo $number_2; ?> = ?</span><input type="text" name="user_answer" /><input type="hidden" name="answer" value="<?php echo $answer; ?>" /></p>
              <p style="padding-top: 15px"><span>&nbsp;</span><input class="submit" type="submit" name="contact_submitted" value="send" /></p>
            </div>
          </form>
        </div>
      </div>
    </div>
    <footer>
      <p><a href="index.html">Home</a> | <a href="examples.html">Examples</a> | <a href="page.html">A Page</a> | <a href="another_page.html">Another Page</a> | <a href="contact.php">Contact Us</a></p>
      <p>Copyright &copy; CSS3_design_one | <a href="http://www.css3templates.co.uk">design from css3templates.co.uk</a></p>
    </footer>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
  </div>
  <!-- javascript at the bottom for fast page loading -->
  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript" src="js/jquery.easing-sooper.js"></script>
  <script type="text/javascript" src="js/jquery.sooperfish.js"></script>
  <script type="text/javascript" src="js/image_fade.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      $('ul.sf-menu').sooperfish();
    });
  </script>
</body>
</html>

Open in new window

0
 
helpfinderAuthor Commented:
hi there,
thanks for answer. Honestly, I can not work with PHP, so that´s why I downloaded template and this contact form was included. I agree some simple number + number is outdated and CAPTCHA is more accurate, but this is not a big deal for me. It should be for small and very simple personal web page with a few visits per day, bubt I want to have contact formular on it.
I do not care how, just some simple solution (the best would be without need to install anything else)
I already contacted comopany which is hosting this web page, but they unfortunately do not support SMTP without auth (gmail the same)
:-(
0
 
Ray PaseurCommented:
One alternative to the contact form might be a simple clickable mailto link.  Example here:
http://www.6327everglades.com/search/search.php
0
 
helpfinderAuthor Commented:
but email link opens email client, what is not I want to use. I need to have option for people who want to contact me write their email address, subject and email body, pust the button and that´s all.
0
 
Julian HansenCommented:
I already contacted comopany which is hosting this web page, but they unfortunately do not support SMTP without auth (gmail the same)
:-(
Did you find out about custom PHP.INI settings?
0
 
helpfinderAuthor Commented:
no, for PHP.ini file I am lost :D
0
 
Dave BaldwinFixer of ProblemsCommented:
Your current code can't be made to work.  You need to pick another solution.  That solution may require you to learn something or hire someone to do it.
0
 
helpfinderAuthor Commented:
OK, anyway thanks for effort and suggestions
0
 
Ray PaseurCommented:
@helpfinder, When the question is, "damn, any idea how to use that..." the right answer is to stop what you're doing and hire a professional developer.  I say this in respect of your time and expense associated with trying to do it yourself, using tools you do not understand.  There is no harm in ignorance, but using it as a foundation will ensure that disaster is not left to chance.

A hosting company that will not allow you to send unauthenticated emails is a burden, not a service.  Get a new host.  I use and recommend ChiHost.com.  They have very good support and permit plenty of emails from my PHP scripts using the mail() function without any headers.  Easy!

Here is a sample form-to-email script.  When a professional programmer sees something like this its utility is immediately obvious and the mind goes to ways of integrating it into an existing application.  Maybe if you provide this to your professional programmer you will be able to get a better result.

<?php // RAY_form_to_email.php
error_reporting(E_ALL);


// SEND MAIL FROM A FORM


// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "NoReply@Your.org";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS - CHANGE THESE FOR YOUR WORK
$to[]  = "You@Your.org";
$to[]  = "Her@Your.org";
$to[]  = "Him@Your.org";


// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
    $telephone  = clean_string($_POST["telephone"]);

    // CONSTRUCT THE MESSAGE THROUGH STRING CONCATENATION
    $content    = NULL;
    $content   .= "You have a New Query From $name" . PHP_EOL . PHP_EOL;
    $content   .= "Tel No: $telephone" . PHP_EOL;
    $content   .= "Email: $email" . PHP_EOL;

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }
}


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT
$form = <<<ENDFORM
<form method="post">
Please enter your contact information
<br/>Email: <input name="email" />
<br/>Phone: <input name="telephone" />
<br/>Name:  <input name="name" />
<br/><input type="submit" />
</form>
ENDFORM;

echo $form;


// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    // IF MAGIC QUOTES IS ON, WE NEED TO REMOVE SLASHES
    $str = stripslashes($str);

    // REMOVE EXCESS WHITESPACE
    $rgx
    = '#'                // REGEX DELIMITER
    . '\s'               // MATCH THE WHITESPACE CHARACTER(S)
    . '\s+'              // MORE THAN ONE CONTIGUOUS INSTANCE OF WHITESPACE
    . '#'                // REGEX DELIMITER
    ;
    $str = preg_replace($rgx, ' ', $str);

    // REMOVE UNWANTED CHARACTERS
    $rgx
    = '#'                // REGEX DELIMITER
    . '['                // START OF A CHARACTER CLASS
    . '^'                // NEGATION - MATCH NONE OF THE CHARACTERS IN THIS CLASS
    . 'A-Z0-9'           // KEEP LETTERS AND NUMBERS
    . '@&+:?_.,/\-'      // KEEP SOME SPECIAL CHARACTERS (ESCAPED HYPHEN)
    . ' '                // KEEP BLANKS
    . ']'                // END OF THE CHARACTER CLASS
    . '#'                // REGEX DELIMITER
    . 'i'                // CASE-INSENSITIVE
    ;
    $str = preg_replace($rgx, NULL, $str);

    return trim($str);
}

Open in new window

Going forward, please don't give the worst possible grade anyone can give at EE.  You've got yourself stuck, and all we can try to do is help you get unstuck.  You got several good suggestions here.  How you work with those suggestions is really up to you; we can't make the choices for you.

Best of luck with the project, ~Ray
0
 
helpfinderAuthor Commented:
thanks Ray,
I really appreciate your help
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now