QuestionEverything
QuestionEverything

Reputation: 5107

Sending email from localhost in php

Ok, I'm just overwhelmed by the number of ways and tools to send emails from localhost using php script.

Can you guys just help me clearing some things up?

These are my types of actions:

  1. I've enabled gmail smtp in php.ini, set port to 465.
  2. I've downloaded phpmailer, tested as per instruction without success.
  3. I've the remote server mails configuration(where my site was previously hosted).
  4. Also, I have a php script downloaded from the internet which is supposed to send email.

What exactly do I need to send emails from localhost using php script?

Upvotes: 1

Views: 2432

Answers (3)

bananos
bananos

Reputation: 11

You need get from dns mx hostnames for domain where you want send email ([email protected] -> domain boo.xx):

function getMX($hostname = "boo.xx", $show = 0){
    if(dns_get_mx($hostname, $mxhosts, $weights)) {
        $i = 0;
        $mxList = NULL;
        foreach($mxhosts as $key => $host) {
            if($show == 1) echo "Hostname: $host (Weight: {$weights[$key]}) <br>";
            $ip = gethostbyname($host);
            if($show == 1) echo "IP " . $ip . "\n<br>";
            if($show == 1) echo "IP " . gethostbyaddr($ip) . "\n<br>";
            $mxList[$i]['host'] = $host;
            $mxList[$i]['ip'] = $ip;
            $mxList[$i]['weight'] = $weights[$key];
            $i++;
        }
        return $mxList;
    } else {
        echo "Could not find any MX records for $hostname\n";
    }
}

Now you have list with mx hosts then you need:

Send email to port 25 to this host (always port 25 and without authentication) with phpmailer or socket client (example with ssl/tls support and authentication):

<?php
// Send with smtp ssl
// ini_set("SMTP","ssl://smtp.gmail.com");
// ini_set("smtp_port","465");

// Login email and password
$login = "[email protected]";
$pass = "123456";

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
stream_context_set_option($ctx, 'ssl', 'verify_peer_name', false);
try{
    // echo $socket = stream_socket_client('ssl://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    echo $socket = stream_socket_client('tcp://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    if (!$socket) {
        print "Failed to connect $err $errstr\n";
        return;
    }else{
        // Http
        // fwrite($socket, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
        // Smtp
        echo fread($socket,8192);
        echo fwrite($socket, "EHLO cool.xx\r\n");
        echo fread($socket,8192);

        // Start tls connection
        echo fwrite($socket, "STARTTLS\r\n");
        echo fread($socket,8192);

        echo stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);

        // Send ehlo
        echo fwrite($socket, "EHLO cool.xx\r\n");
        echo fread($socket,8192);

        // echo fwrite($socket, "MAIL FROM: <[email protected]>\r\n");
        // echo fread($socket,8192);

        echo fwrite($socket, "AUTH LOGIN\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, base64_encode($login)."\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, base64_encode($pass)."\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, "rcpt to: <[email protected]>\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, "DATA\n");
        echo fread($socket,8192);

        echo fwrite($socket, "Date: ".time()."\r\nTo: <[email protected]>\r\nFrom:<[email protected]\r\nSubject:Hello from php socket tls\r\n.\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, "quit \n");
        echo fread($socket,8192);

        /* Turn off encryption for the rest */
        // stream_socket_enable_crypto($fp, false);

        fclose($socket);
    }
}catch(Exception $e){
    echo $e;
}

ANd without authentication

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
stream_context_set_option($ctx, 'ssl', 'verify_peer_name', false);
try{
    // echo $socket = stream_socket_client('ssl://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    echo $socket = stream_socket_client('tcp://mxhost.boo.xx:25', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    if (!$socket) {
        print "Failed to connect $err $errstr\n";
        return;
    }else{
        // Http
        // fwrite($socket, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
        // Smtp
        echo fread($socket,8192);
        echo fwrite($socket, "EHLO cool.xx\r\n");
        echo fread($socket,8192);

        // Start tls connection
        echo fwrite($socket, "STARTTLS\r\n");
        echo fread($socket,8192);

        echo stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);

        // Send ehlo
        echo fwrite($socket, "EHLO cool.xx\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, "MAIL FROM: <[email protected]>\r\n");
        echo fread($socket,8192);

        //echo fwrite($socket, "AUTH LOGIN\r\n");
        //echo fread($socket,8192);

        //echo fwrite($socket, base64_encode($login)."\r\n");
        //echo fread($socket,8192);

        //echo fwrite($socket, base64_encode($pass)."\r\n");
        //echo fread($socket,8192);

        echo fwrite($socket, "rcpt to: <[email protected]>\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, "DATA\n");
        echo fread($socket,8192);

        echo fwrite($socket, "Date: ".time()."\r\nTo: <[email protected]>\r\nFrom:<[email protected]\r\nSubject:Hello from php socket tls\r\n.\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, "quit \n");
        echo fread($socket,8192);

        /* Turn off encryption for the rest */
        // stream_socket_enable_crypto($fp, false);

        fclose($socket);
    }
}catch(Exception $e){
    echo $e;
}

Upvotes: 0

jhon doe learning
jhon doe learning

Reputation: 21

as i can see, you use gmail as a smtp then you need configure the php mailer script ... that will good if you edit your answer and put the code there.

after that you need to check permission, file permission/server permission/gmail permission

Upvotes: 0

hohner
hohner

Reputation: 11588

You need a local SMTP server such as this one. Or, to make your life infinitely times easier, download WAMP which includes everything you need for local PHP projects.

Upvotes: 1

Related Questions