487
edits
m (New page: <span id="top"></span> <div style="padding:0;margin:0; border-bottom:3px inset #000000"> {| | MPG UniCenter || PHP mail function | ...) |
(Major grammar change, and some rewriting for clarity,) |
||
Line 13: | Line 13: | ||
'''Uniform Server 3.5-Apollo.''' | '''Uniform Server 3.5-Apollo.''' | ||
|} | |} | ||
You may be using Uniform Sever either as a test environment or for learning PHP | You may be using Uniform Sever either as a test environment or for learning PHP. What you will quickly discover is PHP’s mail() function does not work. That's strictly not true. It does work, but because of its limitations, it may be prevented from working. For instance a host may require authentication, which the function does not support. Or it could simply be a case of not being set-up correctly. | ||
'''Starting point''' | '''Starting point''' | ||
I am assuming you are not running your own mail server and do not wish to install one. All you want to do is send emails to or via | I am assuming you are not running your own mail server and do not wish to install one. All you want to do is send emails to or via your ISP mail server (as a relay) using PHP, either to test your own scripts or to test third party ones. This article looks at ways to get you up and running. | ||
== Test Scripts == | == Test Scripts == | ||
I try to make UniCenter as practical as possible hence provide scripts and when things go wrong throw in a few error messages. This write-up is no exception, I will be using two basic scripts | I try to make UniCenter as practical as possible hence provide scripts, and when things go wrong, throw in a few error messages. This write-up is no exception, I will be using two basic scripts, which are shown below. | ||
* '''mail.html''' --- Basic page containing a mail form. | * '''mail.html''' --- Basic page containing a mail form. | ||
Line 76: | Line 76: | ||
'''Above php code''' | '''Above php code''' | ||
The above basic code works however it is inconvenient to use. Every time you run the form (mail.htlm) all text fields require an entry. | The above basic code works as is, however it is inconvenient to use. Every time you run the form (mail.htlm) all the text fields require an entry. | ||
I am lazy and it becomes a | I am lazy and it becomes a chore, especially if you only want to change one field and to ignore the others. | ||
I have modified this basic script, it inserts default values for empty form text boxes. This new version is the one downloadable using the above link. | I have modified this basic script, it inserts default values for empty form text boxes. This new version is the one downloadable using the above link. | ||
Line 88: | Line 88: | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
== A better mail.php script == | == A better mail.php script == | ||
A better '''mail.php''' test script | A better '''mail.php''' test script. It adds default values to empty text fields. After saving this script, one minor change is required to personalise it for your own use. | ||
Open the file '''mail.php''' and change it as follows: | Open the file '''mail.php''' and change it as follows: | ||
Line 148: | Line 148: | ||
Change this to your email address as provided by your ISP. | Change this to your email address as provided by your ISP. | ||
For example if your email address is '''fred123@xip.com''' the line will look similar to this: | For example, if your email address is '''fred123@xip.com''', the line will look similar to this: | ||
if (empty($_POST['email'])){<br> | if (empty($_POST['email'])){<br> | ||
Line 164: | Line 164: | ||
Start UniServer and type the following into your browser address bar: '''<nowiki>http://localhost/mail.html</nowiki>''' | Start UniServer and type the following into your browser address bar: '''<nowiki>http://localhost/mail.html</nowiki>''' | ||
There is no need to enter any data into the text boxes | There is no need to enter any data into the text boxes. Just click on '''Send''' and with luck you will receive this error message: | ||
{|cellpadding="8" | {|cellpadding="8" | ||
|-style="background:#e4e4e4" | |-style="background:#e4e4e4" | ||
Line 175: | Line 175: | ||
|} | |} | ||
This error message was expected | This error message was expected. It highlights the problem we are trying to resolve. Our script produced the last line and the warning was produced by PHP. | ||
PHP was looking for a mail server to use | PHP was looking for a mail server to use. It can’t find one so it gives up. The error message is actually very informative. It's telling us where the problem is and in what file. | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
== php.ini == | == php.ini == | ||
Open file php.ini located in folder *\Uniform Server\udrive\usr\local\php | Open file php.ini located in folder *\Uniform Server\udrive\usr\local\php. Search for this section: [mail function] (starts at around line 612) | ||
{|cellpadding="8" | {|cellpadding="8" | ||
Line 205: | Line 205: | ||
<nowiki>;</nowiki> mail.force_extra_paramaters = | <nowiki>;</nowiki> mail.force_extra_paramaters = | ||
| | | | ||
These are the defaults settings | These are the defaults settings. You can see that PHP is looking for an SMTP server on the '''localhost''' machine and will use port '''25''' to communicate with it. | ||
There is no reason why this SMTP server should be local. In theory you can use anyone’s SMTP server for | There is no reason why this SMTP server should be local. In theory you can use anyone’s SMTP server for relaying your mail to the appropriate recipient. | ||
(It really is a shame but spammers have killed off this open-relay mechanism) | (It really is a shame, but spammers have killed off this open-relay mechanism.) | ||
ISP’s now disable open relaying and check you are a valid customer before allowing your email to be relayed through their SMPT servers. | ISP’s now disable open relaying and check if you are a valid customer before allowing your email to be relayed through their SMPT servers. | ||
'''''There are two methods they use for validation'':''' | '''''There are two methods they use for validation'':''' | ||
# You must be directly connected to their system as a paying user (your normal ISP Internet connection). | # You must be directly connected to their system as a paying user (your normal ISP Internet connection). | ||
# You are not directly connected to their system | # You are not directly connected to their system, so you need a name and password to access their SMTP server. | ||
|} | |} | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
== Solution 1 == | == Solution 1 == | ||
Since we are not running our own SMTP server and because of validation we are restricted to using our ISP’s SMTP server. To use this server all that is required is to point PHP | Since we are not running our own SMTP server and because of validation, we are restricted to using our ISP’s SMTP server. To use this server, all that is required is to point PHP at it. To do this you need to known the name of this server. A quick way to find it is to check what settings you have in Outlook Express (or the mail client you are using). | ||
For Outlook Express use: | For Outlook Express use: | ||
Line 227: | Line 227: | ||
# This brings up a new form, select the '''Mail Tab''' and click on the '''default account name''', Click on '''properties''' | # This brings up a new form, select the '''Mail Tab''' and click on the '''default account name''', Click on '''properties''' | ||
# A new menu is opened, Click on the '''Servers Tab''' | # A new menu is opened, Click on the '''Servers Tab''' | ||
# The text box named '''Outgoing mails (SMTP)''' contains the name of the server | # The text box named '''Outgoing mails (SMTP)''' contains the name of the server. For example, '''smtp.xip.com''' | ||
Alternatively check your ISP’s web site and look for "how to set up an email client" | Alternatively check your ISP’s web site and look for "how to set up an email client." The SMPT server name will found there. | ||
{|cellpadding="8" | {|cellpadding="8" | ||
Line 255: | Line 255: | ||
'''''SMTP'':''' | '''''SMTP'':''' | ||
Replace localhost with your ISP's SMTP server name | Replace localhost with your ISP's SMTP server name. In this example I am using '''smtp.xip.com'''. | ||
'''''smtp_port'':''' | '''''smtp_port'':''' | ||
Line 263: | Line 263: | ||
'''''sendmail_from'':''' | '''''sendmail_from'':''' | ||
When a recipient reads their email this email address is displayed in their “from box”. If you expect a reply you should use a real email address. It does not have to be one provided by your ISP. | When a recipient reads their email, this email address is displayed in their “from box”. If you expect a reply you should use a real email address. It does not have to be one provided by your ISP. | ||
Replace '''me@localhost.com''' with a real email address in this example I am using '''fred123@xip.com''' | Replace '''me@localhost.com''' with a real email address in this example I am using '''fred123@xip.com''' | ||
Line 279: | Line 279: | ||
* '''Thank you for sending email''' | * '''Thank you for sending email''' | ||
Login to your mail account and check you received the message. | Login to your mail account and check if you received the message. | ||
'''''Test 2'':''' | '''''Test 2'':''' | ||
To check your ISP relays to other SMTP servers. Send an email to a friend or to another account you own | To check if your ISP relays to other SMTP servers. Send an email to a friend or to another account you own. '''Make sure your ISP is not hosting these accounts'''. | ||
Run the test page '''<nowiki>http://localhost/mail.html</nowiki>''' | Run the test page '''<nowiki>http://localhost/mail.html</nowiki>.''' This time you will need to fill in the text boxes. | ||
Confirm that the emails are received | Confirm that the emails are received and that completes the tests. | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
== Problems == | == Problems == | ||
In the real world things never run that smoothly | In the real world things never run that smoothly. Something always throws a spanner in the works. If the above fails, use this checklist to see if it either resolves the problem or points to where the problem is: | ||
# You need to be connected to the Internet (via your ISP) before running your PHP program. | # You need to be connected to the Internet (via your ISP) before running your PHP program. | ||
# If your host requires authentication for outgoing mail you cannot use the PHP mail function it does not support it. | # If your host requires authentication for outgoing mail you cannot use the PHP mail function because it does not support it. | ||
# Your service provider may be using an alternative port and not 25 (unlikely) Note: Check what you use in Outlook Express to confirm 2) and 3) | # Your service provider may be using an alternative port and not 25 (unlikely) Note: Check what you use in Outlook Express to confirm 2) and 3) | ||
# Make sure your firewall or antivirus program is not blocking '''php.exe''' Internet access (port25). | # Make sure your firewall or antivirus program is not blocking '''php.exe''' Internet access (port25). | ||
I think that covers most causes | I think that covers most causes. You may be surprised that 1) is the prime candidate, especially when testing. | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
Line 303: | Line 303: | ||
The above solution is quick to implement and allows you to test email scripts and any third party scripts that use PHP’s mail function. | The above solution is quick to implement and allows you to test email scripts and any third party scripts that use PHP’s mail function. | ||
One real drawback with this solution it cannot be used if your service provider requires SMTP authentication. | One real drawback with this solution is that it cannot be used if your service provider requires SMTP authentication. | ||
I noticed on the forum | I noticed on the forum that to over come this problem Olajide recommends using '''fake sendmail for Windows'''. For completeness I cover this on the [[installing Fake Sendmail for Windows |next page]] solution 2. | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' |