【发布时间】:2020-07-13 13:05:32
【问题描述】:
在 AWS elasticbeanstalk 上部署一段代码,向用户发送一封电子邮件验证以创建帐户。我测试了电子邮件代码,它可以在我的本地机器上运行。为了在服务器上测试它,我编写了这个测试用例并且它可以工作。但是当它被应该使用它的模块调用时,它就会中断。我需要故障排除方面的帮助。
由于同一个模块被 2 个不同的模块(test 和 prod)调用,并且它与 test 一起工作,我倾向于认为它与调用模块有关,但除了 include 语句的格式之外,我不不知道它会如何破裂。
工作测试代码:
测试结果:
[Wed Apr 01 16:45:53.262139 2020] [php7:notice] [pid 10670] [client 136.55.180.140:29275] Array\n(\n [count] => 1\n)\n, referer: http://bluetooth-env-test.eba-brqgvwur.us-east-2.elasticbeanstalk.com/WebApp/new_user.php
[Wed Apr 01 16:47:40.599465 2020] [php7:warn] [pid 10669] [client 136.55.180.140:29293] PHP Warning: require_once(../PHPMailer/PHPMailer.php): failed to open stream: No such file or directory in /var/app/current/WebApp/utils/emailVerification.php on line 13, referer: http://bluetooth-env-test.eba-brqgvwur.us-east-2.elasticbeanstalk.com/WebApp/new_user.php
[Wed Apr 01 16:47:40.599502 2020] [php7:error] [pid 10669] [client 136.55.180.140:29293] PHP Fatal error: require_once(): Failed opening required '../PHPMailer/PHPMailer.php' (include_path='.:/usr/share/pear7:/usr/share/php') in /var/app/current/WebApp/utils/emailVerification.php on line 13, referer: http://bluetooth-env-test.eba-brqgvwur.us-east-2.elasticbeanstalk.com/WebApp/new_user.php
共享模块的源代码: 它在 require_once('../PHPMailer/PHPMailer.php'); 处中断;
<?php
// session_start();
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
function emailVerify($email, $hash){
require_once('../PHPMailer/PHPMailer.php');
require_once('../PHPMailer/SMTP.php');
require_once('../PHPMailer/Exception.php');
echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."\n";
$mail = new PHPMailer(true);
try{
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->isSMTP();
$mail->Host = gethostbyname('smtp.gmail.com');
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->Username = "bluetooth.project.test@gmail.com";
$mail->Password = "BLANKED_OUT_FOR_STACKOVERFLOW";
$mail->Subject = "Account Verification";
$mail->setFrom("bluetooth.project.test@gmail.com");
$mail->Body = ($_SESSION['root']."/utils/verify?key=".$hash."&email=".$email);
$mail->addAddress($email);
if ($mail->Send()){
echo "success";
}else{
echo "failure\n";
// print_r($mail);
}
$mail->smtpClose();
}catch (phpmailerException $e) {
$errors[] = $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
$errors[] = $e->getMessage(); //Boring error messages from anything else!
}
}
?>
调用 emailVerification 文件的 new_user 模块的一部分:
<?php
session_start();
include "./utils/bootstrap.php";
include "./utils/connection.php";
include "./utils/emailVerification.php";
// include "navbar.php";
// include "utils/bootstrap_js.php";
//If insert if successful
if(isset($_POST['cancel'])){
header("location: index.php");
return;
}
if(isset($_POST['add']) ){
$sql="insert into users( fname, lname, email, password,role, hash)
values (:fname,:lname,:email,:password,:role, :hash)";
$salt = "XyZzy12*_";
$check = hash('md5', $salt.$_POST['pass1']);
$email = $_POST['email'];
$role = checkRole($email, $pdo);
$hash = md5( rand(0,1000) );
if($role == "invalid"){
header("location: ./new_user.php");
return;
}
emailVerify($email, $hash);
$statement = $pdo->prepare($sql);
$statement->execute(
array(
':fname'=> $_POST['firstName'],
':lname'=> $_POST['lastName'],
':email'=>$email,
':password'=>$check,
':role'=>$role,
':hash'=>$hash
)
);
$_SESSION['insert'] = "New User Successfully Created - Please verify by Email";
header("location: new_user.php");
return;
}
function checkRole($email, $pdo){
return "student";
}
}
【问题讨论】:
-
这表明存在包含路径问题;请记住,当您使用相对路径调用
require时,它是相对于调用脚本 而不是 出现require的文件的路径。为您的要求添加前缀带有__DIR__的路径,以确保您从哪里加载。您不使用 composer 有什么特别的原因可以让这一切变得更容易处理吗? -
之前没用过composer,我研究一下。我会尽快尝试 dir 建议。
-
@Synchro DIR 工作
标签: php amazon-elastic-beanstalk phpmailer