Отправка электронной почты средствами PHP

Пример демонстрирует отправку почты с помощью стандартной функции mail, полученный данные от пользователя обрабатываются.функция отправки

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
function send_mail($from, $to, $subject, $text, $headers="")
{
    if (strtolower(substr(PHP_OS, 0, 3)) === 'win')
        $mail_sep = "\r\n";
    else
        $mail_sep = "\n";
  
    function _rsc($s)
    {
        $s = str_replace("\n", '', $s);
        $s = str_replace("\r", '', $s);
        return $s;
    }
  
    $h = '';
    if (is_array($headers))
    {
        foreach($headers as $k=>$v)
            $h = _rsc($k).': '._rsc($v).$mail_sep;
        if ($h != '') {
            $h = substr($h, 0, strlen($h) - strlen($mail_sep));
            $h = $mail_sep.$h;
        }
    }
  
    $from = _rsc($from);
    $to = _rsc($to);
    $subject = _rsc($subject);
    mail($to, $subject, $text, 'From: '.$from.$h);
}
?>
страница отправки:
code: #html
<?php $site_admin = 'your@email.adress';
  
// function ae_send_mail (see code above) is pasted here
  
if (($_SERVER['REQUEST_METHOD'] == 'POST') &&
   isset($_POST['subject']) && isset($_POST['text']) &&
   isset($_POST['from1']) && isset($_POST['from2']))
   {
       $from = $_POST['from1'].' <'.$_POST['from2'].'>';
        // nice RFC 2822 From field
  
        ae_send_mail($from, $site_admin, $_POST['subject'], $_POST['text'],
        array('X-Mailer'=>'PHP script at '.$_SERVER['HTTP_HOST']));
        $mail_send = true;
    }
?>
<html><head><title>Send us mail</title>
</head><body>
<?php
if (isset($mail_send)) {
   echo '<h1>Form has been sent, thank you</h1>';
}
else {
?>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
Your Name: <input type="text" name="from1" size="30" /><br />
Your Email: <input type="text" name="from2" size="30" /><br />
Subject: <input type="text" name="subject" size="30" /><br />
Text: <br />
<textarea rows="5" cols="40" name="text"></textarea>
<input type="submit" value="send" />
</form>
<?php } ?>
</body></html>