用PHP和Localhost发送电子邮件

用PHP和Localhost发送电子邮件,php,email,xampp,localhost,Php,Email,Xampp,Localhost,我已经找了几个小时的解决办法,但我就是不明白。 我对PHP相当陌生,我正在做一个需要发送电子邮件的项目,问题是我不知道怎么做。 其他人也有同样的问题,通过配置php.ini和sendmail.ini解决了这个问题 这是我当前的测试代码: <?php $subject="Hi There!!"; $to="mygmail@gmail.com"; $body="This is a demo email sent using PHP on XAMPP"; if (mail($to,$subje

我已经找了几个小时的解决办法,但我就是不明白。 我对PHP相当陌生,我正在做一个需要发送电子邮件的项目,问题是我不知道怎么做。 其他人也有同样的问题,通过配置php.ini和sendmail.ini解决了这个问题

这是我当前的测试代码:

<?php

$subject="Hi There!!";
$to="mygmail@gmail.com";
$body="This is a demo email sent using PHP on XAMPP";
if (mail($to,$subject,$body)){
 echo "Mail sent successfully!";
}
else{
 echo "Mail not sent!";
}

?>

我收到“已成功发送邮件”,但收件箱中没有任何内容,甚至垃圾邮件文件夹中也没有。 我也尝试过使用PHPMailer,但是(就像我说的)我对php相当陌生,我不知道如何在我的代码中启用它。
你们觉得怎么样?

嗨,鲁佩什,首先我很抱歉有两个人否决了我的帖子(很抱歉我不是像你们这样的编程天才),非常感谢你们的例子,但就像我说的,我真的不明白我应该如何安装PHPMailer,很抱歉我是个笨蛋。好的,我已经更新了帖子作为教程(忽略这些负面标记,今天对于复杂的问题,我也得到了-ve分,我也得到了stack的帮助。)好的,非常感谢Rupesh,现在它是一个魅力。再次非常感谢!!干杯…享受编码
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/Exception.php';
require 'PHPMailer/OAuth.php';
require 'PHPMailer/POP3.php';
require 'PHPMailer/SMTP.php';

$mail = new PHPMailer;

// SMTP configuration
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$mail->Password = '******';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('info@example.com', 'CodexWorld');
$mail->addReplyTo('info@example.com', 'CodexWorld');

// Add a recipient
$mail->addAddress('john@gmail.com');

// Add cc or bcc 
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

// Email subject
$mail->Subject = 'Send Email via SMTP using PHPMailer';

// Set email format to HTML
$mail->isHTML(true);

// Email body content
$mailContent = "<h1>Send HTML Email using SMTP in PHP</h1>
    <p>This is a test email has sent using SMTP mail server with PHPMailer.</p>";
$mail->Body = $mailContent;

// Send email
if(!$mail->send()){
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}else{
    echo 'Message has been sent';
}
-root (your proj root directory from where your php file runs)
  -index.php (file with above code)
  -PHPMailer(folder contain mailer library)
     -php mailer files (go to git repo, download and copy all files from src folder)