未通过电子邮件发送PHP

问题描述

我已经创建了一个发送HTML电子邮件模板的php文件。这是用户注册软件的帐户激活过程的一部分。电子邮件不会到达。我正在使用网络托管服务,因此我知道我具有电子邮件功能。

这是config.php

df.show()
+---+--------------------+
| Id|     Variable_Column|
+---+--------------------+
|  1|[[col2 -> val3,c...|
+---+--------------------+

这是main.php

<?php
// database hostname
define('db_host','hostname');
// database username
define('db_user','username');
// database password
define('db_pass','password');
// database name
define('db_name','database');
// database charset
define('db_charset','utf8');
// Email activation variables
// account activation required?
define('account_activation',true);
define('mail_from','StanCafe <noreply@stancafe.com>');
// Link to activation file
define('activation_link','https://stancafe.com/StanCafe/activate.php');
?>

这是register.php

<?php
// The main file contains the database connection,session initializing,and functions,other PHP files will depend on this file.
// Include thee configuration file
include_once 'config.php';
// We need to use sessions,so you should always start sessions using the below code.
session_start();
// No need to edit below
try {
    $pdo = new PDO('mysql:host=' . db_host . ';dbname=' . db_name . ';charset=' . db_charset,db_user,db_pass);
} catch (PDOException $exception) {
    // If there is an error with the connection,stop the script and display the error.
    exit('Failed to connect to database!');
}
// The below function will check if the user is logged-in and also check the remember me cookie
function check_loggedin($pdo,$redirect_file = 'index.php') {
    // Check for remember me cookie variable and loggedin session variable
    if (isset($_COOKIE['rememberme']) && !empty($_COOKIE['rememberme']) && !isset($_SESSION['loggedin'])) {
        // If the remember me cookie matches one in the database then we can update the session variables.
        $stmt = $pdo->prepare('SELECT * FROM accounts WHERE rememberme = ?');
        $stmt->execute([ $_COOKIE['rememberme'] ]);
        $account = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($account) {
            // Found a match,update the session variables and keep the user logged-in
            session_regenerate_id();
            $_SESSION['loggedin'] = TRUE;
            $_SESSION['name'] = $account['username'];
            $_SESSION['id'] = $account['id'];
            $_SESSION['role'] = $account['role'];
        } else {
            // If the user is not remembered redirect to the login page.
            header('Location: ' . $redirect_file);
            exit;
        }
    } else if (!isset($_SESSION['loggedin'])) {
        // If the user is not logged in redirect to the login page.
        header('Location: ' . $redirect_file);
        exit;
    }
}
// Send activation email function
function send_activation_email($email,$code) {
    $subject = 'Account Activation Required';
    $headers = 'From: noreply@stancafe.com' . mail_from . "\r\n" . 'Reply-To: noreply@stancafe.com' . mail_from . "\r\n" . 'Return-Path: noreply@stancafe.com' . mail_from . "\r\n" . 'X-Mailer: PHP/' . phpversion() . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-Type: text/html; charset=UTF-8' . "\r\n";
    $activate_link = activation_link . '?email=' . $email . '&code=' . $code;
    $email_template = str_replace('%link%',$activate_link,file_get_contents('https://stancafe.com/StanCafe/activation-email-template.html'));
    mail($email,$subject,$email_template,$headers,'-f ' . mail_from);
}
?>

这是Activation-email-template.html

<?php
include 'main.php';
// Now we check if the data was submitted,isset() function will check if the data exists.
if (!isset($_POST['username'],$_POST['password'],$_POST['cpassword'],$_POST['email'])) {
    // Could not get the data that should have been sent.
    exit('Please complete the registration form!');
}
// Make sure the submitted registration values are not empty.
if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email'])) {
    // One or more values are empty.
    exit('Please complete the registration form');
}
// Check to see if the email is valid.
if (!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
    exit('Email is not valid!');
}
// Username must contain only characters and numbers.
if (!preg_match('/^[a-zA-Z0-9]+$/',$_POST['username'])) {
    exit('Username is not valid!');
}
// Password must be between 5 and 20 characters long.
if (strlen($_POST['password']) > 20 || strlen($_POST['password']) < 5) {
    exit('Password must be between 5 and 20 characters long!');
}
// Check if both the password and confirm password fields match
if ($_POST['cpassword'] != $_POST['password']) {
    exit('Passwords do not match!');
}
// Check if the account with that username already exists
$stmt = $pdo->prepare('SELECT id,password FROM accounts WHERE username = ? OR email = ?');
$stmt->execute([ $_POST['username'],$_POST['email'] ]);
$account = $stmt->fetch(PDO::FETCH_ASSOC);
// Store the result so we can check if the account exists in the database.
if ($account) {
    // Username already exists
    echo 'Username and/or email exists!';
} else {
    // Username doesnt exists,insert new account
    $stmt = $pdo->prepare('INSERT INTO accounts (username,password,email,activation_code) VALUES (?,?,?)');
    // We do not want to expose passwords in our database,so hash the password and use password_verify when a user logs in.
    $password = password_hash($_POST['password'],PASSWORD_DEFAULT);
    $uniqid = account_activation ? uniqid() : '';
    $stmt->execute([ $_POST['username'],$password,$_POST['email'],$uniqid ]);
    if (account_activation) {
        // Account activation required,send the user the activation email with the "send_activation_email" function from the "main.php" file
        send_activation_email($_POST['email'],$uniqid);
        echo 'Please check your email to activate your account!';
    } else {
        echo 'You have successfully registered,you can now login!';
    }
}
?>

我已经坚持了好几天,将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...