【问题标题】:php email sending not working [duplicate]php电子邮件发送不起作用[重复]
【发布时间】:2015-11-26 05:32:47
【问题描述】:

这是我发送电子邮件的 php 代码。我收到了“消息已发送,您可以发送另一个消息”的消息,但未发送电子邮件。

     <h4>Please fill out the following form and we will be in touch with you soon.</h4>
<form action="mytest.php" method="post" id="contactform">
  <ol>
    <li>
      <label for="name">Your Name <span class="red">*</span></label>
      <input id="name" name="name" class="text" />
    </li>
    <li>
      <label for="email">Your email <span class="red">*</span></label>
      <input id="email" name="email" class="text" />
    </li>
    <li>
      <label for="subject">Subject</label>
      <input id="subject" name="subject" class="text" />
    </li>
    <li>
      <label for="message">Message <span class="red">*</span></label>
      <textarea id="message" name="message" rows="6" cols="50"></textarea>
    </li>
    <li class="buttons">
      <input type="image" name="imageField" id="imageField" src="images/send.gif" class="send" />
      <div class="clr"></div>
    </li>
  </ol>
</form> 


<?php

if(!$_POST) exit;

$email = $_POST['email'];

$errors=0;

if($errors==1) echo $error;
else{
    $email_from = $_POST['email'];
$email_from = "from@gmail.com";
$headers = "From: " . strip_tags( $_POST['name'] ) . "\r\n";

$mail_to_send_to = "to@gmail.com";
$your_feedbackmail = "from@gmail.com";
$sendflag = 'send';                       
if ( $sendflag == "send" )
        {
                $email = $_REQUEST['email'] ;
                $message = $_REQUEST['message'] ;
                $headers = "From: $your_feedbackmail" . "\r\n" . "Reply-To: $email" . "\r\n" ;
                $a = mail( $mail_to_send_to, "Feedback Form Results", $message, $headers );
                if ($a)
                {
                     print("Message was sent, you can send another one");
                } else {
                     print("Message wasn't sent, please check that you have changed emails in the bottom");
                }
        }
}
?>

我正在使用 Cpanel 来托管我的网站。有什么特殊的配置可以做到这一点吗?我是 php 新手。请帮帮我。

【问题讨论】:

  • 在邮件功能中请记住,即使邮件被接受投递,并不代表邮件真的被发送和接收了!它会将地址返回到$a,因此您会收到该消息已发送的消息
  • 请检查您的垃圾邮件文件夹一次......接收电子邮件的位置......即使邮件被接受递送,您也会得到 $a 的真实性,正如@AbbasGabru 所说。

标签: php forms email post


【解决方案1】:

邮件功能不提供身份验证功能。您必须使用 Mail Pear 包中的 Mail 类。示例见这里:

example

【讨论】:

    【解决方案2】:

    我假设您使用的是共享主机,因为您使用的是 Cpanel,一些共享主机解决方案不能很好地与 php 的 mail() 函数配合使用,我发现使用 phpmailer https://github.com/PHPMailer/PHPMailer 效果更好并提供更多功能

    使用 phpmailer : 从链接下载,将文件放在您的网络应用程序可访问的文件夹中。

    代码:

    $email_from = $_POST['email'];
    $email_from = "from@gmail.com";  // this overwrites the $_POST['email'] value, check this
    $email_from_name = "Nishanthi";
    $gmailUsername = "from@gmail.com";
    $gmailPassword = "mysecretpassword";
    $mail_to_send_to = "to@gmail.com";
    $your_feedbackmail = "from@gmail.com";
    
    $emailSubject = "Place Subject Here";
    $emailContent = "This message can contain <b>HTML</b>";
    
    require '[path_to_your_phpmailer_files]/PHPMailerAutoload.php';
    
    $mail = new PHPMailer;
    
    //$mail->SMTPDebug = 3;                               // Enable verbose debug output
    
    $mail->isSMTP();                                      // Set mailer to use SMTP 
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 2;                                 //Enable SMTP debugging 
    $mail->Debugoutput = 'html';                          //Ask for HTML-friendly debug output
    $mail->Host = 'smtp.gmail.com';                       // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = $gmailUsername;                 // SMTP username
    $mail->Password = $gmailPassword;                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to
    
    $mail->setFrom($email_from, $email_from_name);
    $mail->addAddress($mail_to_send_to);       
    $mail->addReplyTo($your_feedbackmail);
    
    $mail->isHTML(true);                                  // Set email format to HTML
    
    $mail->Subject = $emailSubject;
    $mail->Body    = $emailContent; 
    
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message was sent, you can send another one';
    }
    
    ?>
    

    【讨论】:

    • 感谢您的回答。我使用了你的代码。我有 PHPMailer、PHPMailerOAuth 和 PHPMailerOAuthGoogle 文件。现在我收到此错误致命错误:第 27 行的 C:\wamp\www\mytest\PHPMailerOAuth.php 中找不到类 'PHPMailer'
    • 你能把php代码和文件夹结构贴出来,以便我更好地理解是什么导致了错误
    • 这里的文件夹结构 index.php contact.php PHPMailer.php PHPMailerOAuth.php PHPMailerOAuthGoogle.php
    • 我没有看到一个名为:PHPMailerAutoload.php 的文件?
    • 感谢您回复我。我现在才添加。但同样的错误显示
    猜你喜欢
    • 2015-02-23
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多