PHP邮件function没有完成电子邮件的发送

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

我试过创build一个简单的邮件表单。 表单本身位于我的index.html页面,但是提交给单独的“谢谢你的提交”页面, thankyou.php ,其中embedded了上面的php代码。 代码提交完美,但从来没有发送电子邮件。 请帮忙。

虽然这个答案的一部分仅适用于mail()函数本身的使用,但这些疑难解答步骤中的很多都可以应用于任何PHP邮件系统。

您的脚本似乎有多种原因不能发送电子邮件。 除非存在明显的语法错误,否则很难诊断这些东西。 没有人,你需要通过下面的检查清单来找出你可能遇到的任何潜在的陷阱。

确保错误报告已启用并设置为报告所有错误

错误报告对于消除代码中的错误以及PHP遇到的一般错误至关重要。 需要启用错误报告来接收这些错误。 将以下代码放在PHP文件(或主configuration文件)的顶部将启用错误报告。

 error_reporting(-1); ini_set('display_errors', 'On'); set_error_handler("var_dump"); 

看到这堆栈溢出答案更多细节。

确保mail()函数被调用

这看起来可能很愚蠢,但是一个常见的错误是忘记在你的代码中实际放置mail()函数。 确保它在那里,没有被注释掉。

确保mail()函数调用正确

bool mail(string $ to,string $ subject,string $ message [,string $ additional_headers [,string $ additional_parameters]])

邮件function需要三个必要的参数,并可select第四和第五个参数。 如果您对mail()调用没有至less三个参数,则会失败。

如果您对mail()调用没有正确的顺序,那么它也会失败。

检查服务器的邮件日志

您的Web服务器应logging所有尝试通过它发送电子邮件。 这些日志的位置会有所不同(您可能需要询问您的服务器pipe理员所在的位置),但通常可以在logs下的用户根目录中find它们。 里面将是服务器报告的错误消息,如果有的话,与您尝试发送电子邮件有关。

检查端口连接失败

端口阻止是大多数开发人员在集成代码以使用SMTP传递电子邮件时所面临的一个非常普遍的问题。 而且,这可以在服务器maillogs上很容易地find(邮件日志服务器的位置可以根据服务器的不同而不同,如上所述)。 如果您在共享托pipe服务器上,默认情况下端口25和587保持阻塞状态。 这个块是由你的托pipe服务提供商特意完成的。 即使对于一些专用服务器也是如此。 当这些端口被阻止时,尝试使用端口2525进行连接。如果您发现该端口也被阻止,则唯一的解决方法是与您的托pipe服务提供商联系以解除对这些端口的阻止。

大多数托pipe提供商阻止这些电子邮件端口,以保护他们的networking发送任何垃圾邮件。

plain / TLS连接使用端口25或587,SSL连接使用端口465。 对于大多数用户,build议使用端口587来避免某些托pipe服务提供商设置的速率限制。

不要使用错误抑制操作符

当错误抑制运算符 @被预置为PHP中的expression式时,可能会由该expression式生成的任何错误消息都将被忽略。 有必要使用这个操作符的情况下,但发送邮件不是其中之一。

如果您的代码包含@mail(...)那么您可能会隐藏重要的错误消息,这将有助于您进行debugging。 删除@并查看是否有任何错误报告。

只有在事后检查error_get_last()后才能find具体的错误。

检查mail()返回值

mail()函数:

如果邮件已成功接收,则返回TRUE ,否则返回FALSE 。 重要的是要注意,仅仅因为邮件被接受交付,并不意味着邮件实际上会到达预期的目的地。

这一点很重要,因为:

  • 如果你收到一个FALSE返回值,你知道错误在于你的服务器接受你的邮件。 这可能不是一个编码问题,而是一个服务器configuration问题。 您需要与您的系统pipe理员联系,了解为何发生这种情况。
  • 如果您收到一个TRUE返回值,这并不意味着您的电子邮件肯定会被发送。 这只是意味着电子邮件被PHP成功发送到服务器上相应的处理程序。 除了PHP的控制之外,还有更多的失败点可能导致邮件不能被发送。

所以FALSE会帮助你指出正确的方向,而TRUE并不一定意味着你的电子邮件发送成功。 这一点很重要!

确保您的托pipe服务提供商允许您发送电子邮件并且不限制邮件发送

许多共享的虚拟主机,尤其是免费的虚拟主机提供商,不允许从服务器发送电子邮件或限制在任何给定时间段内可以发送的数量。 这是由于他们努力限制垃圾邮件发送者利用便宜的服务。

如果您认为您的主机已经通过电子邮件发送限制或阻止发送电子邮件,请查看他们的常见问题解答,看看是否列出了这些限制。 否则,您可能需要联系他们的支持人员,以确认在发送电子邮件时是否有任何限制。

检查垃圾文件夹; 防止电子邮件被标记为垃圾邮件

通常,由于各种原因,通过PHP(以及其他服务器端编程语言)发送的电子邮件最终会收到收件人的垃圾邮件文件夹中。 在对代码进行故障诊断之前,请经常检查。

为了避免通过PHP发送的邮件被发送到收件人的垃圾邮件文件夹中,您可以在PHP代码中执行各种操作,否则可能会将您的电子邮件标记为垃圾邮件的可能性降至最低。 Michiel de Mare的好build议包括:

  • 使用电子邮件身份validation方法(如SPF和DKIM)来certificate您的电子邮件和您的域名属于一起,并防止欺骗您的域名。 SPF网站包含一个向导,可以为您的网站生成DNS信息。
  • 检查您的反向DNS ,确保您的邮件服务器的IP地址指向用于发送邮件的域名。
  • 确保您使用的IP地址不在黑名单中
  • 确保回复地址是有效的现有地址。
  • 在“收件人”字段中使用收件人的完整名称,而不仅仅是电子邮件地址(例如"John Smith" <john@blacksmiths-international.com> )。
  • 监控您的滥用帐户,例如abuse@yourdomain.com和postmaster@yourdomain.com。 这意味着 – 确保这些帐户存在,阅读发送给他们的信息,并处理投诉。
  • 最后,取消订阅真的很容易。 否则,您的用户将通过按垃圾邮件button取消订阅,这将影响您的声誉。

请参阅如何确保以编程方式发送的电子邮件不会自动标记为垃圾邮件? 有关这个话题的更多信息。

确保提供了所有邮件标题

一些垃圾邮件软件会拒绝邮件,如果它缺less常见的标题,如“From”和“Reply-to”:

 $headers = array("From: from@example.com", "Reply-To: replyto@example.com", "X-Mailer: PHP/" . PHP_VERSION ); $headers = implode("\r\n", $headers); mail($to, $subject, $message, $headers); 

确保邮件标题没有语法错误

无效的标题与没有标题一样糟糕。 一个不正确的字符可能会导致您的电子邮件脱轨。 仔细检查以确保您的语法正确,因为PHP 不会为您捕获这些错误。

 $headers = array("From from@example.com", // missing colon "Reply To: replyto@example.com", // missing hyphen "X-Mailer: "PHP"/" . PHP_VERSION // bad quotes ); 

确保收件人的值是正确的

有时问题就像电子邮件收件人的值不正确一样简单。 这可能是由于使用了一个不正确的variables。

 $to = 'user@example.com'; // other variables .... mail($recipient, $subject, $message, $headers); // $recipient should be $to 

另一种testing方法是将收件人的值硬编码到mail()函数调用中:

 mail('user@example.com', $subject, $message, $headers); 

这可以应用于所有的mail()参数。

发送到多个帐户

为帮助排除电子邮件帐户问题,请将您的电子邮件发送到不同电子邮件供应商的多个电子邮件帐户 如果您的电子邮件未到达用户的Gmail帐户,请将相同的电子邮件发送到Yahoo帐户,Hotmail帐户和常规POP3帐户(如ISP提供的电子邮件帐户)。

如果电子邮件到达全部或部分其他电子邮件帐户,则知道您的代码正在发送电子邮件,但由于某种原因,电子邮件帐户提供商很可能会阻止它们。 如果电子邮件没有到达任何电子邮件帐户,则问题更可能与您的代码有关。

确保代码与表单方法匹配

如果您将表单方法设置为POST ,请确保使用$_POST查找表单值。 如果您已将其设置为GET或根本没有设置,请确保使用$_GET查找表单值。

确保Web主机支持发送电子邮件

一些虚拟主机提供商不允许或允许通过他们的服务器发送电子邮件。 原因可能会有所不同,但是如果他们已经禁用邮件发送,您将需要使用另一种方法,使用第三方为您发送这些电子邮件。

给他们的技术支持的电子邮件(在他们的在线支持或常见问题解答之后)应该澄清在您的服务器上是否有电子邮件function。

确保localhost邮件服务器已configuration

如果您正在使用WAMP,MAMP或XAMPP在本地工作站上开发,则可能未在工作站上安装电子邮件服务器。 没有一个,PHP默认情况下不能发送邮件。

你可以通过安装一个基本的邮件服务器来克服 对于Windows,您可以使用免费的Mercury Mail 。

您也可以使用SMTP发送您的电子邮件。 从Vikas Dwivedi看到这个伟大的答案 ,学习如何做到这一点。

启用PHP的自定义mail.log

除了MTA和PHP的日志文件之外,您还可以专门为mail()函数启用日志loggingfunction 。 它不logging完整的SMTP交互,但至less包含函数调用参数和调用脚本。

 ini_set("mail.log", "/tmp/mail.log"); ini_set("mail.add_x_header", TRUE); 

有关详细信息,请参阅http://php.net/manual/en/mail.configuration.php 。 (最好在php.ini.user.ini.htaccess启用这些选项。)

使用不同的邮件

PHP的内置mail()函数非常方便,通常可以完成工作,但也有缺点 。 幸运的是,还有其他方法可以提供更多的function和灵活性,包括处理上述许多问题。 一个可能会考虑使用的是stream行的PHPMailer或SwiftMailer ,甚至是较早的PEAR :: Mail 。

在邮件function中添加邮件标题

 $header = "From: noreply@example.com\r\n"; $header.= "MIME-Version: 1.0\r\n"; $header.= "Content-Type: text/html; charset=ISO-8859-1\r\n"; $header.= "X-Priority: 1\r\n"; $status = mail($to, $subject, $message, $header); if($status) { echo '<p>Your mail has been sent!</p>'; } else { echo '<p>Something went wrong, Please try again!</p>'; } 
  1. 总是尝试在邮件function中发送标题。
  2. 如果您通过本地主机发送邮件,然后执行发送邮件的SMTP设置。
  3. 如果您通过服务器发送邮件,请检查您的服务器上是否启用了电子邮件发送function。

你使用SMTPconfiguration发送你的电子邮件? 尝试使用phpmailer来代替。 您可以从https://github.com/PHPMailer/PHPMailer下载该库。; 我以这种方式创build了我的电子邮件:

 function send_mail($email, $recipient_name, $message='') { require("phpmailer/class.phpmailer.php"); $mail = new PHPMailer(); $mail->CharSet="utf-8"; $mail->IsSMTP(); // set mailer to use SMTP $mail->Host = "mail.example.com"; // specify main and backup server $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = "myusername"; // SMTP username $mail->Password = "p@ssw0rd"; // SMTP password $mail->From = "me@walalang.com"; $mail->FromName = "System-Ad"; $mail->AddAddress($email, $recipient_name); $mail->WordWrap = 50; // set word wrap to 50 characters $mail->IsHTML(true); // set email format to HTML (true) or plain text (false) $mail->Subject = "This is a Sampleenter code here Email"; $mail->Body = $message; $mail->AltBody = "This is the body in plain text for non-HTML mail clients"; $mail->AddEmbeddedImage('images/logo.png', 'logo', 'logo.png'); $mail->addAttachment('files/file.xlsx'); if(!$mail->Send()) { echo "Message could not be sent. <p>"; echo "Mailer Error: " . $mail->ErrorInfo; exit; } echo "Message has been sent"; } 

它通过执行以下操作在000webhost上为我工作:

 $headers = "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"; 

发送电子邮件时直接input电子邮件地址

 mail('email@gmail.com', $subject, $message, $headers) 

使用''而不是""

这个代码的工作原理,但电子邮件收到了半小时的滞后

发送邮件之前只需添加一些标题:

 <?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"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html\r\n"; $headers .= 'From: from@example.com' . "\r\n" . 'Reply-To: reply@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); 

还有一件事。 mail()函数在localhost中不起作用。 上传你的代码到服务器,然后尝试。

试试这两个thigs分开和一起:

  1. 删除if($_POST['submit']){}
  2. 删除$from (只是我的肠道)

您可以使用codeigniter的configuration电子邮件,例如使用smtp(简单的方法):

 $config = Array( 'protocol' => 'smtp', 'smtp_host' => 'mail.domain.com', //your smtp host 'smtp_port' => 26, //default port smtp 'smtp_user' => 'name@domain.com', 'smtp_pass' => 'password', 'mailtype' => 'html', 'charset' => 'iso-8859-1', 'wordwrap' => TRUE ); $message = 'Your msg'; $this->load->library('email', $config); $this->email->from('name@domain.com', 'Title'); $this->email->to('emaildestination@domain.com'); $this->email->subject('Header'); $this->email->message($message); if($this->email->send()) { //conditional true } 

这对我有用!

对于任何发现这种情况的人,我不会推荐使用mail 。 这里有一些答案,但不是为什么

PHP的mailfunction不仅是不透明的,它完全依赖于你使用的任何MTA(即Sendmail)来完成这项工作。 mail只会告诉你,如果MTA不能接受它(即当你试图发送Sendmail时)。 它不能告诉你邮件是否成功,因为它已经交给了他。 因此(如约翰·孔德的答案细节),你现在可以摆弄MTA的日志,并希望它告诉你足够的关于修复它的失败。 如果您在共享主机上,或者无法访问MTA日志,那么您运气不佳。 不幸的是,大多数香草安装Linux的默认处理这种方式。

邮件库( PHPMailer ,Zend Framework 2+等)与mail很大不同。 他们所做的是直接向接收邮件服务器打开套接字,然后直接通过该套接字发送SMTP邮件命令。 换句话说,这个类作为自己的MTA(请注意,你可以告诉图书馆使用mail来发送邮件,但我强烈build议你不要这样做)。

这对您来说意味着您可以直接看到接收服务器的响应(例如在PHPMailer中,您可以打开debugging输出 )。 没有更多的猜测,如果邮件发送失败或为什么。

如果您使用的是SMTP(即您正在调用isSMTP() ),则可以使用SMTPDebug属性获取SMTP对话的详细logging。

通过在脚本中包含这样一行来设置此选项:

 $mail->SMTPDebug = 2; 

您还可以获得更好的界面。 有了mail你必须设置所有的头文件,附件等。有了一个库,你有一个专门的function来做到这一点。 这也意味着该function正在做所有棘手的部分(如标题)。

大部分mail()函数在共享主机中被禁用。 更好的select是使用SMTP。 最好的select是Gmail或SendGrid。


SMTPconfig.php

 <?php $SmtpServer="smtp.*.*"; $SmtpPort="2525"; //default $SmtpUser="***"; $SmtpPass="***"; ?> 

SMTPmail.php

 <?php class SMTPClient { function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body) { $this->SmtpServer = $SmtpServer; $this->SmtpUser = base64_encode ($SmtpUser); $this->SmtpPass = base64_encode ($SmtpPass); $this->from = $from; $this->to = $to; $this->subject = $subject; $this->body = $body; if ($SmtpPort == "") { $this->PortSMTP = 25; } else { $this->PortSMTP = $SmtpPort; } } function SendMail () { $newLine = "\r\n"; $headers = "MIME-Version: 1.0" . $newLine; $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine; if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP)) { fputs ($SMTPIN, "EHLO ".$HTTP_HOST."\r\n"); $talk["hello"] = fgets ( $SMTPIN, 1024 ); fputs($SMTPIN, "auth login\r\n"); $talk["res"]=fgets($SMTPIN,1024); fputs($SMTPIN, $this->SmtpUser."\r\n"); $talk["user"]=fgets($SMTPIN,1024); fputs($SMTPIN, $this->SmtpPass."\r\n"); $talk["pass"]=fgets($SMTPIN,256); fputs ($SMTPIN, "MAIL FROM: <".$this->from.">\r\n"); $talk["From"] = fgets ( $SMTPIN, 1024 ); fputs ($SMTPIN, "RCPT TO: <".$this->to.">\r\n"); $talk["To"] = fgets ($SMTPIN, 1024); fputs($SMTPIN, "DATA\r\n"); $talk["data"]=fgets( $SMTPIN,1024 ); 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"); $talk["send"]=fgets($SMTPIN,256); //CLOSE CONNECTION AND EXIT ... fputs ($SMTPIN, "QUIT\r\n"); fclose($SMTPIN); // } return $talk; } } ?> 

contact_email.php

 <?php include('SMTPconfig.php'); include('SMTPmail.php'); if($_SERVER["REQUEST_METHOD"] == "POST") { $to = ""; $from = $_POST['email']; $subject = "Enquiry"; $body = $_POST['name'].'</br>'.$_POST['companyName'].'</br>'.$_POST['tel'].'</br>'.'<hr />'.$_POST['message']; $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body); $SMTPChat = $SMTPMail->SendMail(); } ?> 

如果只使用mail()函数,则需要完成configuration文件。

您需要打开邮件扩展,并设置SMTP smtp_port等,最重要的是,您的用户名和密码。 没有这个,邮件不能发送。 另外,你可以使用PHPMail类来发送。

我认为这应该做的伎俩。 我刚刚添加了一个if(isset并添加了连接到正文中的variables来分隔PHP和HTML。

 <?php $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $from = 'From: yoursite.com'; $to = 'contact@yoursite.com'; $subject = 'Customer Inquiry'; $body = "From:" .$name."\r\n E-Mail:" .$email."\r\n Message:\r\n" .$message; if (isset($_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>'; } } ?> 
 $name = $_POST['name']; $email = $_POST['email']; $reciver = '/* Reciver Email address */'; if (filter_var($reciver, FILTER_VALIDATE_EMAIL)) { $subject = $name; // To send HTML mail, the Content-type header must be set. $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From:' . $email. "\r\n"; // Sender's Email //$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender $template = '<div style="padding:50px; color:white;">Hello ,<br/>' . '<br/><br/>' . 'Name:' .$name.'<br/>' . 'Email:' .$email.'<br/>' . '<br/>' . '</div>'; $sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" . $template . "</div>"; // Message lines should not exceed 70 characters (PHP rule), so wrap it. $sendmessage = wordwrap($sendmessage, 70); // Send mail by PHP Mail Function. mail($reciver, $subject, $sendmessage, $headers); echo "Your Query has been received, We will contact you soon."; } else { echo "<span>* invalid email *</span>"; } 

尝试这个

 <?php $to = "somebody@example.com, somebodyelse@example.com"; $subject = "HTML email"; $message = " <html> <head> <title>HTML email</title> </head> <body> <p>This email contains HTML Tags!</p> <table> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>John</td> <td>Doe</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: <webmaster@example.com>' . "\r\n"; $headers .= 'Cc: myboss@example.com' . "\r\n"; mail($to,$subject,$message,$headers); ?> 

尝试这个

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

如果您无法使用PHP发送邮件,请考虑使用PHPMailerSwiftMailer

当我需要用PHP发送邮件时,我通常使用SwiftMailer。


基本用法:

 require 'mail/swift_required.php'; $message = Swift_Message::newInstance() // The subject of your email ->setSubject('Jane Doe sends you a message') // The from address(es) ->setFrom(array('jane.doe@gmail.com' => 'Jane Doe')) // The to address(es) ->setTo(array('frank.stevens@gmail.com' => 'Frank Stevens')) // Here, you put the content of your email ->setBody('<h3>New message</h3><p>Here goes the rest of my message</p>', 'text/html'); if (Swift_Mailer::newInstance(Swift_MailTransport::newInstance())->send($message)) { echo json_encode([ "status" => "OK", "message" => 'Your message has been sent!' ], JSON_PRETTY_PRINT); } else { echo json_encode([ "status" => "error", "message" => 'Oops! Something went wrong!' ], JSON_PRETTY_PRINT); } 

有关如何使用SwiftMailer的更多信息,请参阅官方文档

你可以使用empty()和isset()函数。 如果你想使它与不同的文件工作,只需修改action='yourphp.php'到我给你的html,并将store the PHP scriptyourphp.php文件。 你也需要改变你的index.htmlindex.php来激活PHPfunction。

PHP

 <?php error_reporting(0); $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 (!(empty($_POST['name']))) { if (!(empty($_POST['email']))){ if (!(empty($_POST['message']))){ mail ($to, $subject, $body, $from); echo '<p>Your message has been sent!</p>'; }else{ echo '<p>Fill your message please.</p>';} }else { echo '<p>Fill your email please.</p>';} }else{ echo '<p>Fill your name please.</p>';} }else{ echo '<p>Fill the form.</p>';} ?> 

HTML

 <html> <form method="post" action="?"> <table> <tr><td>Name</td><td><input type='text' name='name' id='name'/></td></tr> <tr><td>Email</td><td><input type='text' name='email' id='email'/></td></tr> <tr><td>Message</td><td><input type='text' name='message' id='message'/></td></tr> <tr><td></td><td><input type='submit' name='submit' id='submit'/></td></tr> </table> </form> </html> 

最好的祝福!

首先,

您可能需要许多参数的邮件()函数…你可以有5最大。 mail(to,subject,message,headers,parameters); 至于$fromvariables去,这应该会自动来自您的虚拟主机,如果您使用Linux的cPanel。 它自动来自你的cPanel用户名和IP地址。

 $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"; 

还要确保你的mail()函数中有正确的variables顺序。 mail($to,$subject,$message,etc.) ,否则有可能无法正常工作。 让我知道这是否有帮助…

对于那些不想使用外部邮件程序并希望在专用的Linux服务器上邮件()的用户。

php邮件的方式在php.ini[mail function]一节中有描述。 参数sendmail-path描述如何调用sendmail。 默认值是sendmail -t -i ,所以如果你在linux控制台中运行sendmail -t -i < message.txt ,你就完成了。 你也可以添加mail.log来debugging,确保mail()真的被调用。

不同的MTA可以实现sendmail 。 例如,在debian中默认是postfix。 configuration您的MTA发送邮件并使用sendmail -v -t -i < message.txt从控制台进行testing。 文件message.txt应该包含一个消息和一个正文的所有标题,信封的目标地址将从To:标题中取出。 例:

 From: myapp@example.com To: mymail@example.com Subject: Test mail via sendmail. Text body. 

我优先使用ssmtp作为MTA,因为它很简单,不需要运行守护进程打开的端口。 ssmtp只适用于从本地主机发送邮件,它也可以通过您的帐户在公共邮件服务发送authentication电子邮件。 安装ssmtp并编辑config /etc/ssmtp/ssmtp.conf 。 为了还能够将本地系统邮件复制到unix帐户(例如,从cron作业),请configuration/etc/ssmtp/revaliases文件。

以下是我在Yandex邮件上的帐户configuration:

 root=mymail@example.com mailhub=smtp.yandex.ru:465 FromLineOverride=YES UseTLS=YES AuthUser=abcde@yandex.ru AuthPass=password 

这只会影响到一小部分用户,但是我希望这个小小的用户能够logging下来。 这一小部分成员花了6个小时解决一个正在运行的PHP邮件脚本,因为这个问题。

如果您要去一所从www.AceITLab.com运行XAMPP的大学,您应该知道我们的教授没有告诉我们:AceITLab防火墙(而不是Windows防火墙)在XAMPP中阻止了MercuryMail。 你将不得不使用另一个邮件客户端,梨正在为我们工作。 您必须发送到安全性较低的Gmail帐户。

是的,我知道,这对现实世界的电子邮件完全没用。 然而,从我所看到的,学术环境和现实世界往往有less许共同之处。

确保你的服务器上安装了Sendmail。

如果您已经检查了您的代码并确认没有任何错误,请转至/ var / mail并检查该文件夹是否为空。

如果它是空的,你需要做一个:

 sudo apt-get install sendmail 

如果你在Ubuntu服务器上。

您可以使用libmail: http ://lwest.free.fr/doc/php/lib/index.php3?page=mail&lang=en

 include "libmail.php"; $m = new Mail(); // create the mail $m->From( $_POST['form'] ); $m->To( $_POST['to'] ); $m->Subject( $_POST['subject'] ); $m->Body( $_POST['body'] ); $m->Cc( $_POST['cc']); $m->Priority(4); // attach a file of type image/gif to be displayed in the message if possible $m->Attach( "/home/leo/toto.gif", "image/gif", "inline" ); $m->Send(); // send the mail echo "Mail was sent:" echo $m->Get(); // show the mail source 

If you are running this code on a local server (ie your computer for development purposes) it wont send the email to the recipient. What will happen is, it will create a .txt file in a folder named mailoutput .

In the case if you are using a free hosing service like 000webhost or hostinger , those service providers disable the mail() function to prevent unintended uses of email spoofing, spamming etc. I prefer you to contact them to see whether they support this feature.

If you are sure that the service provider supports the mail() function, you can check this PHP manual for further reference, PHP mail()

To check weather your hosting service support the mail() function, try running this code, (Remember to change the recipient email address)

 <?php $to = 'nobody@example.com'; $subject = 'the subject'; $message = 'hello'; $headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ?> 

希望这有助于。

Why don't you use Papercut, You can test send mail in Your PC without Internet

you should use Papercut this simple application to test send mail. and you don't need to configure anything.

Just run it and try test send mail:

test_sendmail.php

 <?php $to = "somebody@example.com"; $subject = "My subject"; $txt = "Hello world!"; $headers = "From: webmaster@example.com" . "\r\n" . "CC: somebodyelse@example.com"; mail($to,$subject,$txt,$headers); ?> 

and you will see this:

在这里输入图像描述