无法使用php中的mail函数发送电子邮件

无法使用php中的mail函数发送电子邮件,php,Php,我的公司交给我一个别人开发的现有项目。我必须解决其中的一些问题,但在其中一个问题上我被卡住了。电子邮件不是从网站发送的。我不知道为什么会这样。代码只是简单的php发送电子邮件的函数。但它仍然不起作用 任何人都能猜出我错过了什么 $to = "test@gmail.com"; $subject = "Property Posted"; $message="TestMessage" $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Cont

我的公司交给我一个别人开发的现有项目。我必须解决其中的一些问题,但在其中一个问题上我被卡住了。电子邮件不是从网站发送的。我不知道为什么会这样。代码只是简单的php发送电子邮件的函数。但它仍然不起作用 任何人都能猜出我错过了什么

$to = "test@gmail.com";
$subject = "Property Posted";

$message="TestMessage"
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$form="www.xxxxxxxxxxxxxxxxxxxxx.com";

// More headers    
$headers .= 'From: <xyz@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

$checkmail = mail($to,$subject,$message,$headers,$form);
if($checkmail) {
    echo "email sent";
} else {
    echo failed"
}
$to=”test@gmail.com";
$subject=“已过账的财产”;
$message=“TestMessage”
$headers=“MIME版本:1.0”。“\r\n”;
$headers.=“内容类型:text/html;字符集=UTF-8”。“\r\n”;
$form=“www.xxxxxxxxxxxxxxxxxx.com”;
//更多标题
$headers.='From:'。“\r\n”;
$headers.='Cc:myboss@example.com' . “\r\n”;
$checkmail=mail($to、$subject、$message、$headers、$form);
如果($checkmail){
回显“已发送电子邮件”;
}否则{
回显失败“
}

虽然我不知道为什么,但它总是会点击
其他

您没有定义
$to
$subject
。请检查以下代码:

 <?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);
  ?>

您没有定义
$to
$subject
。请检查以下代码:

 <?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的mail()函数是可能的。请记住,mail函数在本地服务器中不起作用

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

 mail($to, $subject, $message, $headers);
 ?>

使用PHP的mail()函数是可能的。请记住,mail函数在本地服务器中不起作用

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

 mail($to, $subject, $message, $headers);
 ?>

通过在命令行上执行以下命令来测试服务器邮件服务

echo "message" | mail -s subject youremail@gmail.com
然后尝试下面的代码

error_reporting(E_ALL);
ini_set('display_errors',1);

$to = "test@gmail.com";
$subject = "Property Posted";
$message="TestMessage";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$message,$headers);

通过在命令行上执行以下命令来测试服务器邮件服务

echo "message" | mail -s subject youremail@gmail.com
然后尝试下面的代码

error_reporting(E_ALL);
ini_set('display_errors',1);

$to = "test@gmail.com";
$subject = "Property Posted";
$message="TestMessage";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$message,$headers);

有时电子邮件会变成垃圾邮件,所以另一种可能是通过pear安装邮件和网络smtp

pear install Mail
pear install Net_Smtp
然后,您可以通过SMTP身份验证将邮件发送到另一台服务器:

require_once "Mail.php";

$body = "messages in body part\n";
$subject = "Subject of email message";
$mail_to = "abc@gmail.com";
$mail_from = "efg@gmail.com";

//SMTP Configuration
$host = "smtp.server.com""; // Or IP address";
$username = "username";
$password = "password";

$smtp = Mail::factory('smtp',
 array (
 'host' => $host,
 'auth' => true,
 'username' => $username,
 'password' => $password
));

$headers = array (
 'From' => $mail_from,
 'To' => $mail_to,
 'Subject' => $subject
);
$mail = $smtp->send($mail_to, $headers, $body);

if (PEAR::isError($mail)) {
 echo "Cound not seend the message";
}

有时电子邮件会变成垃圾邮件,所以另一种可能是通过pear安装邮件和网络smtp

pear install Mail
pear install Net_Smtp
然后,您可以通过SMTP身份验证将邮件发送到另一台服务器:

require_once "Mail.php";

$body = "messages in body part\n";
$subject = "Subject of email message";
$mail_to = "abc@gmail.com";
$mail_from = "efg@gmail.com";

//SMTP Configuration
$host = "smtp.server.com""; // Or IP address";
$username = "username";
$password = "password";

$smtp = Mail::factory('smtp',
 array (
 'host' => $host,
 'auth' => true,
 'username' => $username,
 'password' => $password
));

$headers = array (
 'From' => $mail_from,
 'To' => $mail_to,
 'Subject' => $subject
);
$mail = $smtp->send($mail_to, $headers, $body);

if (PEAR::isError($mail)) {
 echo "Cound not seend the message";
}

签入$to我已发送电子邮件。但我没有在此处显示。在邮件功能中,我删除了$form并尝试了它,但仍然无法发送电子邮件@Anant@Anant根据
mail()
文档,
mail()
接受5,第五个是
附加参数(可选)虽然我不知道这里是否正确使用了第5个参数……很难说。@AzadChauhan您是否尝试过使用邮件函数的基本输入来查看是否失败?
echo mail('my@email.com“,”主题“,”信息“,”发件人:my@email.com);
例如(
my@email.com
当然是你的电子邮件)?这可能是第一件要尝试的事情。请检查它的完整代码send email@Anantcheck in$to我已经通过了电子邮件。但我没有在这里显示它。在邮件功能中,我删除了$表单并尝试了它,但仍然无法发送电子邮件@Anant@Anant根据
mail()
文档,
mail()
接受5,第五个是
附加参数
(可选),尽管我不知道第五个参数在这里是否正确使用……很难说。@AzadChauhan您是否尝试过使用邮件功能的基本输入来查看是否失败?
回送邮件('my@email.com“,”主题“,”信息“,”发件人:my@email.com);
例如(
my@email.com
当然是你的电子邮件)?这可能是第一件应该尝试的事情。请检查它的发送电子邮件@AnantI的完整代码。我已经声明了所有这些,但我没有在这里显示完整代码。我只是发布了简单的发送电子邮件代码。请使用上面的代码发送电子邮件。我已经声明了所有这些,但我没有在这里显示完整代码。我只是发布了简单的发送电子邮件代码请使用以上代码发送电子邮件