PHP 邮件功能 returns false 尽管所有设置

PHP mail function returns false despite all the settings

我是PHP的新手,所以我所知道的实际上都是来自论坛。这些是我在 php.ini 文件

中所做的设置
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = engr.atiq19@gmail.com
sendmail_path = "\"C:\xamppnew\sendmail\sendmail.exe\" -t"
;sendmail_path = "C:\xamppnew\mailtodisk\mailtodisk.exe"

这些是在 sendmail.ini 文件

中所做的更改
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=engr.atiq19@gmail.com
auth_password=************
force_sender=engr.atiq19@gmail.com

这是我用来发送邮件的代码

$to = "engr.atiq19@gmail.com";
$myemail = "engr.atiq19@gmail.com";    
$email_subject = "Contact form submission: $name";
$email_body = "my message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
header('Location: ../index-alt2.html?t="done successfully"');

我建议使用 PHPMailer 从 PHP 发送电子邮件。以下是完成此操作的步骤。

  1. 转到 Github repository
  2. 下载 ZIP。
  3. 将其解压到您的 public_html 目录中。
  4. include '/path/to/PHPMailer/PHPMailerAutoload.php'; 在 PHP 脚本的顶部。
  5. 像往常一样从 HTML 表单中获取值。

这是一个例子...

index.html

<form action="index.php" method="post">
    <input type="email" name="email">
    <input type="text" name="name">
    <input type="text" name="subject">
    <input type="text" name="message">
</form>

index.php

include '/path/to/PHPMailer/PHPMailerAutoload.php';

$email = $_POST['email'];
$name = $_POST['name'];
$subject = $_POST['subject'];
$message = $_POST['message'];

$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'localhost'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, "ssl" also accepted
$mail->Port = 587; // TCP port to connect to

$mail->setFrom('your email', 'your name'); // from
$mail->addAddress($email, $name); // to
$mail->isHTML(true); // if html

$mail->Subject = $subject;
$mail->Body = $message; //HTML

if($mail->send()){
    echo 'Mail sent!';
}
else {
    echo 'Mail failed!';
}