https://wiki.uniformserver.com/index.php?title=SSL_Part_2:_PHP_mail_function&feed=atom&action=historySSL Part 2: PHP mail function - Revision history2024-03-29T13:50:46ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.uniformserver.com/index.php?title=SSL_Part_2:_PHP_mail_function&diff=4918&oldid=prevOlajideolaolorun: Reverted edits by Upazixorys (Talk); changed back to last version by Ric2010-11-24T17:36:25Z<p>Reverted edits by <a href="/Special:Contributions/Upazixorys" title="Special:Contributions/Upazixorys">Upazixorys</a> (<a href="/index.php?title=User_talk:Upazixorys&action=edit&redlink=1" class="new" title="User talk:Upazixorys (page does not exist)">Talk</a>); changed back to last version by <a href="/User:Ric" title="User:Ric">Ric</a></p>
<a href="https://wiki.uniformserver.com/index.php?title=SSL_Part_2:_PHP_mail_function&diff=4918&oldid=4900">Show changes</a>Olajideolaolorunhttps://wiki.uniformserver.com/index.php?title=SSL_Part_2:_PHP_mail_function&diff=4900&oldid=prevUpazixorys at 10:25, 24 November 20102010-11-24T10:25:49Z<p></p>
<a href="https://wiki.uniformserver.com/index.php?title=SSL_Part_2:_PHP_mail_function&diff=4900&oldid=3115">Show changes</a>Upazixoryshttps://wiki.uniformserver.com/index.php?title=SSL_Part_2:_PHP_mail_function&diff=3115&oldid=prevRic: New page: <span id="top"></span> <div style="padding:0;margin:0; border-bottom:3px inset #000000"> {| | MPG UniCenter || SSL Part 2: Home | [[...2008-06-09T17:58:36Z<p>New page: <span id="top"></span> <div style="padding:0;margin:0; border-bottom:3px inset #000000"> {| | <a href="/File:Uc_small_logo.gif" title="File:Uc small logo.gif"> MPG UniCenter</a> || SSL Part 2: <a href="/SSL_Part_2:_Home" title="SSL Part 2: Home"> Home</a> | [[...</p>
<p><b>New page</b></p><div><span id="top"></span><br />
<div style="padding:0;margin:0; border-bottom:3px inset #000000"><br />
{| <br />
| [[Image:uc_small_logo.gif | MPG UniCenter]] ||<br />
SSL Part 2: <br />
[[SSL Part 2: Home | Home]] | <br />
[[SSL Part 2: PC Lock Down | Lock Down]] | <br />
[[SSL Part 2: No IP | No IP]] | <br />
[[SSL Part 2: Installing hMailServer | hMailServer]] |<br />
[[SSL Part 2: Configure hMailServer | Config hMailServer]] | <br />
[[SSL Part 2: PHP mail function |PHP mail function]] | <br />
[[SSL Part 2: Generate CSR |Generate CSR]] | <br />
[[SSL Part 2: CAcert Introduction |CAcert]] | <br />
[[SSL Part 2: CAcert Signing Process |CAcert Signing Process]]<br />
<br />
|}<br />
</div><br />
{| cellpadding="2"<br />
|<br />
__TOC__<br />
||<br />
'''PHP mail function problem'''<br>'''Signed Certificate Project'''<br>Uniform Server 3.5-Apollo'''<br />
|}<br />
With your e-mail server working I suppose one of the first things to try is PHP’s mail function. A very simple looking function with the power to make you rip your hair out when you discover it does not work.<br />
<br />
The truth is it does work, in reality it’s a lack of understanding, which this page attempts to address. When I used this function for the first it failed imagine my surprise when I discovered it required a mail server. Even after acquiring a mail server it persistently refused to work.<br />
<br />
Most problems are solvable however first you need to understand what the failure mechanism is. The following examples use our mail-sever as describe on the previous two pages to investigate PHP’s mail function and server interaction.<br />
<br />
== Basic PHP mail function ==<br />
All examples use a variant of the following script running on Uniform Server 3.5-Apolo. I assume you have set-up your email client with a “postmaster” account ([[SSL Part 2: Configure hMailServer | see previous page]])<br />
<br />
{|<br />
|-<br />
|<br />
{| cellpadding="2" cellspacing="1" style="background:#000000;"<br />
|- style="background:#e8e8e8;"<br />
! mail_test_1.php<br />
|- style="background:#f5f5f5;"<br />
|<br />
<pre style="border:none"><br />
<html><br />
<head><title>Mail Test 1</title></head><br />
<body><br />
<br />
<?php<br />
$to_email = "postmaster@mpg123.no-ip.org"; <br />
$subject = "Mail Test 1"; <br />
$message = "Basic PHP mail function";<br />
<br />
if (mail($to_email,$subject,$message)){<br />
echo '<br>Mail accepted for delivery<br>';<br />
}<br />
else{<br />
echo 'There was an error. Mail not accepted for delivery';<br />
}<br />
?><br />
</body><br />
</html><br />
</pre><br />
|}<br />
|valign="top"|<br />
'''Test 1:'''<br />
{|<br />
|-<br />
|'''Run script:'''||Type into your browser <nowiki>http://localhost/mail_test_1.php</nowiki><br />
|-<br />
|'''Result:'''||Mail accepted for delivery<br />
|-<br />
|'''Email client:'''||style="background:#e8e8e8;"|From: me@localhost.com<br />
|-<br />
|&nbsp;||Subject: Mail Test 1<br />
|}<br />
<br />
The script clearly works, mailed was delivered to our recipient “postmaster” perhaps what is a little surprising is where the mail came from me@localhost.com!<br />
<br />
That e-mail address does not exist, either in our script or e-mail server. A recipient wanting to reply clearly would have a problem.<br />
<br />
The address is automatically added by PHP. Address used is that set in the '''php.ini''' file located in folder *\Uniform Server\udrive\usr\local\php.<br />
<br />
Line 617: '''sendmail_from = me@localhost.com'''<br />
|}<br />
<br />
Suppose you want all your PHP e-mail scripts to reply to a single address changing the php.ini file to that address would achieve the result required. To see this in action change the line in php.ini to '''sendmail_from = me2@localhost.com''' run the above script again. This is a small change to illustrate a point and not a practical solution.<br />
<br />
'''''Note''''': The above is '''extremely important''' you need to think of the combined script and php.ini as a mini-smtp server. It will only relay (deliver) mail to the local server (Line 614: smtp_port = 25) and to a single account defined by (Line 617) sendmail_from = me2@localhost.com.<br />
<br />
'''''[[#top | Top]]'''''<br />
== Basic PHP mail function - Problem ==<br />
The above demonstrates our script and mail server are working. This next test I am sure you will be familiar with or at least the resulting error message. Change the script, replace to email address with your real e-mail address (as provided by your ISP)<br />
<br />
{|<br />
|-<br />
|<br />
{| cellpadding="2" cellspacing="1" style="background:#000000;"<br />
|- style="background:#e8e8e8;"<br />
! mail_test_2.php<br />
|- style="background:#f5f5f5;"<br />
|<br />
<pre style="border:none"><br />
<html><br />
<head><title>Mail Test 1</title></head><br />
<body><br />
<br />
<?php<br />
$to_email = "real_name@real_domain"; <br />
$subject = "Mail Test 2"; <br />
$message = "Basic PHP mail function";<br />
<br />
if (mail($to_email,$subject,$message)){ // Line 10<br />
echo '<br>Mail accepted for delivery<br>';<br />
}<br />
else{<br />
echo 'There was an error. Mail not accepted for delivery';<br />
}<br />
?><br />
</body><br />
</html><br />
</pre><br />
|}<br />
|valign="top"|<br />
'''Test 2:'''<br />
{|<br />
|-<br />
|'''Run script:'''||Type into your browser <nowiki>http://localhost/mail_test_2.php</nowiki><br />
|-<br />
|'''Result:'''||style="background:#e8e8e8;"|<br />
'''Warning: mail() [function.mail]: SMTP server response: 550 Delivery is not allowed to this address. in W:\www\mail_test_1.php on line 10'''<br><br />
There was an error. Mail not accepted for delivery<br />
|}<br />
Error 550 means that this mail server does not allow relaying!<br />
The address (implied) on line 10 is your real e-mail address!<br />
<br />
'''Test:''' Using the '''postmaster account''' you set-up in your client. send yourself an email (to your ISP email account).<br />
<br />
The test demonstrates you can relay email through your email server, to your ISP and receive mail. Clearly the above error message is not referring to your real address.<br />
<br />
Remember the importance of test 1! Its referring to that address from the mini-smtp server. '''The problem is our local mail server sees this as an external server'''.<br />
|}<br />
<br />
----<br />
The answer to the problem is on [[SSL Part 2: Configure hMailServer| this page]] Internet IP ranges section 10 it defines what is allowed to relay through the server. I have reproduced the image below:<br />
{|<br />
|-<br />
|<br />
[[Image:Uc_hms_config_8.gif]]<br />
|valign="top"|<br />
A quick and disastrous solution is to check the bottom box and allow external-to-external relaying.<br />
<br />
This would be '''instant death''' for any e-mail server, not only would you be blacklisted but you open your server for every one to use where they can relay all unsavory stuff and spam.<br />
<br />
'''''Solution:'''''<br />
<br />
The solution is interesting all four selectable options have one thing in common '''accounts'''!<br />
<br />
Does our mini-server '''me2@localhost.com''' have an account on our hMailServer of course not we never set one-up.<br />
<br />
Do we have a domain '''localhost.com''' well we never set that up either.<br />
<br />
You can set-up an account for me2@localhost.com in any domain however it is preferable to create a new domain just in case you delete the one it resides in killing off all your PHP scripts.<br />
<br />
'''Creating a new domain and account see below:'''<br />
|} <br />
----<br />
{|<br />
|-<br />
|<br />
[[Image:Uc_hms_php_mail_1.gif]]<br />
|valign="top"|<br />
<br />
'''Create new domain:'''<br />
<br />
# Login to hMailServer<br />
# Click '''Welcome''' (left menu)<br />
# Click '''Add domain''' (right page)<br />
# Domain name: Enter '''localhost.com'''<br />
# Catch-all address: Enter '''me2''' and '''localhost.com'''<br />
# Click '''Save'''<br />
|} <br />
----<br />
{|<br />
|-<br />
|<br />
[[Image:Uc_hms_php_mail_2.gif]]<br />
|valign="top"|<br />
<br />
'''Create new account:'''<br />
<br />
For the catch-all address to become active it requires an account.<br />
<br />
# Account address: Enter me2<br />
# Password: Enter a password (me2pass)<br />
# Click Save<br />
''<br />
'''Note''''': The password is required only to create the account it is never used by scripts.<br />
|}<br />
<br />
----<br />
'''Re-Run:'''<br />
<br />
Repeat the original test:<br />
{|<br />
|-<br />
|'''Run script:'''|| Type into your browser <nowiki>http://localhost/mail_test_2.php</nowiki><br />
|-<br />
|'''Result:'''||styple="background:#efefef"| '''Mail accepted for delivery''' Indicates mail was sent (Script is working!)<br />
|-<br />
|'''Mail Client:'''&nbsp;&nbsp;&nbsp;|| Wait for the mail to be delivered. After a reasonable time refresh your e-mail client.<br />
|-<br />
|'''Result:'''||styple="background:#efefef"|From: '''me2@localhost.com'''<br />
|-<br />
|&nbsp;||Subject: Mail Test 2<br />
|}<br />
<br />
Both hMailServer and PHP has been set-up correctly however that strange from address (me2@localhost.com) needs resolving. You have two options depending on your requirements.<br />
<br />
'''''1) Common address'''''<br />
<br />
If all your scripts expect all recipients to reply to a single address you can change php.ini and hMailServer to reflect that address.<br />
<br />
'''''2) Generic address'''''<br />
<br />
Solution one is specific and inflexibly however you have a server that is capable of relaying any standard e-mail. The real solution lies in PHP’s mail function and the use of its fourth parameter covered in the next section. <br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Basic PHP mail function - Reply address ==<br />
PHP's basic mail function allows you to specify a fourth parameter. This parameter is primarily used for inserting MIME types however it also allows you to specify a “from address”.<br />
<br />
{|<br />
|-<br />
|<br />
{| cellpadding="2" cellspacing="1" style="background:#000000;"<br />
|- style="background:#e8e8e8;"<br />
! mail_test_3.php<br />
|- style="background:#f5f5f5;"<br />
|<br />
<pre style="border:none"><br />
<html><br />
<head><title>Mail Test 3</title></head><br />
<body><br />
<br />
<?php<br />
$to_email = "real_name@real_domain"; <br />
$subject = "Mail Test 3"; <br />
$message = "Basic PHP mail function";<br />
$from = "fred@someone.com";<br />
<br />
$headers ="From: ". $from . "\r\n";<br />
<br />
if (mail($to_email,$subject,$message,$headers)){<br />
echo '<br>Mail accepted for delivery<br>';<br />
}<br />
else{<br />
echo 'There was an error. Mail not accepted for delivery';<br />
}<br />
?><br />
</body><br />
</html><br />
</pre><br />
|}<br />
|valign="top"|<br />
'''Test 3:'''<br />
{|<br />
|-<br />
|'''$from:'''||Set this variable to the e-mail address you wish a recipient to reply to.<br />
|-<br />
|'''''Note:'''''|| The $headers string created must include \r\n to define a MIME boundary without it some mail clients may throw a wobbly!<br />
|}<br />
<br />
<br />
<br />
{|<br />
|-<br />
|valign="top"|'''Run script:'''||Type into your browser <nowiki>http://localhost/mail_test_3.php</nowiki><br>Wait a short time for the mail to be delivered and then read your e-mail.<br />
|-<br />
|'''Result:'''||style="background:#efefef"|The from address will display '''fred@someone.com'''<br />
|}<br />
|}<br />
<br />
==Summary ==<br />
I hope you found the above digression of interest; PHP’s basic mail function always seems to give new users grief. I faced the same problem because of a basic lack of understanding; thinking in terms of a mini-smtp server helps to visualise the problem. You may be interested in alternative solutions there are two covered on this page.<br />
<br />
Enough of this lets continue with our quest for a [[SSL Part 2: Generate CSR |signed certificate]].<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
----<br />
<br />
{| <br />
| [[Image:uc_small_logo.gif]] || [[User:Ric|Ric]]<br />
|}<br />
<br />
[[Category: UniCenter]]<br />
[[Category: Support]]<br />
[[Category: Installation]]<br />
[[Category: Application]]<br />
[[Category: Development]]</div>Ric