get gmail items to custom page - php

I'm trying this to get the sent emails from Gmail to my custom page.
I've added dummy Gmail credentials while checking please change with your real Gmail account
How can I get the emails? This code isn't giving any errors but also not fetching any emails from the Gmail sent box.
<?php
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'adasd1231312#gmail.com';
$password = '**************';
//echo $hostname;
/* try to connect */
$inbox = imap_open($hostname, $username, $password);
/* grab emails */
$emails = imap_search($inbox,'ALL');
// if emails are returned, cycle through each...
if($emails) {
// begin output var
$output = '';
// put the newest emails on top
rsort($emails);
// for every email...
foreach($emails as $email_number) {
// get information specific to this email
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
// output the email header information
$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
$output.= '<span class="from">'.$overview[0]->from.'</span>';
$output.= '<span class="date">on '.$overview[0]->date.'</span>';
$output.= '</div>';
// output the email body
$output.= '<div class="body">'.$message.'</div>';
}
echo $output;
}
/* close the connection */
imap_close($inbox);
?>
Look forward to hearing from all of you guys.
Help me with this.

Related

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);

"OnClick" Ajax function on ''IMAP to PHP page''

Everything works fine, but i need one more function. On every newmail, in same line i need button "OWNER", so when user input his inicials in that field, field will be locked so anybody can know who own which mail.
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'X';
$password = 'X';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
/* output the email header information */
$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
$output.= '<span class="from">'.$overview[0]->from.'</span>';
$output.= '</div>';
}
echo $test , $test , $output;
}
imap_close($inbox);

Storing Email from IMAP(Unique Email ID)?

How do i store a UNIQUE ID from that email in my database.
I tried $overview = imap_fetch_overview($inbox,$email_number,0); and i received a bunch of numbers but the problem is the number will change when one of the email is deleted.
How do I store it properly? MD5 the message or something?
Basically i'm trying to receive email on my personal web application where i can manage and access my own email. It calls gmail using imap.
Anyway where i can check and store only new emails?
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'xxxx#gmail.com';
$password = '';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');
dd($emails);
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
/* output the email header information */
$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
$output.= '<span class="from">'.$overview[0]->from.'</span>';
$output.= '<span class="date">on '.$overview[0]->date.'</span>';
$output.= '</div>';
/* output the email body */
$output.= '<div class="body">'.$message.'</div>';
}
echo $output;
}
/* close the connection */
imap_close($inbox);
The UID exists for this purpose, it should remain unchanged unless the server gives a different UIDVALIDITY value.
https://www.rfc-editor.org/rfc/rfc3501#section-2.3.1.1
The UID is part of the IMAP standard so it should be correctly implemented by all IMAP servers. If you are only working against gmail, then you might want to look into another value though, since the same message can be seen under different labels, and then the UID might be different for that logically identical message. I don't know the gmail API though.

Fetching mail from pop3 server and leaving copy in the mail server

i am using the following code fetch mail from our POP3 mail server.i have successfully fetched mails.
i would like leave a copy of the mail in the server.how should i do that
<?php
/* connect to gmail */
$hostname='{mail.xxx.com:110/pop3}INBOX';
$username='yyy.xxx#xxx.com';
$password='xxx_9851';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
echo "here";
/* grab emails */
$emails = imap_search($inbox,'SUBJECT "hi"');/* if emails are returned, cycle through each... */
//print_r($emails);
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information
specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
echo "<pre>";
// print_r($overview);
$message = imap_fetchbody($inbox,$email_number,2);
$header=imap_fetchheader($inbox,$email_number);
//print_r($message);
//print_r($header);
/* output the email header information */
$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
$output.= '<span class="from">'.$overview[0]->from.'</span>';
$output.= '<span class="date">on '.$overview[0]->date.'</span>';
$output.= '</div>';
/* output the email body */
$output.= '<div class="body">'.$message.'</div>';
$s = imap_fetchstructure($inbox,$email_number);
// print_r ($s->parts);
if (!$s->parts)
{// simple
// getpart($inbox,$email_number,$s,0); // pass 0 as part-number
}
else { // multipart: cycle through each part
foreach ($s->parts as $partno0=>$p){
attachment_to_file($inbox,$email_number,$p,$partno0+1);
}
}
}
Any help is greatly appreciated.thank you
First of all your are using an IMAP code to fetch the mails so I assume your pop3 servers have imap service also running. And if you are using IMAP service then there is no need of explicitly doing a leave a copy of the mail in the server. the mails will remain on the server unless they are deleted or moved from folder to transh/junk.
The same holds good for POP service also. the mails will always remain on server unless you send a delete command.

Webmail access through PHP [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have Cpanel and domain. I can create email account by going to the email account option in Cpanel. My question is how can I create an email account in Cpanel from a php web application?
I want a proper guide to this.
Thanks in advance.
Please Check it out.I hope it will help you what you want.
http://www.zubrag.com/scripts/cpanel-create-email-account.php
Check this out. Its for gmail account.Configure for your account:
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'username#gmail.com';
$password = 'password here';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
/* output the email header information */
$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
$output.= '<span class="from">'.$overview[0]->from.'</span>';
$output.= '<span class="date">on '.$overview[0]->date.'</span>';
$output.= '</div>';
/* output the email body */
$output.= '<div class="body">'.$message.'</div>';
}
echo $output;
}
/* close the connection */
imap_close($inbox);
I think this will help you.Thanks.razin223

Categories