我正在创建一个必须发送电子邮件确认的注册系统。 我已经安装了laragon,我使用的是php mailer,5.2.14版本。 phpmyadmin设置良好,它接受用户的注册数据,但没有发送电子邮件。
允许我创建用户并发送电子邮件的注册代码(即function.php内部)如下:
function createuser($first_name, $last_name, $username, $email, $password)
{
global $url;
$first_name = escape($first_name);
$last_name = escape($last_name);
$username = escape($username);
$email = escape($email);
$password = escape($password);
$password = password_hash($password);
$token = password_hash($username . microtime());
$sql = "INSERT INTO users(first_name,last_name,username,email,password,token,activition) ";
$sql .= "VALUES('$first_name','$last_name','$username','$email','$password','$token',0)";
confirm(query($sql));
//this is the message that is going to appear to user and it says to open the email where there's the activation link
set_message('Per favore controlla la tua email o la tua cartella spam per vedere il link di attivazione');
//this is the content of the email, that contains my email
$subject = "Attivazione account";
$msg = "Clicca su questo link per attivare il tuo account
$url/activate.php?email=$email&code=$token";
$headers = "From: caredda.sara@gmail.com";
send_email($email, $subject, $msg, $headers);
redirect('index.php');
}
//this is the function to send the email
function send_email($email, $subject, $msg, $headers)
{
return mail($email, $subject, $msg, $headers);
}
我创建了另一个文件,其中有注册系统的html代码,它链接到function.php文件。 在html代码之前,我复制粘贴了phpmailer代码,并进行了自定义修改。 代码如下:
<?php if(isset($_POST['sendmail'])) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
// $mail->SMTPDebug = 4; // 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 = 'caredda.sara@gmail.com'; // SMTP username
$mail->Password = 'my_google_account_password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('caredda.sara@gmail.com', 'Sara Caredda');
$mail->addAddress($_POST['email']); // Add a recipient
$mail->addReplyTo('caredda.sara@gmail.com');
// print_r($_FILES['file']); exit;
for ($i=0; $i < count($_FILES['file']['tmp_name']) ; $i++) {
$mail->addAttachment($_FILES['file']['tmp_name'][$i], $_FILES['file']['name'][$i]); // Optional name
}
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_POST['subject'];
$mail->Body = '<div style="border:2px solid red;">Body del messaggio html<b>in bold!</b></div>';
$mail->AltBody = $_POST['message'];
if(!$mail->send()) {
echo 'Il messaggio non è stato mandato.';
echo 'Errore Mailer: ' . $mail->ErrorInfo;
} else {
echo 'Il messaggio è stato mandato';
}
}
?>
这就是全部。 这段代码不发送任何电子邮件,而我是一个学生,我正在试图理解为什么。 我花了整个晚上的时间。 我想,现在是寻求帮助的时候了。 你能帮帮我吗?
我想是关于授权的问题。 你需要访问谷歌帐户的安全页面,并打开“不太安全的应用程序访问”的访问。