mail()在php中不起作用

时间:2016-06-27 10:29:06

标签: php email e-commerce backend

我正在使用mail()函数在事件发生时发送邮件。但它没有像我预期的那样工作。我试图获得函数的返回。有人请问可能是什么问题。

        $msg = "Your password has been changed.is'".$data['password']."'";
        $to = $data['email'];
        $subject = "password changed";
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: info@hia.com'; 
        $send = mail($to, $subject, $msg, $headers);
        if($send){
            echo "successful";
        }
        else{
            echo "error";
        }

2 个答案:

答案 0 :(得分:0)

不要使用简单的mail(),我更喜欢PHPmailer功能。 这是一个例子。   首先 - 在项目目录中下载phpmailer。   第二 - 在你的目录中创建send_mail.php(你可以改变你的名字)。

  • send_mail.php文件代码。

    require_once "PHPMailer/PHPMailerAutoload.php";
    
    $mail = new PHPMailer(true);
    if (!isset($_POST['send'])) {
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the enquiry form!";
    die;
    }
        $to="abc@xyz.com";
        $senderName = "xyz";
        //send the mail
        $fname = $_POST["fname"];
        $lname = $_POST["lname"];
        $zcode = $_POST["zcode"];
        $email = $_POST["email"];
        $phone = $_POST["phone"];
        //Enable SMTP debugging.
        $mail->SMTPDebug = 0;
        //Set PHPMailer to use SMTP.
        $mail->isSMTP();
        //Set SMTP host name
        $mail->Host = "smtp.gmail.com";
        //Set this to true if SMTP host requires authentication to send email
        $mail->SMTPAuth = true;
        //Provide username and password
        $mail->Username = "abc@xyz.com";
        $mail->Password = "########";
        //If SMTP requires TLS encryption then set it
        $mail->SMTPSecure = "ssl";
        //Set TCP port to connect to
        $mail->Port = 465;
        $mail->From = $to;
        $mail->FromName = $senderName;
        $mail->addAddress($to, $senderName);
        $mail->addReplyTo($email, $name);
        $mail->isHTML(true);
        $mail->Subject = "bfuiebfiaif";
        $mail->Body = "as per your needs";
    if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
    die;
    }
    header('Location: index.php');
    die;
    

答案 1 :(得分:0)

php设置引起的问题。关闭某些服务提供商的安全功能。您必须使用SMTP发送邮件。

github上的几个SMTP类示例; hujuice/smtp - snipworks/php-smtp - PHPMailer/PHPMailer

示例代码根据您的代码编写PHPMailer;

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'info@hia.com';                 // SMTP username
$mail->Password = 'yourmailpassword';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('info@hia.com', 'Mailer');
$mail->addAddress($data['email']);               // Name is optional
$mail->addReplyTo('info@hia.com', 'Information');
$mail->addCC('cc@hia.com');
$mail->addBCC('bcc@hia.com');
$mail->CharSet = 'ISO-8859-1';
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'password changed';
$mail->Body    = 'Your password has been changed.is "'.$data['password'].'"';

if(!$mail->send()) {
    echo 'error';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'successful';
}