如何测试PHP mail()是否已成功发送邮件

时间:2012-05-14 10:49:20

标签: php

如何测试mail()是否已成功发送邮件?

8 个答案:

答案 0 :(得分:13)

mail()只返回一个布尔值,具体取决于邮件是否已成功接受传递。来自php.net网站:

  

如果邮件成功接受传递,则返回TRUE,FALSE   否则。

     

重要的是要注意,因为邮件被接受了   交付,并不意味着邮件实际上会达到预期的目的   目的地。

所以你可以测试它是否已被“发送”,但检查它是否已经发送是另一个故事。

答案 1 :(得分:8)

根据Ben回复,您可以成功检查电子邮件递送,如下所示

$result = mail('abc@test.com', 'Test Subject', $message);
if(!$result) {   
     echo "Error";   
} else {
    echo "Success";
}

为了获得更好的结果,您可以使用PHPMailer。点击下面的链接,获取PHPMailer的详细文档。

http://phpmailer.worxware.com/index.php?pg=tutorial

if(!$mail->Send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}

答案 2 :(得分:2)

来自文档:

  

“如果邮件成功接受传递,则返回TRUE,   否则就错了。

重要的是要注意,仅仅因为邮件被接受传递,并不意味着邮件实际上将到达预定目的地。 “

答案 3 :(得分:2)

来自http://php.net/mail

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

答案 4 :(得分:1)

尝试一下:

if(@mail($emailRecipient, $subject, $message, $headers))
{
  echo "Mail Sent Successfully";
}else{
  echo "Mail Not Sent";
}

答案 5 :(得分:0)

仅邮件()功能就会通知您,您设置的“电子邮件”是合法的,并将发送给您的电子邮件服务器。

您必须单独检查电子邮件地址是否合法。可以找到一篇好文章here

如果这两种方法对您不起作用,您可以使用图像和服务器日志文件的一些“垃圾邮件”方法。

答案 6 :(得分:-1)

if(isset($ _ POST [“ btn_emp”])){

//$hid_emp = ($_POST['hid_emp']);
$employee_name = ($_POST['employee_name']);
$department_id = ($_POST['department_id']);
$serial_number = ($_POST['serial_number']);
$employee_address = ($_POST['employee_address']);
$employee_contact = ($_POST['employee_contact']);
$employee_email = ($_POST['employee_email']);





$insert = "INSERT INTO tbl_employee(department_id,serial_number,employee_name,employee_address,employee_contact,employee_email)VALUES('$department_id', '$serial_number', '$employee_name','$employee_address' ,'$employee_contact', '$employee_email')";
//echo $insert;
//die();
if ($conn->query($insert) === TRUE) {
    //CODE FOR SEND MAIL
    $Mail_Admin_Message = '';
            $Mail_Admin_Message .= '
                <table width="700px" border="0" cellpadding="0" cellspacing="0">

                    <tr height="15px"><td colspan="3"></td></tr>
                    <tr>
                        <td colspan="3" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;">
                            Date</b> :<b> '. $serial_number .'</b>
                        </td>
                    </tr>
                    <tr height="15px"><td colspan="3"></td></tr>
                    <tr>
                        <td colspan="3" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;">
                            Hi</b> <b> '. $employee_name .'</b>
                        </td>
                    </tr>
                    <tr height="15px"><td colspan="3"></td></tr>
                    <tr>
                        <td colspan="3" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;">
                          Your Gift Voucher Code is</b> :<b> '. $department_id .'</b>
                        </td>
                    </tr>
                    <tr height="15px"><td colspan="3"></td></tr>
                    <tr>
                        <td colspan="3" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;">
                            Gift Amount</b> :<b> '. $employee_address .'</b>
                        </td>
                    </tr>
                    <tr height="15px"><td colspan="3"></td></tr>
                    <tr height="15px"><td colspan="3"></td></tr>
                    <tr>
                        <td colspan="3" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold;">Thanks, <br />DairyKart Team</td>
                    </tr>
                </table>';

            $Mail_To_Admin_ID   = $employee_email;
            $Mail_Admin_Subject = "Employee Details";
            $Mail_Admin_Header  = "MIME-Version: 1.0\n";
            $Mail_Admin_Header .= "Content-type: text/html; charset=iso-8859-1\r\n";
            $Mail_Admin_Header .= "Content-Transfer-Encoding: 8bit\n";
            $Mail_Admin_Header .= "X-Priority: 1\n";
            $Mail_Admin_Header .= "From: Employee-Department Project\r\n";
            $Mail_Admin_Header .= "X-MSMail-Priority: High\n";
            mail($Mail_To_Admin_ID, $Mail_Admin_Subject, $Mail_Admin_Message, $Mail_Admin_Header);
            //echo $serial_number;
            //echo $employee_email;
            //die();
    echo "<script>alert('Successfully Added & Check Your Mail to know your Details.!!!'); window.location='add-employee.php'</script>";

答案 7 :(得分:-8)

您可以在PHP中使用$_SERVER['REMOTE_ADDR']来接收用户的远程IP地址。

像这样使用:

<?php 

    $ip = $_SERVER['REMOTE_ADDR'];
    echo "User's IP address is: ".$ip;

 ?>