正确的路径PHPMailer XAMPP Mac

时间:2018-11-16 13:56:38

标签: php email phpmailer

我在XAMPP中的htdocs有我的项目文件夹,其中包含从github下载的PHPMailer文件夹。

在我的项目文件夹中,我还有一个文件test.php:

<?php

require('/PHPMailer/src/PHPMailer.php');

$mail = new PHPMailer(true);

// Send mail using Gmail
if($send_using_gmail){
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPAuth = true; // enable SMTP authentication
    $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
    $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
    $mail->Port = 465; // set the SMTP port for the GMAIL server
    $mail->Username = "myemail@gmail.com"; // GMAIL username
    $mail->Password = "mypw"; // GMAIL password
}

// Typical mail data
$mail->AddAddress($email, $name);
$mail->SetFrom($email_from, $name_from);
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";

try{
    $mail->Send();
    echo "Success!";
} catch(Exception $e){
    // Something went bad
    echo "Fail :(";
}

?>

这将返回此错误:

Warning: require(/PHPMailer/src/PHPMailer.php): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/My_Project/test.php on line 3

Fatal error: require(): Failed opening required '/PHPMailer/src/PHPMailer.php' (include_path='.:/Applications/XAMPP/xamppfiles/lib/php') in /Applications/XAMPP/xamppfiles/htdocs/My_Project/test.php on line 3

谁能告诉我我要去哪里错了?

1 个答案:

答案 0 :(得分:0)

如Oleg所说,您应该使用use composer,但是您的特定问题可能是使其成为绝对路径的斜线,因此请将其更改为:

require 'PHPMailer/src/PHPMailer.php';