
In this article I want to talk about
sending mail from php scripts under Windows.
Of course, I will not reveal America, but I hope that this article will be useful to someone or will simply save time.
')
From the point of view of a php programmer, mail is sent using the standard
mail()
function. And here many beginner developers have a
problem . The script, which works fine on the host server, gives errors on the local computer.
Usually these errors have a description like this:
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 E:\www\simplemail\mailer.php on line ......
The fact is that the mail function itself
does not send mail, it simply calls the
sendmail program, which is
not (and should not be)
included in the web server distribution kit and the interpreter php.
Sendmail, in turn, uses an
SMTP server to send mail.
So, in order for the php script to send mail, you need to
install and configure sendmail and SMTP server .
The sendmail version for Windows can be downloaded
here .
Installation and configuration is carried out in three stages.
1)
Unpack the archive on the same disk where php is installed. For example, I created the folder C: \ wamp \ sendmail.
2)
We make changes to the php.ini file:
[mail function]
SMTP =
sendmail_from =
sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"
As you can see, you only need to specify the path to sendmail so that php can find it.
3)
Configure sendmail . All settings are in the sendmail.ini file (located in the sendmail folder).
But before setting up a few words about the
SMTP server . You do not need to install a server on your computer. Many mail services provide free access to their servers.
Below I will show an example of setting up sendmail to work with the mail.ru SMTP server, but, of course, you can choose any other.
So, open sendmail.ini and set the following parameters:
smtp_server=smtp.mail.ru
; SMTP server address
smtp_port=25
; SMTP server port
default_domain=mail.ru
; default domain
error_logfile=error.log
; error log file
debug_logfile=debug.log
; very useful option at the debugging stage. Logs all operations that sendmail performs.
auth_username=account_name@mail.ru
; your account name
auth_password=account_password
; your password
; The following three options are used if authorization is required on the POP3 server before authorization on the SMTP server
pop3_server=pop.mail.ru
pop3_username=account_name@mail.ru
pop3_password=account_password
; parameter for the MAIL FROM command
force_sender=account_name@mail.ru
hostname=mail.ru
Now do not forget to
restart the web server for the changes to take effect.
To test the operation of the mail, we will write a simple script:
01 <html xmlns = "http://www.w3.org/1999/xhtml">
02 <head>
03 <meta http-equiv = "Content-Type" content = "text / html; charset = utf-8" />
04 <title> Simple Mail </ title>
05 </ head>
06 <body>
07 <? Php
08 $ addr = $ _POST ['addr'];
09 $ theme = $ _POST ['theme'];
10 $ text = $ _POST ['text'];
11 if (isset ($ addr) && isset ($ theme) && isset ($ text)
12 && $ addr! = "" && $ theme! = "" && $ text! = "") {
13 if (mail ($ addr, $ theme, $ text, "From: vova_33@mail.ru")) {
14 echo "<h3> Message sent </ h3>";
15 }
16 else {
17 echo "<h3> An error occurred while sending the message </ h3>";
18 }
nineteen }
20?>
21 <form action = "mailer.php" method = "post">
22 <p>
23 <label for = "addr"> eMail: </ label>
24 <input type = "text" name = "addr" id = "addr" size = "30" />
25 </ p>
26 <p>
27 <label for = "theme"> Letter subject: </ label>
28 <input type = "text" name = "theme" id = "theme" size = "30" />
29 </ p>
30 <p>
31 <label for = "text"> Letter text: </ label>
32 <textarea rows = "10" cols = "20" name = "text" id = "text"> </ textarea>
33 </ p>
34 <p>
35 <input type = "submit" value = "Send" />
36 </ p>
37 </ form>
38 </ body>
39 </ html>
He creates a form with three fields for entering the
address ,
subject and
content of the letter. Clicking on the “Send” button will send a request to the same script (line 21).
If the data is entered, the mail function will be called (line 13), which will send the letter. If successful, the function returns true; otherwise, it returns false.
As you can see, there is nothing difficult in the mail tincture.
Good luck!
Source: Crossposting from my blog -
www.simplecoding.org .