我是PHP的新手,并且正在做一个仅在本地主机上的小项目。我需要它来实际发送电子邮件,以便可以验证它是否正常运行。程序是如何工作的,管理员可以填写表格并向数据库中的用户发送电子邮件,该表格将发送到运行API发送的页面。我使用Mailjet的示例进行了尝试,它实际上已将电子邮件发送给指定的收件人,但是我需要从表单中传递收件人,主题和正文,并且不会发送我在做什么错了?
emailForm.php
<div id = "mail-create" class="col-6 border" style="display: none;">
<h3 class="text-primary text-center">Send mail</h3>
<form method="post" action="sendEmail.php">
<input type="hidden" name="admin" value="1">
<?php include('errors.php'); ?>
<div class="form-group">
<label for="firstname">Choose user</label>
<select name="userEmail" id="userEmail">
<?php foreach ($users as $user): ?>
<option value="<?=$user['email'] ?>"><?=$user['username'] ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group" action="action.php">
<label for="subject">Subject</label>
<input type="text" class="form-control" name="subject" id="subject">
</div>
<div class="form-group">
<label for="message">Message</label><br>
<textarea name="message" id="message" cols="50" rows="8" placeholder="Write message body here"></textarea>
</div>
<button type="submit" name="sendMail" value="send" class="btn btn-primary mb-2" >Send mail</button>
</form>
</div>
sendEmail.php
<?php
require 'vendor/autoload.php';
use \Mailjet\Resources;
if(isset($_POST[‘sendMail’]){
//passed in my actual api keys
$mj = new \Mailjet\Client('MJ_APIKEY_PUBLIC','MJ_APIKEY_PRIVATE',true,['version' => 'v3.1']);
$body = [
'Messages' => [
[
'From' => [
'Email' => "email@email.com",
'Name' => "name"
],
'To' => [
[
'Email' => $_POST['userEmail'],
'Name' => "name"
]
],
'Subject' => $_POST['userEmail'],
'TextPart' => "My first Mailjet email",
'HTMLPart' =>$_POST['message'],
'CustomID' => "AppGettingStartedTest"
]
]
];
$response = $mj->post(Resources::$Email, ['body' => $body]);
$response->success() && var_dump($response->getData());
}
?>