J. Robinson J. Robinson - 3 months ago 11
PHP Question

How can I Setup XAMPP for SMTP outgoing Email on a UNIX machine

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

Thanks in advance for your help.

Heres 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;
}
}
}

Answer

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
    
Comments