PHP mail function: Difference between revisions
Jump to navigation
Jump to search
m
Reverted edits by Upazixorys (Talk); changed back to last version by BobS
Upazixorys (talk | contribs) 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="padding:0;margin:0; border-bottom:3px inset #000000"> | ||
</ | |||
{| | {| | ||
| [[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]] | ||
|} | |} | ||
</div> | |||
{| cellpadding= | {| cellpadding="2" | ||
| | | | ||
__TOC__ | __TOC__ | ||
Line 34: | Line 26: | ||
{| | {| | ||
|-style= | |-style="background:#e4e4e4" | ||
!mail.html | !mail.html | ||
!mail.php | !mail.php | ||
|-style= | |-style="background:#f0f0f0" | ||
|valign= | |valign="top"| | ||
<pre style="border:none;background:#f0f0f0"> | |||
<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> | |||
</pre> | |||
| | | | ||
<pre style="border:none;background:#f0f0f0"> | |||
<html> | |||
<head><title>PHP Mail Sender</title></head> | |||
<body> | |||
<?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 | echo "<h4>Thank you for sending email</h4>"; | ||
} else { | } else { | ||
echo | echo "<h4>Can't send email to $email</h4>"; | ||
} | } | ||
? | ?> | ||
</body> | |||
</html> | |||
</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= | |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= | |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: | ||
<table cellpadding="2"> | |||
<tr> | |||
<td> | |||
{| cellpadding= | {| cellpadding="2" cellspacing="1" style="background:#000000;" | ||
|- style= | |- style="background:#e8e8e8;" | ||
!mail.php | !mail.php | ||
|- style= | |- style="background:#f5f5f5;" | ||
| | | | ||
<div style="padding:4px;"> | |||
<html><br> | |||
<head><title>PHP Mail Sender</title></head><br> | |||
<body><br> | |||
<?php | |||
<nowiki>#</nowiki> Retrieve the form data<br> | |||
$email = $_POST['email']; | $email = $_POST['email'];<br> | ||
$subject = $_POST['subject']; | $subject = $_POST['subject'];<br> | ||
$message = $_POST['message']; | $message = $_POST['message']; | ||
<nowiki>#</nowiki> Check if a $_POST value exists<br> | |||
<nowiki>#</nowiki> If not use defaults<br> | |||
<nowiki>if (empty($_POST['email'])){</nowiki><br> | |||
& | $email = "'''xxx@yyy.com'''";<br> | ||
} | }<br> | ||
if (empty($_POST['subject'])){ | if (empty($_POST['subject'])){<br> | ||
& | $subject = "UniCenter test";<br> | ||
<nowiki>}</nowiki><br> | |||
if (empty($_POST['message'])){ | if (empty($_POST['message'])){<br> | ||
& | $message = "A quick test from UniCenter";<br> | ||
<nowiki>}</nowiki> | |||
<nowiki>#</nowiki> Sends mail and report success or failure<br> | |||
<nowiki>if (mail($email,$subject,$message)) {</nowiki><br> | |||
& | echo "<nowiki><h4>Thank you for sending email</h4></nowiki>";<br> | ||
<nowiki>}</nowiki> else {<br> | |||
& | echo "<nowiki><h4></nowiki>Can't send email to $email<nowiki></h4></nowiki>";<br> | ||
<nowiki>}</nowiki><br> | |||
? | ?><br> | ||
</body><br> | |||
</html> | |||
</div> | |||
|} | |} | ||
</td> | |||
<td> | |||
& | | ||
</td> | |||
<td> | |||
{| cellpadding= | {| cellpadding="2" cellspacing="1" style="background:#000000;" | ||
|- style= | |- style="background:#e8e8e8;" | ||
! Comments | ! Comments | ||
|- style= | |- 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'])){ | if (empty($_POST['email'])){<br> | ||
$email = | $email = "'''fred123@xip.com'''";<br> | ||
} | } | ||
Change default '''subject''' and '''message''' if you wish. | Change default '''subject''' and '''message''' if you wish. | ||
|} | |} | ||
</td> | |||
</tr> | |||
</table> | |||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
== Running the test Scripts == | == Running the test Scripts == | ||
Start UniServer and type the following into your browser address bar: ''' | 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= | {|cellpadding="8" | ||
|-style= | |-style="background:#e4e4e4" | ||
!error message | !error message | ||
|-style= | |-style="background:#f0f0f0" | ||
| | | | ||
Warning: mail() [function.mail]: Failed to connect to mailserver at | 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= | {|cellpadding="8" | ||
|-style= | |-style="background:#e4e4e4" | ||
!php.ini section [mail function] | !php.ini section [mail function] | ||
!Comments | !Comments | ||
|--style= | |--style="background:#f0f0f0" | ||
| | | | ||
[mail function] | [mail function]<br> | ||
<nowiki>;</nowiki> For Win32 only.<br> | |||
SMTP = '''localhost''' | SMTP = '''localhost'''<br> | ||
smtp_port = '''25''' | smtp_port = '''25''' | ||
<nowiki>;</nowiki> For Win32 only.<br> | |||
sendmail_from = me@localhost.com | sendmail_from = me@localhost.com | ||
<nowiki>;</nowiki>For Unix only. You may supply arguments as well (default: ;"sendmail -t -i").<br> | |||
<nowiki>;</nowiki>sendmail_path = "/usr/bin/sendmail.exe -t" | |||
<nowiki>;</nowiki> Force the addition of the specified parameters to be passed as extra parameters<br> | |||
<nowiki>;</nowiki> to the sendmail binary. These parameters will always replace the value of<br> | |||
<nowiki>;</nowiki> the 5th parameter to mail(), even in safe mode.<br> | |||
<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 | 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= | {|cellpadding="8" | ||
|-style= | |-style="background:#e4e4e4" | ||
!php.ini section [mail function] | !php.ini section [mail function] | ||
!Comments | !Comments | ||
|--style= | |--style="background:#f0f0f0" | ||
| | | | ||
[mail function] | [mail function]<br> | ||
<nowiki>;</nowiki> For Win32 only.<br> | |||
SMTP = '''smtp.xip.com''' | SMTP = '''smtp.xip.com'''<br> | ||
smtp_port = '''25''' | smtp_port = '''25''' | ||
<nowiki>;</nowiki> For Win32 only.<br> | |||
sendmail_from = '''me@localhost.com''' | sendmail_from = '''me@localhost.com''' | ||
<nowiki>;</nowiki>For Unix only. You may supply arguments as well (default: ;"sendmail -t -i").<br> | |||
<nowiki>;</nowiki>sendmail_path = "/usr/bin/sendmail.exe -t" | |||
<nowiki>;</nowiki> Force the addition of the specified parameters to be passed as extra parameters<br> | |||
<nowiki>;</nowiki> to the sendmail binary. These parameters will always replace the value of<br> | |||
<nowiki>;</nowiki> the 5th parameter to mail(), even in safe mode.<br> | |||
<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: ''' | 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 ''' | 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. | ||