如何在Windows上使用PHP发送电子邮件?

我的代码不工作,我该如何解决这个问题? 我想在我的雅虎ID上收到邮件。

<?php // the message $msg = "First line of text\nSecond line of text"; // use wordwrap() if lines are longer than 70 characters $msg = wordwrap($msg,70); // send email mail("faisalkhan00668@yahoo.com","My subject",$msg); ?> 

错误:

警告:邮件()[函数。邮件]:“sendmail_from”未在php.ini中设置或自定义“发件人:”头在第9行C:\ xampp \ htdocs \ mail \ mail.php中缺less

在第四个参数中,您可以向邮件添加标题。 在这里你可以添加你的来源:

在这里的PHP网站解释http://php.net/manual/en/function.mail.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); 

这将解决从问题

现在在php.ini中有一行

 ;sendmail_from = postmaster@localhost 

如果你取消注释; 你可以设置一个默认值。 在这种情况下,您将不必为header mail()调用添加标题。

你使用SMTP吗? 尝试在你的php代码的开头插入这两行:

ini_set ("SMTP","localhost"); ini_set ("sendmail_from","xxxyourmailxxx@xxxxxx.xxx");

这可以使用mail()函数完成。 请记住,这不会在本地服务器上工作。

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

要创建SMTP服务器,您需要执行以下操作:

创建SMTPConfig.php

 <?php //These need to be changed to actual values $Smtpserver="127.0.0.1"; $SmtpPort="25"; //default $SmtpUser="username"; $SmtpPass="password"; ?> 

将以下代码添加到index.php文件中:

 <?php include('SMTPconfig.php'); include('SMTPClass.php'); if($_SERVER["REQUEST_METHOD"] == "POST") { $to = $_POST['to']; $from = $_POST['from']; $subject = $_POST['sub']; $body = $_POST['message']; $SMTPMail = new SMTPClient ($Smtpserver, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body); $SMTPChat = $SMTPMail->SendMail(); } ?> <form method="post" action=""> To:<input type="text" name="to" /> From :<input type='text' name="from" /> Subject :<input type='text' name="sub" /> Message :<textarea name="message"></textarea> <input type="submit" value=" Send " />