gmail not responding while connecting through imap? - php

I wrote a simple 3 line code for connecting to gmail through imap-php.
i am using 000webhost for running it.
but when i try to open it, it keeps on loading and after some times shows "Problem loading page" or "No data recieved", as if gmail servers are not responding!!
Any suggestions whats the problem?
$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox";
$username = 'abc#gmail.com';
$password = "abc123";
$inbox = imap_open($hostname,$username,$password)or die('Cannot connect to Gmail: ' . imap_last_error());

$server_name = "imap.gmail.com";
$account_name = "email#mydomain.com";
$account_pwd = "my_account_password";
$inbox = imap_open("{".$server_name.":993/imap/ssl/novalidate-cert}INBOX",
$account_name, $account_pwd) OR error_imap_mail("Unable To Open Imap
Connection","Imap Failure",imap_errors());
function error_imap_mail($subject,$messagetext,$errors) {
global $report_email;
$mailfrom = "support#mydomain.com";
$mailsubject = "$subject";
$mailbody = "$messagetext\n\n";
if (count($errors) >0) {
if (substr($errors[0],0,8) <> "[IN-USE]") {
array_reverse($errors);
foreach ($errors as $key=>$value) {
$mailbody .= "\nErr No.".$key." Error:".$value;
}
foreach ($report_email as $value) {
mail($value, $mailsubject, $mailbody, "From: $mailfrom");
}
}#if (substr($err_array[0],0,8) <> "[IN-USE]") {
}#if (count($errors) >0) {
exit;
}
Get your script to send you the errors if it fails to connect.
Have you turned IMAP on in the account setting in Gmail?

Try this:
$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
$username = 'abc#gmail.com';
$password = "abc123";
$inbox = imap_open($hostname,$username,$password)or die('Cannot connect to Gmail: ' . imap_errors());
Also make sure you enable IMAP for you account via GMail settings.

Related

IMAP - in_reply_to always blank

I want to pair a reply with the original message.
I am connecting to a gmail account like so:
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'somebody#gmail.com'
$password = 'password';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'UNSEEN');
$max_emails = 3;
if($emails) {
rsort($emails);
foreach($emails as $email_number) {
$overview = imap_fetch_overview($inbox,$email_number,0);
if($count++ >= $max_emails) break;
}
}
When an email in the inbox is a reply, the [in_reply_to] field appears within imap_fetch_overview, so it knows it's a reply, however this field is always blank.
How can I pair a reply to the sent message?
Ignore this...
I was displaying results like so:
print_r($overview);
When doing this instead:
var_dump($overview);
The information is there.
Not sure why that is...

How to receive gmail using imap in server?

I want to receive the mails from gmail using imap. I have used the code $connection_result = $imap->connect('{imap.gmail.com:993/imap/ssl}INBOX', 'xxxxxxx#gmail.com', 'xxxxxx');
It works in the local xampp but if host in my server, its giving error as
Cannot connect to Mail: Can't connect to gmail-imap.l.google.com,993: Network is unreachable
How to use in server. Anyone can help?
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'example#gmail.com';
$password = 'example';
$inbox = imap_open($hostname,$username ,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'ALL');
if($emails) {
$output = '';
rsort($emails);
foreach($emails as $email_number) {
$overview = imap_fetch_overview($inbox,$email_number,0);
$output.= 'Name: '.$overview[0]->from.'</br>';
$output.= 'Message ID: '.$overview[0]->message_id.'</br>';
$output.= 'Subject: '.$overview[0]->subject.'</br>';
$output.= 'UID: '.$overview[0]->uid.'</br>';
$output.= '<hr/>';
}
echo $output;
}
imap_close($inbox);

PHP Freezing (again)

My website has a create account form, in which it inserts into a database and sends an email and verifies on my AWS SES server. But for some reason my code keeps freezing. If I put an echo "Testing" right above //include_phpmailer, "Testing" will not show up.
<?php
//include phpmailer
require_once('class.phpmailer.php');
require_once('ses.php');
$username = "scapXXXXXX";
$password = "baXXXXXXXX";
$hostname = "db4free.net:3306/scapterlogin";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Could Not establish connection!" . mysql_error());
$dbselect = mysql_select_db("scapterlogin", $dbhandle);
$myusername = $_POST['user'];
$mypassword = $_POST['pass'];
$myemail = $_POST['email'];
$query = "INSERT INTO userInfo (Username, Password, Email, activation_code) VALUES('$myusername', '$mypassword', '$myemail')";
$myquery = mysql_query($query);
if ($myquery) {
//verify email address using SES
$verify = new SimpleEmailService("access_key", "secret_key");
$verify->verifyEmailAddress($myemail));
//SMTP Settings
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "email-smtp.us-west-2.amazonaws.com";
$mail->Username = "AKIAXXXXXXXXX";
$mail->Password = "AoDRf8NBXXXXXXXXXXX";
//
$mail->SetFrom('webmaster#scapter.org', 'The Scapter Team'); //from (verified email address)
$mail->Subject = "New Account"; //subject
//message
$body = 'Dear " . $myusername . ", \r\n Welcome to Scapter! We hope you enjoy all of our products. We have got an account request from this email.
If you did not create an account, you can safely ignore this email.';
$body = eregi_replace("[\]",'',$body);
$mail->MsgHTML($body);
//
//recipient
$mail->AddAddress($myemail, $myusername);
//Success
if ($mail->Send()) {
//setcookie('loggedin', $myusername, Time()+3600);
//header('Location: login.php');
}
//Error
if(!$mail->Send()) {
//echo "Mailer Error: " . $mail->ErrorInfo;
}
}
?>
There are couple of unusual factors in your code..
$hostname for db connection does not seem to be a valid one (db4free.net:3306/scapterlogin). If you have an IP address of the db server, you should use that instead.
Using
if ($mail->Send()) {}
and
if(!$mail->Send()) {}
effectively calls the same method twice you should replace 2nd occurrence with a simple else condition.
Using 'die' before $mail->Send() or $myquery = mysql_query($query); can help you identify whether the specific block of code is causing trouble.
You say if you echo on the first line, it will not show up, which leads me to believe that the file is not being loaded, and maybe you have error display turned off, so it's not giving you an error? Check your logs for errors - maybe you are not loading this file with the correct name or you have permissions set so that your server doesn't want to run it.

Getting e-mail ID of sender while fetching mails from Gmail

I am fetching email from gmail where I can fetch body, name of the sender etc. I have to get the email-id of the sender, which I am not able to get. I have tried with some variables names like fromaddress after fetchting headerinfo by imap_header but didn't work.
Can I get some help regarding this ?
function connect_mail(){
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '*****#gmail.com';
$password = '*****';
$inbox = imap_open($hostname,$username,$password) or die(t('Cannot connect to Gmail: ' . imap_last_error()));
$emails = imap_search($inbox,'ALL');
$Msgcount = count($emails);
for ($x = 1; $x <= $Msgcount; $x++)
{
$overview = imap_fetch_overview($inbox, $x);
$title = $overview[0]->subject;
echo "Subject of the Mail : ".$title."</br>";
$from = $overview[0]->from;
echo "Name of the sender : ".$from."</br>";
//Now I have to get mail ID of senders & print it, but how?
}
}
I have tried my luck with several methods but got failed each time...
Thanks in Advance :)
I have done it..Hurray!
Here is the code:-
function connect_mail(){
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '*****#gmail.com';
$password = '*****';
$inbox = imap_open($hostname,$username,$password) or die(t('Cannot connect to Gmail: ' . imap_last_error()));
$emails = imap_search($inbox,'ALL');
$Msgcount = count($emails);
for ($x = 1; $x <= $Msgcount; $x++)
{
$overview = imap_fetch_overview($inbox, $x);
$title = $overview[0]->subject;
echo "Subject of the Mail : ".$title."</br>";
$from = $overview[0]->from;
echo "Name of the sender : ".$from."</br>";
$header = imap_headerinfo($inbox, $x);
$fromaddress = $header->from[0]->mailbox . "#" . $header->from[0]->host;
echo "From E-Mail Address : ".$fromaddress.;
}
}
Thanks :)

accessing my gmail inbox via php code

how i can access my gmail account through my php code? I need to get the subject and the from address to from my gmail account.And then i need to mark the accessed as read on gmail
Should i use gmail pop3 clint?is that any framework that i can use for accessing gmail pop3
server.
I would just use the PHP imap functions and do something like this:
<?php
$mailbox = imap_open("{imap.googlemail.com:993/ssl}INBOX", "USERNAME#googlemail.com", "PASSWORD");
$mail = imap_search($mailbox, "ALL");
$mail_headers = imap_headerinfo($mailbox, $mail[0]);
$subject = $mail_headers->subject;
$from = $mail_headers->fromaddress;
imap_setflag_full($mailbox, $mail[0], "\\Seen \\Flagged");
imap_close($mailbox);
?>
This connects to imap.googlemail.com (googlemail's imap server), sets $subject to the subject of the first message and $from to the from address of the first message. Then, it marks this message as read. (It's untested, but it should work :S)
This works for me.
<?php
$yourEmail = "you#gmail.com";
$yourEmailPassword = "your password";
$mailbox = imap_open("{imap.gmail.com:993/ssl}INBOX", $yourEmail, $yourEmailPassword);
$mail = imap_search($mailbox, "ALL");
$mail_headers = imap_headerinfo($mailbox, $mail[0]);
$subject = $mail_headers->subject;
$from = $mail_headers->fromaddress;
imap_setflag_full($mailbox, $mail[0], "\\Seen \\Flagged");
imap_close($mailbox);
?>
You can use IMAP from PHP.
<?php
$mbox = imap_open("{imap.example.org:143}", "username", "password")
or die("can't connect: " . imap_last_error());
$status = imap_setflag_full($mbox, "2,5", "\\Seen \\Flagged");
echo gettype($status) . "\n";
echo $status . "\n";
imap_close($mbox);
?>
Another nice IMAP example is available at http://davidwalsh.name/gmail-php-imap
Zend Framework has the Zend_Mail API for reading mail as well. It makes it easy to switch protocols if need be (POP3, IMAP, Mbox, and Maildir). Only the IMAP and Maildir storage classes support setting flags at this time.
http://framework.zend.com/manual/en/zend.mail.read.html
Read messages example from the Zend Framework docs:
$mail = new Zend_Mail_Storage_Pop3(array('host' => 'localhost',
'user' => 'test',
'password' => 'test'));
echo $mail->countMessages() . " messages found\n";
foreach ($mail as $message) {
echo "Mail from '{$message->from}': {$message->subject}\n";
}

Categories