Solved

Sending on IIS 7.5 via a PHP form and Pear

Posted on 2014-03-23
22
315 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 83

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 108

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
 
LVL 83

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 83

Expert Comment

by:Dave Baldwin
ID: 39951300
0
 
LVL 83

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 83

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 83

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 83

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 83

Expert Comment

by:Dave Baldwin
ID: 39957914
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
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses four methods for overlaying images in a container on a web page
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

16 Experts available now in Live!

Get 1:1 Help Now