我用registerNewUser的方法创建了一个PHP Mail类。基本上,正如我的标题所说,该方法返回true,而它应该返回false,因为我没有收到电子邮件。有人能解释一下我做错了什么吗?(请不要评论我向用户发送未加密密码。此未加密密码是一个25个字符的字母数字随机生成的密码。安全性不是问题(我不希望使用某种类型的框架。我想手动编码,所以请不要告诉我使用PHPMailer或任何类似的东西。
编辑:我发现我需要设置XAMPP来使用SMTP发送传出电子邮件。这个问题不是重复的,因为每个其他问题的其他答案都是基于窗口的,即使这样也没有提供关于如何在XAMPP localhost服务器上设置SMTP的分步说明。
这是代码:
<?php
class Mail {
private $headers;
public function __construct() {
// Setting Up Mail Headers
$this->headers = "MIME-Version: 1.0 'r'n";
$this->headers .= "Content-type: text/html; charset=iso-8859-1 'r'n";
$this->headers .= "From: Admin @ NAStepsOnline <no-reply@NAStepsOnline.com>'r'n"."X-Mailer: php";
}
/**
* @desc Mails the User from registration email.
* This function should only be called through
* registerNew() within the user.class file
* @param str $userName First Name of the user
* @param str $userEmail Email address of the user
* @param str $userPass Password of user (UNENCRYPTED)
* @return bool True = Mail Sent Sucessfully
* False = Mail Not Sent
*/
public function registerNewUser($userName, $userEmail, $userPass) {
// Define Subject Line
$subject = "Thanks " . $userName . " for Registering On NAStepsOnline.com";
// Setting Up Message to the User
$msg = "<html><body>";
$msg .= $userName . " Thanks for registering at NAStepsOnline.com<br><br>";
$msg .= "Here is your password (case sensitive): " . $userPass . "<br><br>";
$msg .= "Please use the login form to login.<br>";
$msg .= "To assign a sponsor to your account please visit the Profile Settings page and click on My Sponsor.<br>";
$msg .= "If you have any problems please contact us using the Contact Us page.<br><br>";
$msg .= "Thanks,<br>The Team @ NAStepsOnline.com";
$msg .= "</body></html>";
// Mailing the user Registration
$mail = mail($userEmail, $subject, $msg, $this->headers);
if($mail) {
return true;
} else {
return false;
}
}
}
首先安装sendmail
sudo apt-get install sendmail
在php.ini
文件中找到[mail function]
并将其更改如下:
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from=your@gmail.com
sendmail_path=/usr/sbin/sendmail -t -i
Windows
Now edit the `sendmail.ini`: [sendmail] smtp_server=smtp.gmail.com smtp_port=587 error_logfile=error.log debug_logfile=debug.log auth_username=username@gmail.com auth_password=gmail-password force_sender=username@gmail.com
Linux
一旦安装了
sendmail
,就可以运行以下命令:sudo sendmailconfig
。回答所有问题的[Y]
。创建新目录:
sudo mkdir -m 700 /etc/mail/authinfo && cd /etc/mail/authinfo
创建新文件:
sudo touch ./gmail-auth
插入以下内容:
AuthInfo: "U:YOUR ACCOUNT NAME" "I:YOUR GMAIL EMAIL ADDRESS" "P:YOUR GMAIL PASSWORD"
创建新的哈希图:
makemap hash gmail-auth < gmail-auth
打开
/etc/mail/sendmail.mc
及以上版本,首先MAILER定义添加:define(`SMART_HOST',`[smtp.gmail.com]')dnl define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl define(`confAUTH_OPTIONS', `A p')dnl TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl FEATURE(`authinfo',`hash -o /etc/mail/authinfo/gmail-auth')dnl
重建配置并重新启动发送邮件服务
sudo make -C /etc/mail && sudo service sendmail restart
macOS
用
brew install sendmail
安装自制软件和sendmail
要获得安装目录,请执行以下命令:brew info sendmail
。执行本答案Linux部分中相同的配置步骤。