问题描述
我使用registerNewUser的方法创建了一个PHP Mail类。基本上,因为我的标题说方法返回true,当它应该返回false,因为我没有收到一封电子邮件。有人可以解释如果我做错了什么? (请不要评论我发送用户一个未加密的密码这个未加密的密码是一个25字符的字母数字随机生成的密码安全不是一个问题)我不想使用某种类型的框架。我想手动编码,所以请不要告诉我使用PHPMailer或任何类别。
编辑:我想知道我需要设置XAMPP以使用SMTP发送传出电子邮件。这个问题不是重复,因为每一个其他问题的每个其他答案是基于Windows的,甚至不提供如何在XAMPP本地主机服务器上设置SMTP的一步一步的指示。
以下是代码:
<?php
class Mail {
private $ headers;
public function __construct(){
//设置邮件头
$ this-> headers =MIME-Version:1.0 \r\\\
;
$ this-> headers。=Content-type:text / html; charset = iso-8859-1 \r\\\
;
$ this-> headers。=From:Admin @ NAStepsOnline< [email protected]> \r\\\
。X-Mailer:php;
}
/ **
* @desc将用户从注册电子邮件中邮寄。
*此函数只能通过user.class文件中的
* registerNew()调用
* @param str $ userName用户的名字
* @param str $ userEmail用户的电子邮件地址
* @param str $ userPass用户的密码(UNENCRYPTED)
* @return bool True =邮件已成功发送
* False =邮件未发送
* /
public function registerNewUser($ userName,$ userEmail,$ userPass){
//定义主题行
$ subject =Thanks。 $ userName。 注册NAStepsOnline.com;
//为用户设置消息
$ msg =< html>< body>;
$ msg。= $ userName。 感谢您在NAStepsOnline.com注册< br>< br>;
$ msg。=这里是您的密码(区分大小写):。 $ userPass。 < br>< br>;
$ msg。=请使用登录表单登录。< br>;
$ msg。=要为您的帐户分配赞助商,请访问个人资料设置页面,然后点击我的赞助商< br>;
$ msg。=如果您有任何问题,请使用联系我们页面与我们联系。< br>< br>;
$ msg。=谢谢,< br> The Team @ NAStepsOnline.com;
$ msg。=< / body>< / html>;
//为用户注册
$ mail = mail($ userEmail,$ subject,$ msg,$ this-> headers);
if($ mail){
return true;
} else {
return false;
}
}
}
首先安装 sendmail
sudo apt-get install sendmail
并将其更改如下:
SMPT = smtp.gmail.com
smtp_port = 587
[email protected]
sendmail_path = / usr / sbin / sendmail -t -i
-
Windows
现在编辑 sendmail.ini :
[sendmail]
smtp_server = smtp.gmail.com
smtp_port = 587
error_logfile = error.log
debug_logfile = debug.log
[email protected]
auth_password = gmail-password
[email protected] $ b $
一旦您安装了 code> sendmail ,您可以运行以下命令: sudo sendmailconfig 。对所有问题回答 [Y] 。
创建新目录:
sudo mkdir -m 700 / etc / mail / authinfo&& cd / etc / mail / authinfo
创建新文件:
sudo touch ./gmail-auth
插入以下内容:
AuthInfo:U:YOUR ACCOUNT NAMEI:YOUR GMAIL EMAIL ADDRESSP:YOUR GMAIL PASSWORD
创建新的哈希映射:
makemap hash gmail-auth< gmail-auth
打开 /etc/mail/sendmail.mc 及以上的第一个 定义add:
define(`SMART_HOST' smtp.gmail.com]'dnl
define(`RELAY_MAILER_ARGS',`TCP $ h 587')dnl
define(`ESMTP_MAILER_ARGS',`TCP $ h 587')dnl
(`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
重建配置并重新启动sendmail服务
sudo make -C / etc / mail& & sudo服务sendmail重新启动
-
Homebrew
使用安装 sendmail brew install sendmail
为了获得安装dir,执行以下命令:
brew info sendmail 。从此答案的 Linux 部分执行相同的配置步骤。
I've created a PHP Mail class with the method of registerNewUser. Basically as my title says the method is returning true, when it should be returning false because i'm not receiving an email. Can someone explain if i'm doing something wrong? (Please do not comment on me sending the user an unencrypted password. This unencrypted password is a 25 character alpha numeric randomly generated password. Security is not an issue here) I do not wish to use some type of framework. I want to code this by hand, so please don't tell me to use PHPMailer or anything of that sort.
Edit: I'm figuring out that I'm needing to set up XAMPP to send outgoing Emails using SMTP. This question is not a duplicate as every other answer on every other question is windows based, and even then doesn't provide a step by step instruction on how to set up SMTP on a XAMPP localhost server.
Here's the code:
<?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 <[email protected]>\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; } } }
First install a sendmail
sudo apt-get install sendmail
In the php.ini file find [mail function] and change it as follows:
SMPT=smtp.gmail.com smtp_port=587 [email protected] 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 [email protected] auth_password=gmail-password [email protected]
Linux
Once you have installed sendmail you can run following command: sudo sendmailconfig. Answer [Y] to all questions.
Make new directory:
sudo mkdir -m 700 /etc/mail/authinfo && cd /etc/mail/authinfo
Create new file:
sudo touch ./gmail-auth
Insert following content:
AuthInfo: "U:YOUR ACCOUNT NAME" "I:YOUR GMAIL EMAIL ADDRESS" "P:YOUR GMAIL PASSWORD"
Create new hash map:
makemap hash gmail-auth < gmail-auth
Open /etc/mail/sendmail.mc and above first MAILER definition add:
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
Rebuild configuration and restart sendmail service
sudo make -C /etc/mail && sudo service sendmail restart
Homebrew
Install sendmail with brew install sendmailIn order to get installation dir execute following command: brew info sendmail. Do the same configuration steps from Linux part of this answer.
这篇关于如何在UNIX计算机上为SMTP发送电子邮件设置XAMPP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!