PHP mail function: Difference between revisions

From The Uniform Server Wiki
Jump to navigation Jump to search
No edit summary
m (Reverted edits by Upazixorys (Talk); changed back to last version by BobS)
 
Line 1: Line 1:
----
<span id="top"></span>
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
<div style="padding:0;margin:0; border-bottom:3px inset #000000">
----
=[http://ovarynetyv.co.cc This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page]=
----
=[http://ovarynetyv.co.cc CLICK HERE]=
----
</div>
&lt;span id=&quot;top&quot;&gt;&lt;/span&gt;
&lt;div style=&quot;padding:0;margin:0; border-bottom:3px inset #000000&quot;&gt;
{|  
{|  
| [[Image:uc_small_logo.gif | MPG UniCenter]] ||
| [[Image:uc_small_logo.gif | MPG UniCenter]] ||
Line 14: Line 6:
[[Installing Fake Sendmail for Windows | Installing Fake Sendmail for Windows]]
[[Installing Fake Sendmail for Windows | Installing Fake Sendmail for Windows]]
|}
|}
&lt;/div&gt;
</div>
{| cellpadding=&quot;2&quot;
{| cellpadding="2"
|
|
__TOC__
__TOC__
Line 34: Line 26:


{|
{|
|-style=&quot;background:#e4e4e4&quot;
|-style="background:#e4e4e4"
!mail.html
!mail.html
!mail.php
!mail.php
|-style=&quot;background:#f0f0f0&quot;
|-style="background:#f0f0f0"
|valign=&quot;top&quot;|
|valign="top"|
&lt;pre style=&quot;border:none;background:#f0f0f0&quot;&gt;
<pre style="border:none;background:#f0f0f0">
&lt;html&gt;
<html>
&lt;head&gt;&lt;title&gt;Mail sender&lt;/title&gt;&lt;/head&gt;
<head><title>Mail sender</title></head>
&lt;body&gt;
<body>
&lt;form action=&quot;mail.php&quot; method=&quot;POST&quot;&gt;
<form action="mail.php" method="POST">
&lt;b&gt;Email&lt;/b&gt;&lt;br&gt;
<b>Email</b><br>
&lt;input type=&quot;text&quot; name=&quot;email&quot; size=40&gt;
<input type="text" name="email" size=40>
&lt;p&gt;&lt;b&gt;Subject&lt;/b&gt;&lt;br&gt;
<p><b>Subject</b><br>
&lt;input type=&quot;text&quot; name=&quot;subject&quot; size=40&gt;
<input type="text" name="subject" size=40>
&lt;p&gt;&lt;b&gt;Message&lt;/b&gt;&lt;br&gt;
<p><b>Message</b><br>
&lt;textarea cols=40 rows=10 name=&quot;message&quot;&gt;&lt;/textarea&gt;
<textarea cols=40 rows=10 name="message"></textarea>
&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot; Send &quot;&gt;
<p><input type="submit" value=" Send ">
&lt;/form&gt;
</form>
&lt;/body&gt;
</body>
&lt;/html&gt;
</html>
&lt;/pre&gt;
</pre>
|
|
&lt;pre style=&quot;border:none;background:#f0f0f0&quot;&gt;
<pre style="border:none;background:#f0f0f0">
&lt;html&gt;
<html>
&lt;head&gt;&lt;title&gt;PHP Mail Sender&lt;/title&gt;&lt;/head&gt;
<head><title>PHP Mail Sender</title></head>
&lt;body&gt;
<body>
&lt;?php
<?php


# Retrieve the form data
# Retrieve the form data
Line 69: Line 61:
# Sends mail and report success or failure
# Sends mail and report success or failure
if (mail($email,$subject,$message)) {
if (mail($email,$subject,$message)) {
   echo &quot;&lt;h4&gt;Thank you for sending email&lt;/h4&gt;&quot;;
   echo "<h4>Thank you for sending email</h4>";
} else {
} else {
   echo &quot;&lt;h4&gt;Can't send email to $email&lt;/h4&gt;&quot;;
   echo "<h4>Can't send email to $email</h4>";
}
}
?&gt;
?>
&lt;/body&gt;
</body>
&lt;/html&gt;
</html>
&lt;/pre&gt;
</pre>
|-
|-
|
|
'''Produces a form similar to this:'''
'''Produces a form similar to this:'''
[[Image:Uc_php_mail_form.gif]]
[[Image:Uc_php_mail_form.gif]]
|valign=&quot;top&quot;|
|valign="top"|
'''Above php code'''
'''Above php code'''


Line 91: Line 83:


The modified script is shows below.
The modified script is shows below.
|width=&quot;50%&quot;|  
|width="50%"|  
|}
|}


Line 100: Line 92:
Open the file '''mail.php''' and change it as follows:
Open the file '''mail.php''' and change it as follows:


&lt;table cellpadding=&quot;2&quot;&gt;
<table cellpadding="2">
&lt;tr&gt;
<tr>
&lt;td&gt;
<td>
{| cellpadding=&quot;2&quot; cellspacing=&quot;1&quot; style=&quot;background:#000000;&quot;
{| cellpadding="2" cellspacing="1" style="background:#000000;"
|- style=&quot;background:#e8e8e8;&quot;
|- style="background:#e8e8e8;"
!mail.php
!mail.php
|- style=&quot;background:#f5f5f5;&quot;
|- style="background:#f5f5f5;"
|
|
&lt;div style=&quot;padding:4px;&quot;&gt;
<div style="padding:4px;">
&lt;html&gt;&lt;br&gt;
<html><br>
&lt;head&gt;&lt;title&gt;PHP Mail Sender&lt;/title&gt;&lt;/head&gt;&lt;br&gt;
<head><title>PHP Mail Sender</title></head><br>
&lt;body&gt;&lt;br&gt;
<body><br>
&lt;?php
<?php


&lt;nowiki&gt;#&lt;/nowiki&gt; Retrieve the form data&lt;br&gt;
<nowiki>#</nowiki> Retrieve the form data<br>
$email = $_POST['email'];&lt;br&gt;
$email = $_POST['email'];<br>
$subject = $_POST['subject'];&lt;br&gt;
$subject = $_POST['subject'];<br>
$message = $_POST['message'];
$message = $_POST['message'];


&lt;nowiki&gt;#&lt;/nowiki&gt; Check if a $_POST value exists&lt;br&gt;
<nowiki>#</nowiki> Check if a $_POST value exists<br>
&lt;nowiki&gt;#&lt;/nowiki&gt; If not use defaults&lt;br&gt;
<nowiki>#</nowiki> If not use defaults<br>
&lt;nowiki&gt;if (empty($_POST['email'])){&lt;/nowiki&gt;&lt;br&gt;
<nowiki>if (empty($_POST['email'])){</nowiki><br>
&amp;nbsp;&amp;nbsp;$email = &quot;'''xxx@yyy.com'''&quot;;&lt;br&gt;
&nbsp;&nbsp;$email = "'''xxx@yyy.com'''";<br>
}&lt;br&gt;
}<br>
if (empty($_POST['subject'])){&lt;br&gt;
if (empty($_POST['subject'])){<br>
&amp;nbsp;&amp;nbsp;$subject = &quot;UniCenter test&quot;;&lt;br&gt;
&nbsp;&nbsp;$subject = "UniCenter test";<br>
&lt;nowiki&gt;}&lt;/nowiki&gt;&lt;br&gt;
<nowiki>}</nowiki><br>
if (empty($_POST['message'])){&lt;br&gt;
if (empty($_POST['message'])){<br>
&amp;nbsp;&amp;nbsp;$message = &quot;A quick test from UniCenter&quot;;&lt;br&gt;
&nbsp;&nbsp;$message = "A quick test from UniCenter";<br>
&lt;nowiki&gt;}&lt;/nowiki&gt;
<nowiki>}</nowiki>


&lt;nowiki&gt;#&lt;/nowiki&gt; Sends mail and report success or failure&lt;br&gt;
<nowiki>#</nowiki> Sends mail and report success or failure<br>
&lt;nowiki&gt;if (mail($email,$subject,$message)) {&lt;/nowiki&gt;&lt;br&gt;
<nowiki>if (mail($email,$subject,$message)) {</nowiki><br>
&amp;nbsp;&amp;nbsp;echo &quot;&lt;nowiki&gt;&lt;h4&gt;Thank you for sending email&lt;/h4&gt;&lt;/nowiki&gt;&quot;;&lt;br&gt;
&nbsp;&nbsp;echo "<nowiki><h4>Thank you for sending email</h4></nowiki>";<br>
&lt;nowiki&gt;}&lt;/nowiki&gt; else {&lt;br&gt;
<nowiki>}</nowiki> else {<br>
&amp;nbsp;&amp;nbsp;echo&amp;nbsp;&quot;&lt;nowiki&gt;&lt;h4&gt;&lt;/nowiki&gt;Can't&amp;nbsp;send&amp;nbsp;email&amp;nbsp;to&amp;nbsp;$email&lt;nowiki&gt;&lt;/h4&gt;&lt;/nowiki&gt;&quot;;&lt;br&gt;
&nbsp;&nbsp;echo&nbsp;"<nowiki><h4></nowiki>Can't&nbsp;send&nbsp;email&nbsp;to&nbsp;$email<nowiki></h4></nowiki>";<br>
&lt;nowiki&gt;}&lt;/nowiki&gt;&lt;br&gt;
<nowiki>}</nowiki><br>
?&gt;&lt;br&gt;
?><br>
&lt;/body&gt;&lt;br&gt;
</body><br>
&lt;/html&gt;
</html>
&lt;/div&gt;
</div>
|}
|}
&lt;/td&gt;
</td>
&lt;td&gt;
<td>
&amp;nbsp;
&nbsp;
&lt;/td&gt;
</td>
&lt;td&gt;
<td>
{| cellpadding=&quot;2&quot; cellspacing=&quot;1&quot; style=&quot;background:#000000;&quot;
{| cellpadding="2" cellspacing="1" style="background:#000000;"
|- style=&quot;background:#e8e8e8;&quot;
|- style="background:#e8e8e8;"
! Comments
! Comments
|- style=&quot;background:#f5f5f5;&quot;
|- style="background:#f5f5f5;"
|  
|  
Locate the the email address highlighted in bold.
Locate the the email address highlighted in bold.
Line 158: Line 150:
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'])){&lt;br&gt;
if (empty($_POST['email'])){<br>
$email = &quot;'''fred123@xip.com'''&quot;;&lt;br&gt;
$email = "'''fred123@xip.com'''";<br>
}
}


Change default '''subject''' and '''message''' if you wish.
Change default '''subject''' and '''message''' if you wish.
|}
|}
&lt;/td&gt;
</td>
&lt;/tr&gt;
</tr>
&lt;/table&gt;
</table>


'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''
== Running the test Scripts ==
== Running the test Scripts ==
Start UniServer and type the following into your browser address bar: '''&lt;nowiki&gt;http://localhost/mail.html&lt;/nowiki&gt;'''
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. Just click on '''Send''' and with luck you will receive this error message:
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=&quot;8&quot;
{|cellpadding="8"
|-style=&quot;background:#e4e4e4&quot;
|-style="background:#e4e4e4"
!error message
!error message
|-style=&quot;background:#f0f0f0&quot;
|-style="background:#f0f0f0"
|
|
Warning: mail() [function.mail]: Failed to connect to mailserver at &quot;localhost&quot; port 25, verify your &quot;SMTP&quot; and &quot;smtp_port&quot; setting in php.ini or use ini_set() in W:\www\mail.php on line 24
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in W:\www\mail.php on line 24


Can't send email to fred123@xip.com '''''Note: should display your real email address'''''
Can't send email to fred123@xip.com '''''Note: should display your real email address'''''
Line 191: Line 183:
Open file php.ini located in folder *\Uniform Server\udrive\usr\local\php. Search for this section: [mail function] (starts at around line 612)
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=&quot;8&quot;
{|cellpadding="8"
|-style=&quot;background:#e4e4e4&quot;
|-style="background:#e4e4e4"
!php.ini section [mail function]
!php.ini section [mail function]
!Comments
!Comments
|--style=&quot;background:#f0f0f0&quot;
|--style="background:#f0f0f0"
|
|
[mail function]&lt;br&gt;
[mail function]<br>
&lt;nowiki&gt;;&lt;/nowiki&gt; For Win32 only.&lt;br&gt;
<nowiki>;</nowiki> For Win32 only.<br>
SMTP = '''localhost'''&lt;br&gt;
SMTP = '''localhost'''<br>
smtp_port = '''25'''
smtp_port = '''25'''


&lt;nowiki&gt;;&lt;/nowiki&gt; For Win32 only.&lt;br&gt;
<nowiki>;</nowiki> For Win32 only.<br>
sendmail_from = me@localhost.com
sendmail_from = me@localhost.com


&lt;nowiki&gt;;&lt;/nowiki&gt;For&amp;nbsp;Unix&amp;nbsp;only. You may supply arguments as well (default: ;&quot;sendmail -t -i&quot;).&lt;br&gt;
<nowiki>;</nowiki>For&nbsp;Unix&nbsp;only. You may supply arguments as well (default: ;"sendmail -t -i").<br>
&lt;nowiki&gt;;&lt;/nowiki&gt;sendmail_path = &quot;/usr/bin/sendmail.exe -t&quot;
<nowiki>;</nowiki>sendmail_path = "/usr/bin/sendmail.exe -t"


&lt;nowiki&gt;;&lt;/nowiki&gt;&amp;nbsp;Force&amp;nbsp;the&amp;nbsp;addition&amp;nbsp;of&amp;nbsp;the&amp;nbsp;specified&amp;nbsp;parameters&amp;nbsp;to&amp;nbsp;be&amp;nbsp;passed&amp;nbsp;as&amp;nbsp;extra&amp;nbsp;parameters&lt;br&gt;
<nowiki>;</nowiki>&nbsp;Force&nbsp;the&nbsp;addition&nbsp;of&nbsp;the&nbsp;specified&nbsp;parameters&nbsp;to&nbsp;be&nbsp;passed&nbsp;as&nbsp;extra&nbsp;parameters<br>
&lt;nowiki&gt;;&lt;/nowiki&gt; to the sendmail binary. These parameters will always replace the value of&lt;br&gt;
<nowiki>;</nowiki> to the sendmail binary. These parameters will always replace the value of<br>
&lt;nowiki&gt;;&lt;/nowiki&gt; the 5th parameter to mail(), even in safe mode.&lt;br&gt;
<nowiki>;</nowiki> the 5th parameter to mail(), even in safe mode.<br>
&lt;nowiki&gt;;&lt;/nowiki&gt; mail.force_extra_paramaters =
<nowiki>;</nowiki> mail.force_extra_paramaters =
|
|
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.
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.
Line 237: Line 229:
# The text box named '''Outgoing mails (SMTP)''' contains the name of the server. For example, '''smtp.xip.com'''
# 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 &quot;how to set up an email client.&quot; The SMPT server name will found there.
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=&quot;8&quot;
{|cellpadding="8"
|-style=&quot;background:#e4e4e4&quot;
|-style="background:#e4e4e4"
!php.ini section [mail function]
!php.ini section [mail function]
!Comments
!Comments
|--style=&quot;background:#f0f0f0&quot;
|--style="background:#f0f0f0"
|
|
[mail function]&lt;br&gt;
[mail function]<br>
&lt;nowiki&gt;;&lt;/nowiki&gt; For Win32 only.&lt;br&gt;
<nowiki>;</nowiki> For Win32 only.<br>
SMTP = '''smtp.xip.com'''&lt;br&gt;
SMTP = '''smtp.xip.com'''<br>
smtp_port = '''25'''
smtp_port = '''25'''


&lt;nowiki&gt;;&lt;/nowiki&gt; For Win32 only.&lt;br&gt;
<nowiki>;</nowiki> For Win32 only.<br>
sendmail_from = '''me@localhost.com'''
sendmail_from = '''me@localhost.com'''


&lt;nowiki&gt;;&lt;/nowiki&gt;For&amp;nbsp;Unix&amp;nbsp;only. You may supply arguments as well (default: ;&quot;sendmail -t -i&quot;).&lt;br&gt;
<nowiki>;</nowiki>For&nbsp;Unix&nbsp;only. You may supply arguments as well (default: ;"sendmail -t -i").<br>
&lt;nowiki&gt;;&lt;/nowiki&gt;sendmail_path = &quot;/usr/bin/sendmail.exe -t&quot;
<nowiki>;</nowiki>sendmail_path = "/usr/bin/sendmail.exe -t"


&lt;nowiki&gt;;&lt;/nowiki&gt;&amp;nbsp;Force&amp;nbsp;the&amp;nbsp;addition&amp;nbsp;of&amp;nbsp;the&amp;nbsp;specified&amp;nbsp;parameters&amp;nbsp;to&amp;nbsp;be&amp;nbsp;passed&amp;nbsp;as&amp;nbsp;extra&amp;nbsp;parameters&lt;br&gt;
<nowiki>;</nowiki>&nbsp;Force&nbsp;the&nbsp;addition&nbsp;of&nbsp;the&nbsp;specified&nbsp;parameters&nbsp;to&nbsp;be&nbsp;passed&nbsp;as&nbsp;extra&nbsp;parameters<br>
&lt;nowiki&gt;;&lt;/nowiki&gt; to the sendmail binary. These parameters will always replace the value of&lt;br&gt;
<nowiki>;</nowiki> to the sendmail binary. These parameters will always replace the value of<br>
&lt;nowiki&gt;;&lt;/nowiki&gt; the 5th parameter to mail(), even in safe mode.&lt;br&gt;
<nowiki>;</nowiki> the 5th parameter to mail(), even in safe mode.<br>
&lt;nowiki&gt;;&lt;/nowiki&gt; mail.force_extra_paramaters =
<nowiki>;</nowiki> mail.force_extra_paramaters =
|
|
'''''SMTP'':'''
'''''SMTP'':'''
Line 280: Line 272:
'''''Test1'':'''
'''''Test1'':'''


Start UniServer and type the following into your browser address bar: '''&lt;nowiki&gt;http://localhost/mail.html&lt;/nowiki&gt;'''
Start UniServer and type the following into your browser address bar: '''<nowiki>http://localhost/mail.html</nowiki>'''


Again there is no need to enter any data into the text boxes just click on Send you will receive this message:
Again there is no need to enter any data into the text boxes just click on Send you will receive this message:
Line 293: Line 285:
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'''.
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 '''&lt;nowiki&gt;http://localhost/mail.html&lt;/nowiki&gt;.''' This time you will need to fill in the text boxes.
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 and that completes the tests.
Confirm that the emails are received and that completes the tests.



Latest revision as of 08:37, 24 November 2010

MPG UniCenter

PHP mail function | Installing Fake Sendmail for Windows

Uniform Server 3.5-Apollo.

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

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

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.php --- Script that processes the above form information
mail.html mail.php
<html>
<head><title>Mail sender</title></head>
<body>
<form action="mail.php" method="POST">
<b>Email</b><br>
<input type="text" name="email" size=40>
<p><b>Subject</b><br>
<input type="text" name="subject" size=40>
<p><b>Message</b><br>
<textarea cols=40 rows=10 name="message"></textarea>
<p><input type="submit" value=" Send ">
</form>
</body>
</html>
<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php

# Retrieve the form data
$email      = $_POST['email'];
$subject    = $_POST['subject'];
$message = $_POST['message'];

# Sends mail and report success or failure
if (mail($email,$subject,$message)) {
  echo "<h4>Thank you for sending email</h4>";
} else {
  echo "<h4>Can't send email to $email</h4>";
}
?>
</body>
</html>

Produces a form similar to this:

Above php code

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 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.

The modified script is shows below.

Top

A better mail.php 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:

mail.php

<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php

# Retrieve the form data
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];

# Check if a $_POST value exists
# If not use defaults
if (empty($_POST['email'])){
  $email = "xxx@yyy.com";
}
if (empty($_POST['subject'])){
  $subject = "UniCenter test";
}
if (empty($_POST['message'])){
  $message = "A quick test from UniCenter";
}

# Sends mail and report success or failure
if (mail($email,$subject,$message)) {
  echo "<h4>Thank you for sending email</h4>";
} else {
  echo "<h4>Can't send email to $email</h4>";
}
?>
</body>
</html>

 

Comments

Locate the the email address highlighted in bold.

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:

if (empty($_POST['email'])){
$email = "fred123@xip.com";
}

Change default subject and message if you wish.

Top

Running the test Scripts

Start UniServer and type the following into your browser address bar: http://localhost/mail.html

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:

error message

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in W:\www\mail.php on line 24

Can't send email to fred123@xip.com Note: should display your real email address

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. 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

php.ini

Open file php.ini located in folder *\Uniform Server\udrive\usr\local\php. Search for this section: [mail function] (starts at around line 612)

php.ini section [mail function] Comments

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
sendmail_from = me@localhost.com

;For Unix only. You may supply arguments as well (default: ;"sendmail -t -i").
;sendmail_path = "/usr/bin/sendmail.exe -t"

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
; mail.force_extra_paramaters =

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 relaying your mail to the appropriate recipient. (It really is a shame, but spammers have killed off this open-relay mechanism.)

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:

  1. You must be directly connected to their system as a paying user (your normal ISP Internet connection).
  2. You are not directly connected to their system, so you need a name and password to access their SMTP server.

Top

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 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:

  1. On the top menu Click on Tools and select accounts
  2. This brings up a new form, select the Mail Tab and click on the default account name, Click on properties
  3. A new menu is opened, Click on the Servers Tab
  4. 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." The SMPT server name will found there.

php.ini section [mail function] Comments

[mail function]
; For Win32 only.
SMTP = smtp.xip.com
smtp_port = 25

; For Win32 only.
sendmail_from = me@localhost.com

;For Unix only. You may supply arguments as well (default: ;"sendmail -t -i").
;sendmail_path = "/usr/bin/sendmail.exe -t"

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
; mail.force_extra_paramaters =

SMTP:

Replace localhost with your ISP's SMTP server name. In this example I am using smtp.xip.com.

smtp_port:

Leave the port set to 25

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.

Replace me@localhost.com with a real email address in this example I am using fred123@xip.com

Top

Test Solution 1

Test1:

Start UniServer and type the following into your browser address bar: http://localhost/mail.html

Again there is no need to enter any data into the text boxes just click on Send you will receive this message:

  • A page is displayed with the following message:
  • Thank you for sending email

Login to your mail account and check if you received the message.

Test 2:

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 http://localhost/mail.html. This time you will need to fill in the text boxes. Confirm that the emails are received and that completes the tests.

Top

Problems

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:

  1. You need to be connected to the Internet (via your ISP) before running your PHP program.
  2. If your host requires authentication for outgoing mail you cannot use the PHP mail function because it does not support it.
  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)
  4. Make sure your firewall or antivirus program is not blocking php.exe Internet access (port25).

I think that covers most causes. You may be surprised that 1) is the prime candidate, especially when testing.

Top

Summary

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 is that it cannot be used if your service provider requires SMTP authentication.

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 next page solution 2.

Top


Ric