如何测试 PHP mail() 是否成功投递邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10581896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:30:53  来源:igfitidea点击:

How to test if PHP mail() has successfully delivered mail

php

提问by Sunny

How can I test if mail()has successfully delivered mail?

如何测试mail()邮件是否成功投递?

回答by Ben Everard

Well mail()simply returns a boolean value depending on whether the mail was successfully accepted for delivery. From the php.netsite:

好吧,mail()简单地返回一个布尔值,具体取决于邮件是否已成功接受交付。从php.net站点:

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

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

如果邮件被成功接受交付,则返回 TRUE,否则返回 FALSE。

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

So you can test to see if it's been "sent", however checking if it's been delivered is another story.

因此,您可以测试它是否已“发送”,但是检查它是否已交付是另一回事。

回答by Sanjeev Chauhan

As per Ben reply you can check successfully email delivery as below

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

$result = mail('[email protected]', 'Test Subject', $message);
if(!$result) {   
     echo "Error";   
} else {
    echo "Success";
}

For better result you can use PHPMailer. Click on below link for detailed documentation of PHPMailer.

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

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

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.';
}

回答by triclosan

from the docs:

从文档:

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

“如果邮件被成功接受交付,则返回 TRUE,否则返回 FALSE。

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination. "

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

回答by Aaron W.

From http://php.net/mail

来自http://php.net/mail

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

回答by Faisal

Try this:

尝试这个:

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

回答by andrew

The mail() function alone just notifies you that the "email" you have set up is legit and will be send my your email server.

单独的 mail() 函数只是通知您您设置的“电子邮件”是合法的,并将发送给我的电子邮件服务器。

You must check separately for the email address to be legit. A good article can be found here

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

If these two methods do not work for you well, you can use some "spam" approach using images and your server's log files.

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

回答by user10988803

if (isset($_POST["btn_emp"])) {

如果(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>";

回答by iJamesPHP

You can use $_SERVER['REMOTE_ADDR']in PHP to receive the user's remote IP address.

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

Use like so:

像这样使用:

<?php 

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

 ?>