Solved

Sending on IIS 7.5 via a PHP form and Pear

Posted on 2014-03-23
22
310 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
22 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Have you downloaded the Mail package from the Pear site?
0
 

Author Comment

by:GEMCC
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Another option is PHPMailer.  https://github.com/PHPMailer/PHPMailer
0
 

Author Comment

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

Have a great day,

Don
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
Comment Utility
Forgot to leave off */ . . . Still not working
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Please click on "Request Attention" above and get some others to look at your question.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
0
 

Accepted Solution

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

Author Closing Comment

by:GEMCC
Comment Utility
Resolved the issue.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now