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

时间:2016-04-27 09:54:19

标签: php 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);

1 个答案:

答案 0 :(得分:0)

试试这个..

 <?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);
    }
    ?>