在AWS Elasticbeanstalk上部署一段代码,向该用户发送电子邮件验证以创建帐户。我测试了电子邮件代码,它可以在本地计算机上运行。为了在服务器上测试它,我编写了这个测试用例,它可以工作。但是,当它由应该使用它的模块调用时,它就会中断。我需要有关故障排除的帮助。
由于同一模块由2个不同的模块(测试和生产)调用,并且可以与测试一起使用,所以我倾向于认为它与调用模块有关,但是除了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 = "[email protected]";
$mail->Password = "BLANKED_OUT_FOR_STACKOVERFLOW";
$mail->Subject = "Account Verification";
$mail->setFrom("[email protected]");
$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!
}
}
?>
调用电子邮件验证文件的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";
}
}
基于Synchro的输入,有效的解决方案是:
require_once( __DIR__.'/../PHPMailer/PHPMailer.php');
require_once( __DIR__.'/../PHPMailer/SMTP.php');
require_once( __DIR__.'/../PHPMailer/Exception.php');