Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sending on IIS 7.5 via a PHP form and Pear

Posted on 2014-03-23
22
Medium Priority
?
344 Views
Last Modified: 2014-07-27
Hello,

I have PHP v5.3 and Pear running on IIS 7.5.  I am trying to send an e-mail via a form.  I see that I need a mail.php file, but do not know from where to get it.  If I can use the default SMTP in IIS, great.

Please advise,

Don
0
Comment
Question by:GEMCC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
22 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39949520
IIS 7.5 doesn't come with SMTP enabled.  If you haven't done that yet, this page looks like good instructions:  http://www.chrislazari.com/2011/04/iis-75-installing-smtp-service-securing.html

And here http://pear.php.net/package/Mail/download is the location for the Pear Mail package.  For some reason, I haven't been able to get Pear to work on this machine so I can't tell you much more about it.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39949973
If you want a simple and easy-to-follow example of form-to-email, this script shows the essential moving parts.  Couple it with a CAPTCHA test and you have something good enough to deploy.

<?php // demo/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 (AT LEAST SOMETHING IN THE EMAIL INPUT CONTROL)
if (!empty($_POST['e']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $safe_mail = clean_string($_POST['e']);
    $safe_name = clean_string($_POST['n']);
    $safe_fone = clean_string($_POST['t']);
    $safe_idea = clean_string($_POST['i']);

    // CONSTRUCT THE MESSAGE THROUGH STRING CONCATENATION
    $content = NULL;
    $content .= "You have a New Query From $safe_name" . PHP_EOL . PHP_EOL;
    $content .= "Tel No: $safe_fone" . PHP_EOL;
    $content .= "Email: $safe_mail" . PHP_EOL;
    $content .= "Idea: $safe_idea" . PHP_EOL;

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

    // PRODUCE THE THANK-YOU PAGE
    echo '<p>THANK YOU</p>' . PHP_EOL;
    die('TASK COMPLETE');
}


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT
$form = <<<ENDFORM
<form method="post">
Please enter your contact information
<br/>Email: <input name="e" />
<br/>Phone: <input name="t" />
<br/>Name:  <input name="n" />
<br/>Ideas? <textarea name="i"></textarea>
<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 DOUBLE QUOTES
    . "'"                // KEEP SINGLE QUOTES
    . '@&+:?_.,/\-'      // KEEP SOME SPECIAL CHARACTERS (ESCAPED HYPHEN)
    . ' '                // KEEP BLANKS
    . ']'                // END OF THE CHARACTER CLASS
    . '#'                // REGEX DELIMITER
    . 'i'                // CASE-INSENSITIVE
    ;
    $str = preg_replace($rgx, NULL, $str);

    // REMOVE EXCESSIVE INPUT
    $str = substr($str, 0, 255);

    return trim($str);
}

Open in new window

HTH, ~Ray
0
 

Author Comment

by:GEMCC
ID: 39950237
I regret ot say the form above does not work because the SMTP server requires authentication and per my research that's why I needed to install Pear.

I ran some tests and it appears Pear is running fine.

Any other ideas?

Have a great day,

Don
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39950993
Have you downloaded the Mail package from the Pear site?
0
 

Author Comment

by:GEMCC
ID: 39951276
No, can you send me the link?  I thought there was another install, but I cannot find it.

Have a great day,

Don
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39951300
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39951315
Another option is PHPMailer.  https://github.com/PHPMailer/PHPMailer
0
 

Author Comment

by:GEMCC
ID: 39951622
I saw this, but isn't this for a Linux box?

Have a great day,

Don
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39951731
No, PHPMailer is a PHP package, not a binary package.  I have several earlier versions running on this Windows XP machine under IIS.
0
 

Author Comment

by:GEMCC
ID: 39954954
OK, I believe I have everything installed correctly.

Keeping in mind this is all on an IIS server, if I am going to use a Linux/Postifx server for SMTP services, do I need to do anything with SMTP services in IIS?

The line:

require_once "mail.php";

Is this correct or is it supposed to be the full path.  If it is (I am never sure about slashed), is it c:\program files (x86)\... or c:/program files (x86)/...?

Thanks for your help,

Don
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39955203
IIS has nothing to do with this and neither does Linux/Postfix.  The PEAR Mail package is intended to allow you to connect to SMTP servers that are not on your machine or that require authentication which the PHP mail() function does not do.  "require_once "mail.php"; should be a file on a relative path on the web server.  It is probably the main 'class' for the PEAR Mail package.  For some reason PEAR doesn't seem to work on this machine so I couldn't download the package to see how it works.
0
 

Author Comment

by:GEMCC
ID: 39956062
I understand, but is the syntax c:\program files (x86)\PHP\... or c:/program files (x86)/PHP/...?

Have a great day,

Don
0
 

Author Comment

by:GEMCC
ID: 39956123
Well, I moved the site over to a Linux box and now it works!  I guess I too am having issues with Pear on an IIS box.  Drag.

Have a great day,

Don
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39956653
Linux derived programs either automatically switch the '\' to '/' or they don't like spaces in file path names.  I realize that Win7 and newer try to block you from putting anything in 'C:' but on XP that's where I have always put PHP and Apache.  I believe WAMP and XAMPP also create directories without spaces.
0
 

Author Comment

by:GEMCC
ID: 39957800
Per http://pear.php.net/manual/en/installation.checking.php it appears my install of Pear works fine.

Do you see anything wrong here:

<?php
 require_once "Mail.php";
 
 $from = "Sandra Sender <sender@example.com>";
 $to = "contact@domain.net>";
 $subject = "Hi!";
 $body = "Hi,\n\nHow are you?";
 
 $host = "mail.domain.net";
 $username = "user@domain.net";
 $password = "password";
 
 $headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     '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>");
  }*/
 ?>

Is the line require_once "mail.php"; supposed to be the full path?

Any help is greatly appreciated.

Have a great day,

Don
0
 

Author Comment

by:GEMCC
ID: 39957819
Forgot to leave off */ . . . Still not working
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39957914
Please click on "Request Attention" above and get some others to look at your question.
0
 
LVL 43

Expert Comment

by:Rob
ID: 39996038
0
 

Accepted Solution

by:
GEMCC earned 0 total points
ID: 40211425
Ended up moving the site from IIS to Linux.
0
 

Author Closing Comment

by:GEMCC
ID: 40222267
Resolved the issue.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question