Access emails using imap - php

I am using Apache on Ubuntu 14.02 and running php on the same.I have connected to a remote mssql server hence no need of mysql .
I want to know how to connect to an email id registered with gmail in order to read the incoming emails .
I have tried the below , but it shows a blank screen while running it from my localhost :
<?php
error_reporting('E_ALL');
$mailboxPath = "{imap.gmail.com:993/imap/ssl}INBOX";
$username = "my_email#gmail.com";
$password = "mypassword";
$imap = imap_open($mailboxPath, $username, $password);
print_r($imap);
?>
I am inserting my actual gmail username and password while running the program . I want to know if i am missing something .
Many Thanks

Here is the solution:
Login to your gmail account, enable imap.
Let the access here first: https://www.google.com/settings/security/lesssecureapps
Go to: https://accounts.google.com/b/0/DisplayUnlockCaptcha
and enable access.
That's it.
I had the exact same problem when I try to get emails with imap on php.
I was running the exact same script that I know it works with my other gmail.
The solution is above and the error message was:
ERROR: Can not authenticate to IMAP server: [ALERT] Please log in via your web browser

Have you activated the imap.so extension in your php.ini file ? extension=imap.so it's a basical cause of issues on localhost server

Try this (you need to turn on your Outbound relay):
Log into your account at google.com/a/yourdomain.com
Click the Settings tab and then select Email in the left column.
In the Outbound relay section, select Allow users to send mail through an external SMTP when configuring a "from" address hosted outside your domain.
Click Save changes.

Related

imap php not working

I am trying to run imap_open() in on my server but its giving me this error.
Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX in /home/bestinsaudi2/public_html/mailbox.php on line 55
Cannot connect to Gmail: Can not authenticate to IMAP server: [ALERT] Please log in via your web browser: https://support.google.com/mail/acco
I have enabled imap in gmail account i am using .I also enabled less secure apps. I copied code from david walsh . His credentials is giving this error.
Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX in /home/bestinsaudi2/public_html/mailbox.php on line 55
Cannot connect to Gmail: Can not authenticate to IMAP server: [AUTHENTICATIONFAILED] Invalid credentials (Failure)
I am writing down the code i am using with my own credentials.
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'myemail#gmail.com';
$password = 'mypassword';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
here is one more thing i am not using wamp or xamp server i am using ipower. Somewhere i found to run command like $ composer require secit-pl/imap-bundle
And its confusing .How to and where to run this and is it necessary with my code? Also do i have to download any library for this ? or should some additional step?
If less secure apps are enabled, and the app password is not working or not desired, it might be that google has found the IP address that your imap is using to be suspicious and blocked it.
Log into that account from a browser and check for an email from that has the subject "Critical security alert". The date on the email should be around the same time that imap stopped working. If you find such an email, it should spell out that a sign-in attempt was blocked, and have a big button that says "Check activity". When you click the button, it will ask if the sign in that it blocked was actually from you. Indicate that it was, and you should be able to sign in with imap again from that IP.
If you or someone else has already indicated that it was or was not them, there is an option to change your answer.
When google decides to do this, it only sends a single email. So if it's been a few days, that email might be buried in the inbox. It does not get resent for each blocked sign in from that IP.
You are probably looking for gmail setup for something called "Less Secure Apps":
https://support.google.com/accounts/answer/6010255?hl=en
that's why it requires you to log in with a browser.
After you set this up correctly, and enable less secure apps, this should work nicely, just checked with my own account.
EDIT: is safe apps is alredy enabled, you may need to create App Password:
https://support.google.com/mail/answer/185833?hl=en
and change your credentials to:
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'email#gmail.com';
$password = 'app password here, not your gmail password';
EDIT2: App Password functionality require to have 2-Step verification enabled

Read email with IMAP and PHP - Connection Refused

I want to read a mail's inbox using PHP IMAP.
On phpinfo() i see both OpenSSL and IMAP enabled.
When trying to connect:
/* connect to inbox */
$hostname = "{imap-mail.outlook.com:993/imap/tls}INBOX";
$username = 'mail#...';
$password = 'password';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Error: ' . imap_last_error());
It will always throw an error:
Can't connect to imap.glbdns2.microsoft.com,993: Connection refused (errflg=2)
On the outlook settings it says:
Why does this happen and how can I fix it?
I had almost the same problem with my email server. It was not accepting request from other third party servers which was using it. After sometimes, it came down to the increased banning rules I had set for the server. The fail2ban was now set to permanently ban offending IPs which tried to unsuccessfully login for more than three times. I was lucky since I managed this email server myself so I un-blacklisted the IP and it works fine. Firewall problem is the most likely cause of this. Try the following:
check your IP if it is blacklisted by any blacklist houses (the requesting server IP);
a site like https://mxtoolbox.com/ can help;
if you have another server (like VPS container), try running the code from it;
if all fails, you may have to contact the: imap-mail.outlook.com people for help.

Open shared exchange email account with imap_open() PHP

In exchange environment I have PHP application setup to open and IMAP stream and parse emails. I have no problem connecting to my personal email account with the following:
imap_open("{exchange.server/ssl}INBOX", "username", "password");
Now I have been given access to a group email account and I'm wondering if its possible to specify other accounts with PHP's IMAP functions. I know when I setup outlook with a new profile I add the group email then my credentials with no problems.
I've also see telnet commands that support this in How can I access a shared Exchange mailbox with IMAP (over telnet)?
TELNET: ? LOGIN domain/username/mailboxname password
Using this logic I have tried:
imap_open("{exchange.server/ssl}INBOX", "username/sharedmailbox", "$password");
This gives an error I've also tried to add the domain with no luck.
Turns out I was very close with what I posted in the question however I was including #example.com for sharedmailbox#example.com in the imap_open(). Once I removed that I had no problem connecting with the following.
imap_open("{exchange.server/ssl}INBOX", "domain/username/sharedmailbox", "$password");

fetching gmail inbox with imap

i got a code from here to download gmail inbox:
http://davidwalsh.name/gmail-php-imap
use these 2 hostnames
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox
but getting this error.
Warning: imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox in /home/medicalh/public_html/testmail.php on line 9
Cannot connect to Gmail: Can't connect to gmail-imap.l.google.com,993: Connection timed out
i have searched on stackoverflow but did't got solution
thanks
change you host name to this one and try
$hostname= '{imap.gmail.com:993/ssl/novalidate-cert}';
and double check the username and password
Open the php.ini file and remove ; before ;extension=php_imap.dll
and restart your server.
Your code will work...
You can download my Gmail class at my website: . I have also included a test file for you to test the class file.
If it does not work, I believe it could be your server's problem. You should contact your system administrator.
One of the issues with gmail IMAP SSL authentication is related to Google's account security.Once you get the login error once, sign out of all your google accounts. Then, visit this link:
https://accounts.google.com/DisplayUnlockCaptcha
Log in with the account you're attempting to access via imap.
Follow the steps and you'll then be able to login in to gmail with php imap.
It's visually shown here:
http://jeffreifman.com/filtered-open-source-imap-mail-filtering-software-for-php/configuring-gmail/
source: http://php.net/manual/en/function.imap-open.php

PHP Swift mailer: Failed to authenticate on SMTP using 2 possible authenticators

When I send an email with the PHP Swift mailer to this server: smtp.exchange.example.com like this:
// Load transport
$this->transport =
Swift_SmtpTransport::newInstance(
self::$config->hostname,
self::$config->port
)
->setUsername(self::$config->username)
->setPassword(self::$config->password)
;
// Load mailer
$this->mailer = Swift_Mailer::newInstance($this->transport);
// Initialize message
$this->message = Swift_Message::newInstance();
// From
$this->message->setFrom(self::$config->from);
// Set message etc. ...
// Send
$this->mailer->send($this->message);
I get a strange error back:
Failed to authenticate on SMTP server with username "user#example.com" using 2 possible authenticators
I know for sure that the login-info is correct.
This might be old but somebody might get help through this. I too faced the same problem and received a mail on my gmail account stating that someone is trying to hack your account through an email client or a different site.
THen I searched and found that doing below would resolve this issue.
Go to https://accounts.google.com/UnlockCaptcha‎
and unlock your account for access through other media/sites.
UPDATE : 2015
Also, you can try this, Go to https://myaccount.google.com/security#connectedapps
At the bottom, towards right there is an option "Allow less secure apps". If it is "OFF", turn it on by sliding the button.
UPDATE : 2021
The google URL has now changed to : https://myaccount.google.com/lesssecureapps. You will be asked to relogin
I had the same issue, so I've disabled one setting on my WHM root login, which is as follows :
WHM > Home > Server Configuration > Tweak Settings > Restrict outgoing SMTP to root, exim, and mailman (FKA SMTP Tweak) [?]
I really have the same problem, finally, i solved it.
its likey not the Swift Mail's problem. It's Yaml parser's problem.
if your password only the digits, the password senmd to swift finally not the same one.
swiftmailer:
transport: smtp
encryption: ssl
auth_mode: login
host: smtp.gmail.com
username: your_username
password: 61548921
you need fix it with double quotes
password: "61548921"
I just received the same error.
The strange thing is that commented the credential lines
// ->setUsername(self::$config->username)
// ->setPassword(self::$config->password)
And the problem disappeared.
I'm puzzled because the mail server definitely needs those credentials.
I tried almost all the possible suggestions mention here but for me problem got solved after changing "Access for less secure apps" to ENABLE in my Google account security settings tab. Hope this might useful for others !
The server might require some kind of encryption and secure authentication.
see http://swiftmailer.org/docs/sending.html#encrypted-smtp
try putting username in double quote "username", somehow that fixed for me.
I faced the same problem and solved it. Try to log in manually to your gmail account first.
This issue is due to google has detected your gmail account has been abusing. E.g send a lot of emails over a short period of time.
You perhaps use the wrong username.
I had a similar error. Ensure you're not using uppercase while logging into server.
Example: JacekPL#lala.pl
If you use ->setUsername('JacekPL'), this can cause an error. Use ->setUsername('jacekpl') instead. This solved my problem.
First check if there is any connectivity problem and you can reach the SMTP server:
In terminal type:
telnet servername portnumber
If you receive the 220 response you can reach the SMTP server and there is no connectivity problem but if the connection to the server failed see what's wrong in your network.
If the server needs auth try to authenticate on the server by username and password and see if something goes wrong.
At last see if the server requires encryption and if yes openssl and other stuff are configured correctly.
I got the same same error.
I was using gmail account and Google's SMTP server to send emails. The problem was due to SMTP server refusing to authorize as it considered my web host (through whom I sent email) as an intruder.
I followed Google's process to identify my web host as an valid entity to send email through my account and problem was solved.
If you are trying to send mail from your local enviroment eg. XAMPP or WAMP, this error will occur everytime, go ahead and try the same code on your web hosting or whatever you are using for production.
Also, 2 step authentication from google may be the issue.
Strange enough sending emails works again. We did not change anything and the host say they did not either. We think a server restarts or so. It is strange :S

Categories