随机密码生成和发送电子邮件


Random password generation and sending a email

我想在表格中输入电子邮件和公司名称,然后必须向给定的电子邮件发送随机生成的密码。

以下是我用于生成随机密码和发送电子邮件的代码。

<?php
$company_name = "";
$email = "";
if (isset($_POST['submit'])) {
    $company_name = $_POST["company_name"];
    $email = $_POST["email_address"];
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*_";
    $password = substr(str_shuffle($chars), 0, 8);
    $to = $email;
    $subject = 'your registration is completed';
    $message = 'Welcome' . $company_name . ''
            . 'Your email and password is following :'
            . 'Email:' . $email . ''
            . 'Your new password : ' . $password . ''
            . 'Now you can login with this email and password';
    $headers = 'From :' . $email . "'r'n";
    mail($to, $subject, $message, $headers);
}
?>

但这显示了以下错误。

Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in "path"'password_generation.php on line 21

第 21 行mail($to, $subject, $message, $headers);

试试这个。

 <?php
    $company_name = "";
    $email = "";
    if (isset($_POST['submit'])) {
        $company_name = $_POST["company_name"];
        $email = $_POST["email_address"];
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*_";
        $password = substr(str_shuffle($chars), 0, 8);
        $to = $email;
        $subject = 'your registration is completed';
        $message = 'Welcome' . $company_name . ''
                . 'Your email and password is following :'
                . 'Email:' . $email . ''
                . 'Your new password : ' . $password . ''
                . 'Now you can login with this email and password';
        $headers = 'From: Your name <'.$email .'>' . "'r'n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n"; 
        mail($to, $subject, $message, $headers);
    }
    ?>