phpemail

SMTP Authentication with PHP mail() function


I'm stuck on trying to add SMTP Authentication to my php script using the PHP mail() function.

The script currently works but because it isn't using SMTP Authentication the path of the php file and many other sensitive details are being included in the header (account username, etc.).

I'm currently specifying some of the header information using "$headers = ", etc., but I understand I need to use SMTP Authentication to fix this.

Is there a simple way to make my script use SMTP Authentication without having to use phpmailer, etc? Can I simply specify the port, authentication, username, password?

Update: Here is come code:

            `code`$eol = PHP_EOL;
            $headers =  "From: Test <test@test.com>".$eol;
            $headers .= "Reply-To: test@test.com".$eol;
            $headers .= "MIME-Version: 1.0".$eol;
            $headers .= "Content-Type: multipart/mixed; boundary=\"$random_hash\"".$eol.$eol;
            $subject = 'Subject Goes Here';
            $message="--".$random_hash.$eol;
            $message.="Content-Type: text/plain; charset=UTF-8".$eol;
            $message.="Content-Transfer-Encoding: 8bit".$eol.$eol;
            $message.="Hello,".$eol;
            $message.="Body content goes here.".$eol.$eol;
            $message.="Thank you,".$eol.$eol;
            $message.="--".$random_hash.$eol;
            @mail(to, subject, message, headers);`code`

Solution

  • Why don't you try the Pear Mail interface something like this:

    require_once "Mail.php";
    $username = 'user@gmail.com';
    $password = 'password';
    $smtpHost = 'ssl://smtp.gmail.com';
    $smtpPort = '465';
    $to = 'mail@to.com';
    $from =  'user@gmail.com';
    
    $subject = 'Contact Form';
    $successMessage = 'Message successfully sent!';
    
    
    $replyTo = '';
    $name = '';
    $body = '';
    
    
    $headers = array(
        'From' => $name . " <" . $from . ">",
        'To' => $to,
        'Subject' => $subject
    );
    $smtp = Mail::factory('smtp', array(
                'host' => $smtpHost,
                'port' => $smtpPort,
                'auth' => true,
                'username' => $username,
                'password' => $password
            ));
    
    $mail = $smtp->send($to, $headers, $body);
    
    if (PEAR::isError($mail)) {
        echo($mail->getMessage());
    } else {
        echo($successMessage);
    }
    

    More information at https://pear.php.net/manual/en/package.mail.mail.php

    EDIT:

    The only way to this without more coding or using external library, is to update sendmail:

    Define SMTP Server

    smtp_server=mail.mydomain.com
    

    If you need to change the smtp and SSL ports ; smtp port (normally 25)

    smtp_port=25
    
    ; SMTPS (SSL) support
    ;   auto = use SSL for port 465, otherwise try to use TLS
    ;   ssl  = alway use SSL
    ;   tls  = always use TLS
    ;   none = never try to use SSL
    
    smtp_ssl=auto
    

    And finally your authentication credentials for SMTP server:

    auth_username=username
    auth_password=password
    

    Ref: http://php.net/manual/en/ref.mail.php