在php中发送确认电子邮件

时间:2014-12-18 05:30:55

标签: php mysql email

我有一个发送确认电子邮件的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"; 
        } 
     } 
  } 
 } 
?>

5 个答案:

答案 0 :(得分:0)

如果您拥有邮件服务器凭据,则可以使用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';
}
?>`

答案 1 :(得分:0)

如果您有邮件服务器,则按照邮件服务器规则进行操作。

您可以使用 PHPMailer 并遵循phpmailer功能的规则。

答案 2 :(得分:0)

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

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

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

如果你想这样做,请参阅:Send email using the GMail SMTP server from a PHP page

答案 3 :(得分:0)

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

答案 4 :(得分:0)

Download PHPMailerAutoload 

link here

<?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";
?>