PHP的邮件功能,但我没有收到任何邮件

时间:2019-03-22 09:28:22

标签: php email mailer

我使用mail()函数接收来自正在访问我的网页的客户的邮件,我将此代码用于邮件功能(mail.php)。我从一开始就使用相同的代码,但从一开始就对我有用,但是现在不起作用。

<?php

if(isset($_POST['submit'])){
    $to = "******";
    $message = "
    <html>
        <head>
            <title>HTML email</title>
        </head>
        <body>
            <p>Enquiry</p>
            <table>
                <tr>
                    <td><strong>Name</strong></td><td>:</td><td>".$_POST['name']."</td>                 
                </tr>
                <tr>
                    <td><strong>Email ID</strong></td><td>:</td><td>".$_POST['email']."</td>                    
                </tr>
                <tr>
                    <td><strong>Mobile</strong></td><td>:</td><td>".$_POST['mobileno']."</td>                   
                </tr>
                <tr>
                    <td><strong>Message</strong></td><td>:</td><td>".$_POST['msg']."</td>                   
                </tr>
            </table>
        </body>
    </html>
    ";

    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    // More headers
    $headers .= 'From: <****>' . "\r\n";
    $headers .= 'Cc: *****' . "\r\n";

    if(mail($to,$subject,$message,$headers)) {
       echo '<script>
                alert("Email Sent Successfully!");
                window.location.href="../contact-us/contactus.html";
            </script>';
    } else {
        echo '<script>
                alert("Sorry your mail was not send kindly try again later.");
                window.location.href="../contact-us/contactus.html";
            </script>'; 
    }
}
?>

这是我的联系表代码。

<form method="post" action="../mail/mail.php">
     <p class="comment-form-author">
       <label>Name<span>(required)</span></label>
        <span class="icon-input">
        <input type="text" name="name" required />
        </span> </p>
        <p class="comment-form-email">
        <label>Email<span>(required)</span></label>
        <span class="icon-input">
        <input type="email" name="email" required />
        </span> </p>

        <p class="comment-form-mobileno">
        <label>Mobile No.<span>(required)</span></label>
        <span class="icon-input">
        <input type="text" name="mobileno" required />
        </span> </p>

        <p class="comment-form-comment">
        <label>Message<span>(required)</span></label>
        <textarea name="msg">
        </textarea>
        </p>
        <p class="form-submit">
        <input type="submit" value="submit" name="submit">
        </p>
    </form>

当我单击“提交”时,它向我显示 “很抱歉,您的邮件发送失败,请稍后再试。”

1 个答案:

答案 0 :(得分:0)

我建议您同样使用PHPMailer。为此,您需要下载PHPMailer库。它在github中可用。将lib放在您的Web服务器文件夹中。在if(isset($ _ POST ['submit'])){}内进行写入并根据需要进行更改

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;// authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";//You can also use other smtp such as outloook<br> (oulook.office365.come)
$mail->Port = 465; // or 587 and 25 for outlook
$mail->IsHTML(true);
$mail->Username = "email@gmail.com";//Your Email Address
$mail->Password = "password";//Your Password
$mail->SetFrom("example@gmail.com");//Again Your Email email address
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddCC("CC@gmail.com");//if any cc (Set in if condifition if no cc)
$mail->AddAddress("email@gmail.com");//Email Address of the person you want to send to

if(!$mail->Send()) {
echo error here
} else {
echo "Message has been sent";
}