Using PHP with IMAP to check POP3 "syntax error" - php

I am trying to connect to a pop3 email account that is hosted by my website host. My website is a shared host. The email host that I check my emails with windows live is mail.mysite.com with port 110 and it works fine.
Using php and imap I have not had any luck. I did check and my host does have IMAP installed. After that I fought with it for a few hours and learned when imap_open fails it tries 3 more times just for fun. As I started with imap_last_error() as my error checker it was telling me Cannot connect to Mail: Too many login failures Once I figured out what was happening it did not take long to figure out how to get the rest of the errors and then how to turn it off so it does not retry.
Now I am getting syntax error I have tried dozens and dozens different variations of hostname. Below I include 4 so people can see some of the saner things I have tried and the results.
As I am completely new to this I am likely missing something obvious to someone with more experience. I am also open to other ways to go about this. I just want to be able to use a php script to open and read Emails on a particular account. I am not picky about which or what kind of interface. I have been trying IMAP as it is the only one that I have found.
//$hostname = '{mail.mysite.com:110}INBOX'; //array(1) { [0]=> string(49) "[CLOSED] IMAP connection broken (server response)" }
//$hostname = '{mail.mysite.com:110/pop3}INBOX'; //array(1) { [0]=> string(142) "Certificate failure for mail.mysite.com: Server name does not match certificate: /OU=Domain Control Validated/CN=*.websitesource.net" }
$hostname = "{mail.mysite.com:110/pop3/novalidate-cert}INBOX"; //array(1) { [0]=> string(12) "syntax error" }
//$hostname = '{mail.websitesource.net:110/pop3}INBOX'; //array(1) { [0]=> string(12) "syntax error" }
//A temp account for testing
$user = 'a#mysite.com';
$password = '12345678!';
/* try to connect */
$inbox = imap_open($hostname,$username,$password,0,0) or die( var_dump(imap_errors ()) );
//see errors at top of code, current error 'syntax error'

You are using the wrong variable. Replace $username with $user
$inbox = imap_open($hostname,$user,$password,0,0) or die( var_dump(imap_errors ()) );

Ok I feel rather silly. $username is not the same as $user. When I started with various scripts I did not know about the Certificate problem(different hosts) and somewhere along the lines I no doubt copied part of one test script and part of another and wolla a simple bug that was hidden by the earlier bugs.

Related

cURL error [0]: Failed to connect to 127.0.0.1 port 8545: Connection refused

I am using laravel package(furqansiddiqui/erc20-php) for erc20 token transfer, But i am getting error when execute the code
$geth = new EthereumRPC('127.0.0.1', 8545);
$erc20 = new ERC20($geth);
$token = $erc20->token("0x05f4a42e251f2d52b8ed15e9fedaacfcef1fad27");
var_dump($token->name()); // string(7) "Zilliqa"
Make sure to run geth by --rpc parameter
geth --rpc
As easier way to connect to Ethereum network, you can use https://infura.io
Infura support mainnet and testnets (rinkeby, ropsten, kovan, goerli), thats good for development.

Debugging SMTP email failures - GSuite

I am working on an eCommerce site that sends a number of emails to the customer when they complete their order using G Suite SMTP relay service. But a large number of these emails are failing. There does not seems to be any pattern to it either - sometimes all emails will send, some times just one or two, and sometimes none.
I am getting the following error: 421, "4.7.0", Try again later, closing connection.
Looking here: https://support.google.com/a/answer/3726730?hl=en doesn't really help me debug this or figure out why some emails fail.
I am using the phpmailer class (https://sourceforge.net/projects/phpmailer/)
The issue seems to occur when the first handshake fails:
function Hello($host="") {
$this->error = null; # so no confusion is caused
if(!$this->connected()) {
$this->error = array(
"error" => "Called Hello() without being connected");
return false;
}
# if a hostname for the HELO was not specified determine
# a suitable one to send
if(empty($host)) {
# we need to determine some sort of appopiate default
# to send to the server
$host = "localhost";
}
// Send extended hello first (RFC 2821)
//If this fails then the second attempt will always fail
if(!$this->SendHello("EHLO", $host))
{
//when this fails it generates the try again later error
if(!$this->SendHello("HELO", $host))
return false;
}
return true;
}
So what is the best approach for debugging this?
The error message is pretty explicit. You call a 3rd party web service, which returns an error code which says the server you are calling is at capacity, try later. Is this a free service which allows you to upgrade to a paid plan? Usually whee you see his kind of thing.

Searching email body without downloading - IMAP

Can I search ALL emails over IMAP without downloading the message(s)?
As specified in 6.4.4 of RFC 3501 IMAP version 4 revision 1 (IMAP4rev1):
The SEARCH command searches the mailbox for messages that match
the given searching criteria. Searching criteria consist of one
or more search keys. The untagged SEARCH response from the server
contains a listing of message sequence numbers corresponding to
those messages that match the searching criteria.
The defined search keys are as follows. Refer to the Formal
Syntax section for the precise syntactic definitions of the
arguments
BODY
Messages that contain the specified string in the body of the
message.
...so I wonder if I can search inside the body of the email without downloading it first?
The function imap_search() could help you but have in mind that it needs to be supported by the IMAP server to work.
UPDATE:
This is the small program:
error_reporting(E_ALL);
ini_set('display_errors', '1');
$user = ''; // put your Gmail email address here (including '#gmail.com');
$pass = ''; // put your Gmail password here
$host = 'imap.gmail.com:993'; // Put your IMAP server here with portGmail
$keyword = ''; // put the word you want to find
$mailbox = sprintf('{%s/imap/ssl/user=%s}INBOX', $host, $user);
$query = sprintf('BODY "%s"', $keyword);
$mbox = imap_open($mailbox, $user, $pass, OP_READONLY);
if ($mbox) {
$list = imap_search($mbox, $query, SE_UID);
var_dump($list);
imap_close($mbox);
}
It may or may not work with your setup. It worked for me with one account on our company mail server. It failed to connect on another server that works fine with my regular email client.
It worked and failed on the same time (!!) with Gmail. Don't ask!

Services_Twitter Pear PHP code - OAuth direct messages suddenly stopped working

I have the following PHP code, which up to a week or so ago was working to send Twitter direct messages to a particular user. I now get "HTTP_OAuth_Exception: Unable to connect to tcp://api.twitter.com:80. Error #0: php_network_getaddresses: getaddrinfo failed: Name of service not known in /usr/share/pear/HTTP/OAuth/Consumer.php on line 257"
I've searched around and can't find that anything has changed with Twitter since it was working and the server configuration also hasn't changed. I tried using SSL in case Twitter suddenly required connection via SSL, but that gave basically the same error (except it said ssl and port 443).
I'm at a loss to see what's wrong and don't believe anything changed on my side. My code is based on the example in the Services_Twitter documentation (http://pear.php.net/package/Services_Twitter/docs/latest/Services_Twitter/Services_Twitter.html#var$oauth)
Any help would be greatly appreciated.
require_once 'Services/Twitter.php';
require_once 'HTTP/OAuth/Consumer.php';
$twitterto = 'xxxxxxxxxxxx';
$message='This is a test message';
$cons_key='xxxxxxxxxxxx';
$cons_sec='xxxxxxxxxxxx';
$auth_tok='xxxxxxxxxxxx';
$tok_sec='xxxxxxxxxxxx';
try {
$twitter = new Services_Twitter();
$oauth = new HTTP_OAuth_Consumer($cons_key,
$cons_sec,
$auth_tok,
$tok_sec);
$twitter->setOAuth($oauth);
// The error is raised on the following line.
$twitter->direct_messages->new($twitterto, $message);
$twitter->account->end_session();
} catch (Services_Twitter_Exception $e) {
echo $e->getMessage();
}

Help with gmail message reading with php

I need to read emails from gmail but i cant connect to gmail pop3 server.
Can anyone help me here ?
Here the code:
$pop3 = new POP3;
$pop3->server = 'pop.gmail.com';
$pop3->user = 'username';
$pop3->passwd = 'password';
$pop3->debug = true;
$pop3->pop3_connect()
The result:
Warning: fsockopen() [function.fsockopen]: unable to connect to pop.gmail.com:110 (Connection timed out) in /public_html/cron/pop3.php on line 64
Thanks
According to this page (connecting to Gmail using Outlook Express), you have to use port 995 for POP3 access to Gmail, and furthermore, SSL must be enabled.
Wikipedia also states this:
E-mail clients can encrypt POP3 traffic using Transport Layer Security (TLS) or Secure Sockets Layer (SSL). A TLS/SSL connection is negotiated using the STLS command. Some clients and servers, like Google Gmail, instead use the deprecated alternate-port method, which uses TCP port 995 (POP3S).
I'm not sure if it will help you, but GMAIL has an ATOM feed. I wrote a PHP script to download the Atom Feed, using CURL, so that I could check my email on my antiquated cell phone that only supported very simple HTML. So, depending on what you want to do, it might be easier to download and process the ATOM feed than it is to connect to the POP server.
I don't know what class you're using -- but for instance, using Daniel Lemos' package is shown below. The key is choosing the right port (995), and the right encryption method (TLS set to true for whatever pop3 package you are using). For example, you could use something like the below to initiate the connection. Not a big fan of how this class is architected, or the sample code (lot of nested if statements), but it does the job.
$pop3=new pop3_class();
$apop=0;
$pop3->authentication_mechanism="USER";
$pop3->debug=0;
$pop3->html_debug=1;
$pop3->join_continuation_header_lines=1;
$pop3->hostname = "pop.gmail.com";
$pop3->port = 995; // The port that gmail uses...
$pop3->tls = 1; // This is encryption
$user = "someuser";
$password = "some password";
if( !empty($error=$pop3->Open()) ){
die( "Something terrible happened..." );
}
$pop3->Login($user,$password,$apop);
I think there are two easy options to your email:
Cron atom feed like Kibbee says. But then you will have a little delay between when message was sent and when you fetch it.
Use http://smtp2web.com/ which will post your email to your website which means a lot shorter delay. Offcourse privacy should not be crucial, because your mail will pass through intermediate.

Categories