我正在尝试通过pear发送电子邮件,它工作正常,但问题是每当我刷新页面时都会发送电子邮件,但是我试图通过单击提交按钮发送邮件我希望点击提交按钮发送电子邮件,我不知道如何将我的代码嵌入到此,所以我只能在提交按钮时发送邮件,我已经尝试但无法弄清楚。提前感谢您的帮助。
<?php
require_once 'Mail.php'; //this loads up PEAR Mail
if (!class_exists('Mail')) { die('Error: The PEAR Mail class does not exist.'); }
$host = 'ssl://smtp.gmail.com';
$username = 'softphoton001@gmail.com'; //replace this with your new Gmail address
$password = 'softphoton001softphoton00'; //replace this with your new Gmail account password
$port = '465';
$from_email = $username;
$from = '"softphoton" <'.$from_email.'>'; //put your name in here, but keep the double quotes
$to = '"softphoton002" <softphoton002@gmail.com>'; //put in your name and main email address to send a test to
$subject = 'Test Message';
$body = 'If you’re reading this, our test worked test rahul2.';
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject,
'Reply-To' => $from_email,
'MIME-Version' => 1,
'Content-type' => 'text/html;UTF-8'
);
$params = array(
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password
);
$smtp = Mail::factory('smtp', $params);
echo '<p>Preparing to send the test message...</p>';
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo '<p>Error! '.$mail->getMessage().'</p>';
} else {
echo '<p>Your test message was sent successfully.</p>';
}
?>
<form action="login.php" method="post">
<label for="username"><b>username</b></label>
<input type="text" placeholder="Enter Email" name="username" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="password" required>
<button type="submit" name="submit">Sign Up</button>
</form>