繁体   English   中英

在php中使用发送确认电子邮件

[英]sending confirmation email using in php

我有一个用于发送确认电子邮件的php代码。 但是如何使用我的邮件服务器将此电子邮件发送给注册用户。 使用gmail发送确认电子邮件的示例。

<?php   
    if(isset($_SESSION['error'])) {
         header("Location: index.php");
         exit; 
        } else { 
           $username = $_POST['username']; 
           $email = $_POST['email']; 
           $password = $_POST['password']; 
           $com_code = md5(uniqid(rand()));

        $sql2 = "INSERT INTO user (username, email, password, com_code) VALUES ('$username', '$email', '$password', '$com_code')"; $result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error());

        if($result2) { 
            $to = $email; 
            $subject = "Confirmation from  MyName to $username"; 
            $header = "TutsforWeb: Confirmation from TutsforWeb"; 
            $message = "Please click the link below to verify and activate your account. rn"; $message .= "http://www.yourname.com/confirm.php?passkey=$com_code";

        $sentmail = mail($to,$subject,$message,$header);

        if($sentmail) { 
           echo "Your Confirmation link Has Been Sent To Your Email Address."; 
        } else { 
          echo "Cannot send Confirmation link to your e-mail address"; 
        } 
     } 
  } 
 } 
?>

如果您具有邮件服务器凭据,则可以使用SMTP发送电子邮件。 您还可以使用非常易于使用的PHPMailer

首先,您需要使用以下代码从上述链接安装PHPMailer。

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;


$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 = 'user@gmail.com';                 // Your gmail username
$mail->Password = 'your_gmail_password';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = 'from@example.com'; // from email address
$mail->FromName = 'User1'; // whatever is the name of sender
$mail->addAddress($_POST['email'], $_POST['username']);     // Add a recipient

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = $subject;
$mail->Body    =  $message ;

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>`

如果您有邮件服务器,则遵循邮件服务器规则。

您可以使用PHPMailer并遵循phpmailer函数的规则。

修复:您必须确定您是否在服务器实例中安装了邮件服务器。 这取决于服务器环境。 您可以通过简单的网络搜索找到方法。 提示:如果仅从正常操作中获得响应,则您的邮件服务器未连接。 但是,如果持续等待大约4到5秒钟,则意味着服务器在大多数时间都存在问题。 Ubuntu-[如何安装后缀] [1] Windows-[SMTP电子邮件] [2]

进一步的问题:一旦您可以使用php邮件功能发送邮件,但仍然提供了准确的标头信息,否则邮件将发送到垃圾邮件文件夹。

错误:$ header =“ TutsforWeb:来自TutsforWeb的确认”; 正确:
$headers = 'From: webmaster@example.com' . "\\r\\n" . 'Reply-To: webmaster@example.com' . "\\r\\n" . 'X-Mailer: PHP/' . phpversion();

如果您要使用gmail,只需参考以下内容: 从PHP页面使用GMail SMTP服务器发送电子邮件

您首先要从存储注册用户的表中获取数据,然后可以向注册用户发送邮件

Download PHPMailerAutoload 

链接到这里

<?php

// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0

require("lib/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();

// set mailer to use SMTP
$mail->IsSMTP();

// we are setting the HOST to localhost
$mail->Host = "mail.example.com";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication

// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
$mail->Username = "user@gmail.com";  // SMTP username
$mail->Password = "password"; // SMTP password

$mail->From = "from@example.com";

// below we want to set the email address we will be sending our email to.
$mail->AddAddress("to@example.com", "To whom");

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);

$mail->Subject = "You have received feedback from your website!";
$message = "Text Message";
$mail->Body    = $message;
$mail->AltBody = $message;

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM