1

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 <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;
    }
  }
}
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
J. Robinson
  • 760
  • 1
  • 12
  • 41
  • did you check in spam ? and are you on local or live server. – shubham715 Aug 09 '16 at 04:35
  • Localhost XAMPP server running on a Unix platform, checked the spam folder. Nothing. – J. Robinson Aug 09 '16 at 04:36
  • its not working on local server . for that you need to use smtp . you can use phpmailer to send mail from local machine. – shubham715 Aug 09 '16 at 04:37
  • As stated in my above question i dont wish to add a mailing framework to my site. So PHPMailer is out of the question. – J. Robinson Aug 09 '16 at 04:38
  • then its not working from localhost without smtp and also on live server mail will be in spam . and phpmailer is too easy you can check tutorials http://thecoderain.blogspot.in/2016/05/send-mail-using-phpmailer-from-localhost-and-live-server.html – shubham715 Aug 09 '16 at 04:39
  • I do not, again, wish to use the PHPMailer framework. I'll take the SMTP function into advisement. But i dont understand why the mail isn't sending from localhost, when i was just an hour earlier with NO changes in the mail.class file. – J. Robinson Aug 09 '16 at 04:42
  • @J.Robinson you didn't state anywhere what mail server you are actually trying to use. Or do you expect this to work without a mail server installed? – eis Aug 09 '16 at 06:16

1 Answers1

4

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
    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

    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 sendmail In order to get installation dir execute following command: brew info sendmail. Do the same configuration steps from Linux part of this answer.

nix9
  • 578
  • 2
  • 5
  • 20
  • i can't find the sendmail.ini file. where is it located on the unix platform? – J. Robinson Aug 09 '16 at 06:16
  • Try just running following command: `sudo sendmailconfig`. The ini file can be found only on Windows. – nix9 Aug 09 '16 at 06:28
  • Can you re-edit your original answer to reflect the changes for the Unix OS? I'm confused because the mail function within php.ini alot of things are commented saying for Win32 only. :D Please and thank you! – J. Robinson Aug 09 '16 at 06:35
  • @J.Robinson Updated as you wish and added additional configuration content. – nix9 Aug 09 '16 at 08:21
  • Unfortunately @nix9 I am unable to use the sudo apt-get command. I installed homebrew and used the brew install sendemail package instead. I hate to ask again but is there any way to update your answer to reflect homebrew and not apt-get? Here is my new question if you want to post there. http://stackoverflow.com/questions/38855758/how-do-i-set-up-xampp-server-using-homebrews-sendemail-package-php-ini-and-co – J. Robinson Aug 09 '16 at 16:31