在php中发送确认电子邮件

时间:2022-03-27 07:26:54

I have a php code for sending confirmation email. But how to send this email to registered user using my mail server. Example using gmail to send confirmarion 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 个解决方案

#1


0  

If you have mail server then you follow the your mail server rules.

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

You can use PHPMailer and follow the rule of phpmailer function.

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

#2


0  

Fix : You have to find out whether you have installed a mail server in your server instance. This depends on server environment. You can find how to with simple web search. Tip: If just get the response from the operation as normal your mail server is not connected. But if it's keep waiting for like 4 to 5 seconds means most of the time server is there issue is something in it. Ubuntu - [How to install postfix][1] Windows - [SMTP E-mail][2]

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

Further issues : Once you can send the mail using php mail function but still you have give the accurate header information otherwise the mail will send to spam folder.

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

Wrong: $header = "TutsforWeb: Confirmation from TutsforWeb"; Correct:
$headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

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

If you want to do it gmail just refer this : Send email using the GMail SMTP server from a PHP page

如果你想这样做,请参考以下内容:从PHP页面使用GMail SMTP服务器发送电子邮件

#3


0  

You first fetch data from table where registered users are stored then you can send mail to registered users

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

#4


0  

If you have your mail servers credentials then you can use SMTP to send emails. You can also use PHPMailer which is very easy to use.

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

First thing is you need to install PHPMailer from above link after that use following code `

首先,您需要在使用以下代码之后从上面的链接安装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';
}
?>`

#5


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

#1


0  

If you have mail server then you follow the your mail server rules.

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

You can use PHPMailer and follow the rule of phpmailer function.

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

#2


0  

Fix : You have to find out whether you have installed a mail server in your server instance. This depends on server environment. You can find how to with simple web search. Tip: If just get the response from the operation as normal your mail server is not connected. But if it's keep waiting for like 4 to 5 seconds means most of the time server is there issue is something in it. Ubuntu - [How to install postfix][1] Windows - [SMTP E-mail][2]

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

Further issues : Once you can send the mail using php mail function but still you have give the accurate header information otherwise the mail will send to spam folder.

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

Wrong: $header = "TutsforWeb: Confirmation from TutsforWeb"; Correct:
$headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

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

If you want to do it gmail just refer this : Send email using the GMail SMTP server from a PHP page

如果你想这样做,请参考以下内容:从PHP页面使用GMail SMTP服务器发送电子邮件

#3


0  

You first fetch data from table where registered users are stored then you can send mail to registered users

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

#4


0  

If you have your mail servers credentials then you can use SMTP to send emails. You can also use PHPMailer which is very easy to use.

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

First thing is you need to install PHPMailer from above link after that use following code `

首先,您需要在使用以下代码之后从上面的链接安装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';
}
?>`

#5


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