PHP邮件功能无法完成电子邮件的发送

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: yoursite.com'; 
    $to = 'contact@yoursite.com'; 
    $subject = 'Customer Inquiry';
    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
        if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        }
    }?>

我试过创建一个简单的邮件表单。表单本身在我的index.html页面上,但提交单独的“谢谢你的提交”页面thankyou.php,其中嵌入了上面的PHP代码。代码提交完美,但从不发送电子邮件。请帮忙。


www说
浏览 4030回答 7
7回答

收到一只叮咚

在邮件功能中添加邮件标题$header&nbsp;=&nbsp;"From:&nbsp;noreply@example.com\r\n";&nbsp;$header.=&nbsp;"MIME-Version:&nbsp;1.0\r\n";&nbsp;$header.=&nbsp;"Content-Type:&nbsp;text/html;&nbsp;charset=ISO-8859-1\r\n";&nbsp; $header.=&nbsp;"X-Priority:&nbsp;1\r\n";&nbsp;$status&nbsp;=&nbsp;mail($to,&nbsp;$subject,&nbsp;$message,&nbsp;$header);if($status){&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;'<p>Your&nbsp;mail&nbsp;has&nbsp;been&nbsp;sent!</p>';}&nbsp;else&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;'<p>Something&nbsp;went&nbsp;wrong,&nbsp;Please&nbsp;try&nbsp;again!</p>';&nbsp;}

德玛西亚99

始终尝试在邮件功能中发送标题。如果您通过localhost发送邮件,请执行smtp设置以发送邮件。如果您通过服务器发送邮件,请检查服务器上是否启用了电子邮件发送功能。

有只小跳蛙

通过执行以下操作,它在000webhost上为我工作:$headers&nbsp; = "MIME-Version: 1.0" . "\r\n";$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";$headers .= "From: ". $from. "\r\n";$headers .= "Reply-To: ". $from. "\r\n";$headers .= "X-Mailer: PHP/" . phpversion();$headers .= "X-Priority: 1" . "\r\n";&nbsp;发送电子邮件时直接输入电子邮件地址mail('email@gmail.com', $subject, $message, $headers)使用''与否""此代码有效,但收到的电子邮件滞后半小时

白衣染霜花

大部分mail()功能在共享主机中被禁用。更好的选择是使用SMTP。最好的选择是Gmail或SendGrid。SMTPconfig.php<?php&nbsp;&nbsp; &nbsp; $SmtpServer="smtp.*.*";&nbsp; &nbsp; $SmtpPort="2525"; //default&nbsp; &nbsp; $SmtpUser="***";&nbsp; &nbsp; $SmtpPass="***";?>SMTPmail.php<?phpclass SMTPClient{&nbsp; &nbsp; function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->SmtpServer = $SmtpServer;&nbsp; &nbsp; &nbsp; &nbsp; $this->SmtpUser = base64_encode ($SmtpUser);&nbsp; &nbsp; &nbsp; &nbsp; $this->SmtpPass = base64_encode ($SmtpPass);&nbsp; &nbsp; &nbsp; &nbsp; $this->from = $from;&nbsp; &nbsp; &nbsp; &nbsp; $this->to = $to;&nbsp; &nbsp; &nbsp; &nbsp; $this->subject = $subject;&nbsp; &nbsp; &nbsp; &nbsp; $this->body = $body;&nbsp; &nbsp; &nbsp; &nbsp; if ($SmtpPort == "")&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->PortSMTP = 25;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->PortSMTP = $SmtpPort;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; function SendMail ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $newLine = "\r\n";&nbsp; &nbsp; &nbsp; &nbsp; $headers = "MIME-Version: 1.0" . $newLine;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fputs ($SMTPIN, "EHLO ".$HTTP_HOST."\r\n");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $talk["hello"] = fgets ( $SMTPIN, 1024 );&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fputs($SMTPIN, "auth login\r\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $talk["res"]=fgets($SMTPIN,1024);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fputs($SMTPIN, $this->SmtpUser."\r\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $talk["user"]=fgets($SMTPIN,1024);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fputs($SMTPIN, $this->SmtpPass."\r\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $talk["pass"]=fgets($SMTPIN,256);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fputs ($SMTPIN, "MAIL FROM: <".$this->from.">\r\n");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $talk["From"] = fgets ( $SMTPIN, 1024 );&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fputs ($SMTPIN, "RCPT TO: <".$this->to.">\r\n");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $talk["To"] = fgets ($SMTPIN, 1024);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fputs($SMTPIN, "DATA\r\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $talk["data"]=fgets( $SMTPIN,1024 );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fputs($SMTPIN, "To: <".$this->to.">\r\nFrom: <".$this->from.">\r\n".$headers."\n\nSubject:".$this->subject."\r\n\r\n\r\n".$this->body."\r\n.\r\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $talk["send"]=fgets($SMTPIN,256);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //CLOSE CONNECTION AND EXIT ...&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fputs ($SMTPIN, "QUIT\r\n");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fclose($SMTPIN);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return $talk;&nbsp; &nbsp; }&nbsp;}?>contact_email.php<?php&nbsp;include('SMTPconfig.php');include('SMTPmail.php');if($_SERVER["REQUEST_METHOD"] == "POST"){&nbsp; &nbsp; $to = "";&nbsp; &nbsp; $from = $_POST['email'];&nbsp; &nbsp; $subject = "Enquiry";&nbsp; &nbsp; $body = $_POST['name'].'</br>'.$_POST['companyName'].'</br>'.$_POST['tel'].'</br>'.'<hr />'.$_POST['message'];&nbsp; &nbsp; $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);&nbsp; &nbsp; $SMTPChat = $SMTPMail->SendMail();}?>
打开App,查看更多内容
随时随地看视频慕课网APP