Abyss, SMTP, Forms and PHP on Mac OS X

 
Post new topic   Reply to topic    Aprelium Forum Index -> Tutorials
View previous topic :: View next topic  
Author Message
Ian McPherson
-


Joined: 12 Jun 2007
Posts: 23

PostPosted: Fri Jun 15, 2007 10:44 am    Post subject: Abyss, SMTP, Forms and PHP on Mac OS X Reply with quote

Hi,

I had problems with sendmail under PHP on my Mac OS X machine. I use EIMS (Eudora Internet Mail Server) 3.2.x, an excellent Mac OS email server, which, naturally enough, takes over port 25 for SMTP. I had to find a way to use EIMS for web forms (and other applications) where email was necessary. BTW, this uses the Abyss PHP 5 package for Mac OS X. This may not be the most elegant solution ever, but it works.

First, I chose PHPMailer and downloaded the package. I installed the two class files (class.phpmailer.php and class.smtp.php) in the HD/Applications/PHP5/lib/php/ directory.

I then created an email address (let's call it forms@host.com) with login enabled and the world's nastiest password. I would also suggest you limit, if you can, relaying for the domain to LAN IP Nos and/or local domains only.

I especially wanted custom form response pages for this install, as I hate the cheap looking things that PHP generates automatically. I wanted pages that look like the website they came from.

OK. So here's the code for the PHP:

Code:
<?php
// Ian McPherson - third go 14-06-07

$title = $_POST['title']; // form field
$firstname = $_POST['firstname']; // form field
$lastname = $_POST['lastname']; // form field
$company = $_POST['company']; // form field
$address = $_POST['address']; // form field
$code = $_POST['code']; // form field
$state = $_POST['state']; // form field
$country = $_POST['country']; // form field
$email = $_POST['email']; // form field
$phone = $_POST['phone']; // form field
$fax = $_POST['fax']; // form field
$request = $_POST['request']; // form field

$ian_err = "error.html"; // html error page
$ian_ok = "thanks.html"; // html thanks page

require("class.phpmailer.php"); // call to PHPMailer class file

$mail = new PHPMailer(); // make new email message

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "0.0.0.0"; // SMTP server IP - LAN IP is OK
$mail->From = "forms@host.com"; // genuine email address in SMTP Server - login must be on
$mail->AddAddress("client@client.com"); // final TO address

$mail->Subject = "Your favourite project has had an email response";
$mail->Body = "Dear Brian,\n\nYou have received a Brochure request from the free\nbrochure page on the Your Favourite Project website.\n\nThe enquirer's details are as follows: Brochure request\n\nPlease post to:\n\nTitle: $title\nFirst Name: $firstname\nLast name: $lastname\nCompany: $company\nAddress: $address\nPostode: $code\nState: $state\nCountry: $country\nEmail: $email\nPhone: $phone\nFax: $fax\nAny additional comments: $request\n\nRegards,\n\nFred Nerk\nYour Favourite Project Webmaster\nmailto:fred@host.com"; // body of email - \n is a line break, $xxxxx are form fields to be included in the email

if(!$mail->Send()) // send the email to the SMTP Server
{
   header("Location: $ian_err"); // if there's an error, send browser to this page
   exit;
}
else
{
   header("Location: $ian_ok"); // No error? send browser here
}
?>


Now, here's the code for the Javascript form trapping. Note that not all fields are "required":

Code:
<SCRIPT>
<!--  Hide from old browsers

function checkForm(form)
{
   if (form.title.value == "")
   {
      alert("Please enter your Title.");
      return false;
   }

   if (form.firstname.value == "")
   {
      alert("Please enter your First name.");
      return false;
   }

   if (form.lastname.value == "")
   {
      alert("Please enter your Last name.");
      return false;
   }

   if (form.company.value == "")
   {
      alert("Please enter your Company name.");
      return false;
   }

   if (form.address.value == "")
   {
      alert("Please enter your Address.");
      return false;
   }

   if (form.code.value == "")
   {
      alert("Please enter your Postcode or Zip.");
      return false;
   }

   if (form.state.value == "")
   {
      alert("Please enter your State.");
      return false;
   }

   if (form.country.value == "")
   {
      alert("Please enter your Country.");
      return false;
   }
   
   return true;
}
// -->
</SCRIPT>


The last thing to do is add the Javascript to the FORM tag in your HTML:

Code:
<FORM ACTION="whatever.php" METHOD=POST onSubmit="return checkForm(this);">


Obviously, the problem with this approach is that every form page requires an individual script.

Yeah, I know. That's a time-wasting bummer...

Maybe some genius PHP guy can figure out how to rewrite or extend this approach to "include" text from an external file, which would make the script form independent? Now that would be excellent...

Any good ideas, you guys? I'm all ears... :)

Ian McPherson
Back to top View user's profile Send private message
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Wed Jun 27, 2007 11:07 pm    Post subject: Re: Abyss, SMTP, Forms and PHP on Mac OS X Reply with quote

Ian McPherson,

The text could be put in the normal text file and the content of the variable initialized using the readfile() function as in:

Code:
eval( "$mail->Body = \"" + addslashes(file_get_contents("body.txt")) + "\";");


body.txt should contain:

Code:
Dear Brian,

You have received a Brochure request from the free
brochure page on the Your Favourite Project website.
The enquirer's details are as follows: Brochure request
Please post to:

Title: $title
First Name: $firstname
Last name: $lastname
Company: $company
Address: $address
Postode: $code
State: $state
Country: $country
Email: $email
Phone: $phone
Fax: $fax
Any additional comments: $request

Regards,

Fred Nerk
Your Favourite Project Webmaster
mailto:fred@host.com

_________________
Support Team
Aprelium - http://www.aprelium.com
Back to top View user's profile Send private message Send e-mail
Ian McPherson
-


Joined: 12 Jun 2007
Posts: 23

PostPosted: Thu Jun 28, 2007 4:45 am    Post subject: PHP Forms Reply with quote

Hi,

That solves half the problem, but I still would have to write a different PHP file for each form unless the fields don't change names (which is unlikely). I suppose I could use the same readme technique for the fields?

Thanks,

Ian
Back to top View user's profile Send private message
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Thu Jun 28, 2007 10:25 am    Post subject: Re: PHP Forms Reply with quote

Ian McPherson,

Sure you could use the same technique for other fields. If you know how to split strings in PHP (using explode()) you can even have a single file with all data separated with a special string (say for example --**--). You read the file in a string, explode it and get an array of strings, and fill each field with the right element of the array.
_________________
Support Team
Aprelium - http://www.aprelium.com
Back to top View user's profile Send private message Send e-mail
wlfdgcrkz
-


Joined: 06 Jul 2007
Posts: 1

PostPosted: Fri Jul 06, 2007 6:32 pm    Post subject: Reply with quote

Could someone please tell me where the file names and locations for this code to be placed?

ie.
1. code for the PHP:
2. here's the code for the Javascript form trapping.
3.the Javascript to the FORM tag in your HTML

Does this method eliminte the need for formmail.php ?

I apologise for my ignonce.
Back to top View user's profile Send private message
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Sat Jul 07, 2007 2:59 pm    Post subject: Reply with quote

[quote="wlfdgcrkz"]Could someone please tell me where the file names and locations for this code to be placed?

ie.
1. code for the PHP:
Quote:


You can put that PHP script in any directory inside your web site (by default the subdirectory called htdocs/ located inside Abyss Web Server installlation directory).

[quote="wlfdgcrkz"]
2. here's the code for the Javascript form trapping.
3.the Javascript to the FORM tag in your HTML

Does this method eliminte the need for formmail.php ?

I apologise for my ignonce.


The question is not clear. What Javascript part are you speaking about? Javascript runs on the client side and cannot replace what is going to run on the server side (the PHP script in that case).
_________________
Support Team
Aprelium - http://www.aprelium.com
Back to top View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> Tutorials All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group