经过多次介绍,
我做了以下事情:
这是php.ini:
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = xx@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
sendmail.ini:
smtp_server=smtp.gmail.com
smtp_port=25
smtp_ssl=auto
error_logfile=error.log
debug_logfile=debug.log
auth_username=xx@gmail.com
auth_password=xxyyxxyy
force_sender=xx@gmail.com
这是前端代码:
<form action="send.php" method="post" id="newsletter" name="newsletter">
<input type="text" name="signup-email" id="signup-email" value="" />
<button id="actbtn" class="btn btn-7 btn-7h icon-envelope">Subscribe!</button>
<span class="arrow"></span>
</form>
<div id="response"></div>
</div>
终于send.php:
<?php
$host = "localhost";
$dbname = "database-name";
$user = "";
$pass = "";
$email = filter_var($_POST['signup-email'], FILTER_SANITIZE_EMAIL);
$datetime = date('Y-m-d H:i:s');
try {
$db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
if (empty($email)) {
$status = "error";
$message = "The email address field must not be blank";
} else if (!preg_match('/^[^0-9][A-z0-9._%+-]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/', $email)) {
$status = "error";
$message = "You must fill the field with a valid email address";
} else {
$existingSignup = $db->prepare("SELECT COUNT(*) FROM signups WHERE signup_email_address='$email'");
$existingSignup->execute();
$data_exists = ($existingSignup->fetchColumn() > 0) ? true : false;
if (!$data_exists) {
$sql = "INSERT INTO signups (signup_email_address, signup_date) VALUES (:email, :datetime)";
$q = $db->prepare($sql);
$q->execute(
array(
':email' => $email,
':datetime' => $datetime
));
if ($q) {
$status = "success";
$message = "You have been successfully subscribed";
} else {
$status = "error";
$message = "An error occurred, please try again";
}
} else {
$status = "error";
$message = "This email is already subscribed";
}
}
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);
$db = null;
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
因此,当我在时事通讯表格中输入电子邮件时,它在我的邮件帐户中没有收到任何邮件。
我正在使用xampp。
有人可以帮助我吗?
还是我错过了什么?谢谢。
答案 0 :(得分:0)
不要在php.ini中更改任何内容。使用phpmailer从本地发送邮件 Download phpmailer
使用此代码 -
func2
希望这会有所帮助.. :)
表 -
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'gmailemail'; // SMTP username
$mail->Password = 'gmailpassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'from@gmail.com';
$mail->FromName = 'GOKU';
$mail->addAddress('qwe@qwe.com'); // Add a recipient
$mail->addAddress('qwe@qwe.com'); // Name is optional
// $mail->addReplyTo('info@example.com', 'Information');
// $mail->addCC('cc@example.com');
$mail->addBCC('qwe.qwe@gmail.com');
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'hello';
$mail->Body = 'Hi<br>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
在send.php中-
<form action="send.php" method="post" id="newsletter" name="newsletter">
<input type="text" name="signup-email" id="signup-email" value="" />
<input type="submit" id="actbtn" class="btn btn-7 btn-7h icon-envelope" value="Subscribe!" name="subscribe">
<span class="arrow"></span>
</form>