×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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
 

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

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(i).'<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;'

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close