php 注册后发送验证邮件

Posted

技术标签:

【中文标题】php 注册后发送验证邮件【英文标题】:php Send validation email after registration 【发布时间】:2014-02-11 13:50:38 【问题描述】:

我是 php、PHPMyAdmin、Localhost 等的新手。我目前正在构建一个正在我的本地主机上测试的网站(我已经在我的 Ubuntu 上安装了 Xampp)。我想制作一个注册表,它可以正常工作(一切都进入我的数据库),但我想看看哪些用户实际上激活了他们的帐户。

所以我想制作一封带有唯一代码的验证邮件。现在的问题是我尝试输入:mail($to, $subject, $message, $headers),但我发现它不起作用。

我尝试安装 sendmail、postfix、dovecot、telnet 等,但似乎没有任何效果,而且我找不到好的演练。

我使用以下内容:ubuntu (12.04LTS)、xampp (php5.5)、localhost/phpmyadmin。

如果有人知道一个好的演练,或者知道如何完成这项工作,我将不胜感激!

【问题讨论】:

安装postfixhelp.ubuntu.com/community/Postfix的教程 在命令行中输入“sendmail sample-email@example.org”来尝试发送邮件功能。然后你的消息,如果 evrything 设置好,按 ctrl+D 发送,应该发送邮件到 sample-email@example.org 【参考方案1】:

这里有一个简短的教程,可能会有所帮助。

http://sourcelibrary.org/2011/08/29/how-to-set-up-the-php-mail-function-on-a-ubuntu-linux-lamp-server/

你是否用这一行更新了你的 php.ini 文件?

sendmail_path = "/usr/sbin/sendmail -t -i"

然后重启apache。

【讨论】:

我已经更新了我的 php.ini 邮件,我按照教程操作但没有收到邮件【参考方案2】:

您可以尝试使用 PHPMailer。它是一个强大的 PHP 发送电子邮件库。

https://github.com/Synchro/PHPMailer

在这里,我将为您举一个例子。 首先,为您的计算机下载 PHPMailer。 其次,在安装时需要从该位置获取 PHPMailerAutoload.php。在我的示例中,它位于 phpmailer 目录中。这个(phpmailer)与我的应用程序在同一目录中,所以:

<?php
require 'phpmailer/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer mail() test';
//Read an html message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.gif');

//send the message, check for errors
if (!$mail->send()) 
    echo "Mailer Error: " . $mail->ErrorInfo;
 else 
    echo "Message sent!";

?>

这个例子我取自 PHPMailer Github 的 https://github.com/PHPMailer/PHPMailer/blob/master/examples/mail.phps

您也可以使用您的 GMail 帐户(如果有的话)发送电子邮件 https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps

还有其他例子。

让我们知道您是如何修复它的。 祝你好运。

【讨论】:

以上是关于php 注册后发送验证邮件的主要内容,如果未能解决你的问题,请参考以下文章

laravel 8 : 注册后发送邮件验证 laravel

Java实现注册时发送激活邮件验证

如何使用腾讯企业邮箱发送会员注册验证邮件

验证电子邮件并注册用户后,如何在电子邮件上生成链接后获取令牌?

java案例----用户注册--发送邮件并激活/发送邮件验证码

在测试 php 以在 xampp 中发送电子邮件验证时遇到问题