[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

Sending on IIS 7.5 via a PHP form and Pear

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
GEMCC
Asked:
GEMCC
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Ray PaseurCommented:
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
 
GEMCCAuthor Commented:
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
Dave BaldwinFixer of ProblemsCommented:
Have you downloaded the Mail package from the Pear site?
0
 
GEMCCAuthor Commented:
No, can you send me the link?  I thought there was another install, but I cannot find it.

Have a great day,

Don
0
 
Dave BaldwinFixer of ProblemsCommented:
0
 
Dave BaldwinFixer of ProblemsCommented:
Another option is PHPMailer.  https://github.com/PHPMailer/PHPMailer
0
 
GEMCCAuthor Commented:
I saw this, but isn't this for a Linux box?

Have a great day,

Don
0
 
Dave BaldwinFixer of ProblemsCommented:
No, PHPMailer is a PHP package, not a binary package.  I have several earlier versions running on this Windows XP machine under IIS.
0
 
GEMCCAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
GEMCCAuthor Commented:
I understand, but is the syntax c:\program files (x86)\PHP\... or c:/program files (x86)/PHP/...?

Have a great day,

Don
0
 
GEMCCAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
GEMCCAuthor Commented:
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
 
GEMCCAuthor Commented:
Forgot to leave off */ . . . Still not working
0
 
Dave BaldwinFixer of ProblemsCommented:
Please click on "Request Attention" above and get some others to look at your question.
0
 
RobOwner (Aidellio)Commented:
0
 
GEMCCAuthor Commented:
Ended up moving the site from IIS to Linux.
0
 
GEMCCAuthor Commented:
Resolved the issue.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now