我真的需要一些帮助。
所以,我正在为学校制作一个提示库,我制作了一个注册系统,您可以在其中输入您的电子邮件和密码并获得一个帐户。之后,它应该向用户发送一封电子邮件以进行验证。我在 sendgrid 上创建了一个帐户来执行此操作并且我有一个 API 密钥,但我的代码一直告诉我它不起作用。
这就是我的代码的样子:
<?php
if(!empty($_POST)){
$email = $_POST["email"];
$options = [
'cost' => 12,
];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT, $options);
try {
// Connect to database
$conn = new PDO('mysql:host=localhost;dbname=promptswap', "evi", "12345");
// Check if email is already in use
$query = $conn->prepare("SELECT COUNT(*) FROM users WHERE email = :email");
$query->bindValue(":email", $email);
$query->execute();
$count = $query->fetchColumn();
// If email is not in use and is valid, create a new user
if ($count == 0 && filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Insert user into database
$query = $conn->prepare("INSERT INTO users (email, password) VALUES (:email, :password)");
$query->bindValue(":email", $email);
$query->bindValue(":password", $password);
$query->execute();
// Send verification email using SendGrid API
$from = new SendGrid\Email("PromptSwap", "[email protected]"); //here I use my email that I registered on sendgrid
$to = new SendGrid\Email(null, $email);
$subject = "Verify Your Email Address";
$token = bin2hex(random_bytes(32)); // Generate random token
$verificationLink = "http://yourwebsite.com/verify.php?email=".urlencode($email)."&token=".urlencode($token); // Change this to your website's URL and verify.php file
$content = new SendGrid\Content("text/html", "Click the following link to verify your email address: <a href='$verificationLink'>$verificationLink</a>");
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = '*****'; //my api key is here where the ***** is
$sg = new \SendGrid($apiKey);
$sg->client->mail()->send()->post($mail);
header("Location: verify.php"); // Change this to your website's verify.php file
} else {
$error = "Invalid email or email already in use.";
}
} catch (Throwable $error){
$error = $e->getMessage();
}
}
?>
我得到的错误是
new SendGrid\Email
行(和其他行)没有被识别,它是一个未定义的对象。
“未定义类型‘SendGrid\Mail\HtmlContent’.intelephense(1009)”
我确实用
composer require sendgrid/sendgrid
在我的作曲家中安装了所有东西,我也把这个 require 'vendor/autoload.php';
放在代码中,然后它仍然没有用。我也试过require_once __DIR__ . '/vendor/autoload.php';
。然后我也尝试这样做:
use SendGrid\Mail\From;
use SendGrid\Mail\HtmlContent;
use SendGrid\Mail\Mail;
use SendGrid\Mail\PlainTextContent;
use SendGrid\Mail\Subject;
use SendGrid\Mail\To;
像这样更改我的代码:
use SendGrid\Mail\From;
$from = new From("[email protected]", "Example User");
我的终端告诉我一切正常:
C:\xampp\htdocs\promptswap\AIAI-prompt-platform-main>composer require sendgrid/sendgrid
Info from https://repo.packagist.org: #StandWithUkraine
./composer.json has been updated
Running composer update sendgrid/sendgrid
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
No security vulnerability advisories found
Using version ^8.0 for sendgrid/sendgrid
我也是这样写的:
$from = new SendGrid\Email("PromptSwap", "[email protected]");
$to = new SendGrid\Email(null, $email);
$subject = "Verify Your Email Address";
$token = bin2hex(random_bytes(32)); // Generate random token
$verificationLink = "http://yourwebsite.com/verify.php?email=".urlencode($email)."&token=".urlencode($token); // Change this to your website's URL and verify.php file
$content = new SendGrid\Mail\HtmlContent("text/html", "Click the following link to verify your email address: <a href='$verificationLink'>$verificationLink</a>");
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = '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'; // Replace with your API key
$sg = new \SendGrid($apiKey);
$sg->client->mail()->send()->post($mail);
真的什么都没用!有谁知道我该如何解决这个问题?或者另一种使用电子邮件验证的方法?谢谢!