[PHP 警告:mail():“sendmail_from”未在 php 中设置.ini或自定义“From:”标头丢失


[PHP Warning: mail(): " sendmail_from" not set in php.ini or custom "From:" header missing

我正在尝试使用 PHP 的 mail() 函数发送测试邮件。

$to = "****@gourab.me";
$sub = "Php Mail";
$msg = "Test Message From PHP";
mail($to, $sub, $msg, "From: **********@gmail.com");

当我尝试通过 step in phpdbg 调试它时,它会显示以下消息:

[PHP Warning: mail(): " sendmail_from" not set in php.ini or custom "From:" header 
missing in C:/xampp/htdocs/tinyProj/mail.php on line 4]

我不明白为什么?

您的From标题格式不正确。试试这个:

$headers =  'MIME-Version: 1.0' . "'r'n"; 
$headers .= 'From: Your name <info@address.com>' . "'r'n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n"; 
mail($to, $subject, $body, $headers);
兄弟,

您似乎正在使用自己的PC/localhost/127.0.0.1服务器,这就是您无法连接到SMTP服务器的原因。您只能使用类似的编码从实时服务器发送邮件,但有一些修改:)即添加一个参数"标头/发件人"。

mail("You@me.com","Answer","Hope You Vote My Answer Up","From: me@you.com");
<?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);
?>
<?php
if(isset($_POST['send'])){
     $from =  $_POST['femail'];
     $phoneno = $_POST['phoneno'];
     $message = $_POST['message'];
     $carrier = $_POST['carrier'];
     if(empty($from)){
       echo("enter the email");
       exit();
     } 
else if(empty($phoneno)){
   echo("enter the phone no");
     exit();
   }
 elseif(empty($carrier)){
   echo("enter the specific carrier");
   exit();
    }
 else if(empty($message)){
  echo("enter the message");
  exit();
  }
  else{
     $message = wordwrap($message, 70);
     $header = $from;
     $subject = 'from submission';
     $to = $phoneno.'@'.$carrier;
     $result = mail($to, $subject, $message, $header);
     echo("message sent to".$to);
  }
  }
?>