PHP function example for sending email

WBOY
Release: 2016-07-25 08:56:39
Original
1186 people have browsed it
This article introduces two examples of sending emails in PHP, using the mail function to send emails. Friends in need can refer to them.

In PHP, there are many unsafe factors when using the built-in mail() function to send emails. We have implemented the following ae_send_mail function, which is suitable for PHP4.0.2 or higher. Only four parameters are used when calling ae_send_mail: sender, recipient, subject, and email content.

This function can remove the impact of all unnecessary special characters on emails by adding some filtering mechanisms.

Code:

<?php
/**
* Email邮件发送
* by bbs.it-home.org
*/
function ae_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);
}
?>
Copy after login

The above function has required parameters: $from, $to, $subject, $text. There is also an optional parameter $headers, which is used to transmit some email header information. It can accept the form of an array, for example: ("Header information 1" => "value", "Header information 2" => "value").

The following are examples on specific pages, you can refer to them.

Code:

<?php 
/**
* as_send_mail函数示例
* 接收联系人信息
* by bbs.it-home.org
*/

$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>发送邮件的例子</title>
</head>
<body>
<?php
if (isset($mail_send)) {
    echo '<h1>邮件已发送!谢谢!</h1>';
}
else {
?>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
姓名: <input type="text" name="from1" size="30" /><br />
邮箱: <input type="text" name="from2" size="30" /><br />
主题: <input type="text" name="subject" size="30" /><br />
内容: <br />
<textarea rows="5" cols="40" name="text"></textarea>
<input type="submit" value="send" />
</form>
<?php } ?>
</body>
</html>
Copy after login

Note that since the ae_send_mail function uses PHP's built-in mail() function to send emails, problems related to this function will also exist in ae_send_mail. When you encounter problems when using it, please refer to the usage and related explanations of the mail function.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template