一旦我的代码在 PHP 中运行,它就会说确认邮件已成功发送,但目标邮件 ID 尚未收到邮件。我已经使用PHP中的mail()
函数发送确认邮件,并且我还在我的Ubuntu上安装了Postfix。这里有什么问题?
<?php
include('config.php');
$tb_name = temp_members_db;
$confirm_code = md5(uniqid(rand()));
$name = $_POST['name'];
$email = $_POST['email'];
$pass = $_POST['pass'];
$country = $_POST['country'];
$sql = "INSERT INTO $tb_name(confirm_code,name,email,password,country) VALUES ('$confirm_code','$name','$email','$pass','$country')";
$result = mysql_query($sql);
if($result) {
$to = $email;
$sub = "Your Confirmation Code";
$message = "Your confirmation code is" . $confirm_code;
$send = mail($to,$sub,$message);
var_export($send);
} else {
echo "Havent found email ID in our database";
}
if($send) {
echo "Sent the confirmation link to your email ID";
} else {
echo "Sending failed";
}
?>
您是否在本地主机服务器上打开了 SMTP 功能?
如果未启用 SMTP 和邮件功能
PHPMailer 类。 非常容易安装和使用。PHP mail()
函数通常通过本地邮件服务器发送,通常在 Linux、BSD 和 OS X 平台上以sendmail
二进制文件为前面,但是,Windows 通常不包括本地邮件服务器;PHPMailer的集成SMTP实现允许在没有本地邮件服务器的情况下在Windows平台上发送电子邮件。
只需从这里下载类文件:https://github.com/PHPMailer/PHPMailer
例:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
设置SMTP,是否要使用安全加密(SSL,TLS和端口)
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // If you want to use encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to(or port 25,465 etc)
设置字段,如发件人、收件人、密件抄送、主题、电子邮件正文等。
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
// Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}