Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using Mail::POP3Client to pull mail from GMail...

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
i have this script:

Code:
use Mail::POP3Client;
use IO::Socket::SSL;
use CGI qw(:standard);
$cgi = new CGI;

$username  = 'myemailaddress@gmail.com';
$password  = 'mypassword';

$mailhost  = 'pop.gmail.com';
$port      = '995';

print $cgi->header();

$pop = new Mail::POP3Client(
USER     => $username,
PASSWORD => $password,
HOST     => $mailhost,
PORT	 => $port,
USESSL   => 'true',
DEBUG	 => 0,
);

if (($pop->Count()) < 1) {
print "No messages...\n";
exit;
}

print $pop->Count() . " messages found!<br><br>";

for($i = 1; $i <= $pop->Count(); $i++) {
 foreach($pop->Head($i)) {
 /^(From|Subject|Email):\s+/i && print $_, "<br><br>";
 }
print '<br>Message: '.$pop->Body(i).'<br><br>';
}

$pop->Close();

exit;

It works fine, but only shows me the Subject and From lines.

It does show me the message, but it's cluttered with:

***
MIME-Version: 1.0 Received: by 1.1.1.1 with HTTP; Mon, 14 Sep 2009 07:51:45 -0700 (PDT) Date: Mon, 14 Sep 2009 06:51:45 -0800 Delivered-To: myemailaddress@gmail.com Message-ID: <s897zda0f@mail.gmail.com> Subject: This is the subject From: John Smith To: myemailaddress@gmail.com Content-Type: multipart/alternative; boundary=000e0cd29db6fc093004738acd42 --000e0cd29db6fc093004738acd42 Content-Type: text/plain; charset=ISO-8859-1 This is the message --000e0cd29db6fc093004738acd42 Content-Type: text/html; charset=ISO-8859-1 This is the message --000e0cd29db6fc093004738acd42--
***

I want to just pull the message out, as well as the "From" email address.

Any ideas?

- g
 
Code:
for($i = 1; $i <= $pop->Count(); $i++) {
 foreach($pop->Head($i)) {
 /^(From|Subject|Email):\s+/i && print $_, "<br><br>";
 }
print '<br>Message: '.$pop->Body([b][red]i[/red][/b]).'<br><br>';
}

Is this a typo? Surely you meant $i.

An e-mail message contains headers in it, just like an HTTP request/response. It's fairly easy to separate the headers from the body of the message, just look for a single blank line.

Code:
From: soandso
To: suchandsuch
Subject: my subject
X-Some-Header: etc
Content-Length: 50

The body of the message is here

With multi-part messages (text/plain and text/html), there's another set of headers followed by the other format for the message. I haven't worked with email-related stuff in a while though so I don't remember the syntax for the start of the second set of headers... the best bet may be to pay attention to the Content-Length header in the first part, and once the headers are done with, only read that many bytes.. after that the next set of headers would start.

If e-mail doesn't have Content-Length, it might be in that "boundary=" part of the Content-Type... I'm sure this info is readily available online, i.e. RFC 1939.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top