ptonev.com

Products and Projects => php Mail Notification => Topic started by: Patches on September 24, 2004, 07:40:08 pm



Title: subject of email
Post by: Patches on September 24, 2004, 07:40:08 pm
Is there anyway to retrive the subject of each emails, and forward those on.

I dont want the email itself,  just the subject line.   I really dont even need the sender's info,  just the subject would be great.  :-)


Title: subject of email
Post by: ptonev on September 24, 2004, 09:43:55 pm
Hi,

> Is there anyway to retrive the subject of each emails, and forward those on.

Yes have way, but must make change on script.

That version you use: socket or IMAP-i?

best regards


Title: subject of email
Post by: Anonymous on September 25, 2004, 04:47:39 am
Do I have to use the imap system,  or does just stealing the explode routine from the imap system work on the pop3??


Title: subject of email
Post by: Patches on September 25, 2004, 05:02:01 am
Im using the pop3 verision  the standard one I guess,  not the imap one....

Here is what Im looking for in my message I send to my cell phone.

From: (will be my mailserver)
Subject: 3 New Emails
Text:
Subject: New Design looks good
Subject: Please send photos
Subject: Payment Sent




I do not care to see who they are from,  just the subject lines only.  I can usually tell from that who they are from.


Title: subject of email
Post by: ptonev on September 25, 2004, 10:11:37 am
Hi, Patches.

Try this:

Code:
find in line 88:
$pos = strpos($v, "From:");

replace with :
$pos = strpos($v, "Subject:");


find in line 96:
return trim(substr($v, 5));

replace with :
return trim(substr($v, 8));


find in line 138:
$from = __mnotify_get_simple_email($from);

replace with :
// $from = __mnotify_get_simple_email($from);


I think that will be enough to solved you question.


Title: subject of email
Post by: Anonymous on September 27, 2004, 05:41:07 pm
That worked,

but if there is only one email, I dont get the subject.   Appears to be a loop problem...


Title: subject of email
Post by: ptonev on September 28, 2004, 02:54:15 am
This is a very strange...

Can you post example or describe situation...


Title: subject of email
Post by: Patches on September 28, 2004, 09:17:07 am
Here is my Config File (edit of course)

Code:

#host;user;password;mailto;subject text;body text
nwdinc.net;email address;pass;XXXXXXXXXXX@messaging.sprintpcs.com;%new% - %emails%;


If I only have one email come into my email box, I get this on my phone:

From: nwdinc@comet.fast-servers.net   <--  This my server,  understand that

Message: Subject: 1 - .

And then its time stamped.


But if there is two messages then I get:

From: nwdinc@comet.fast-servers.net
Message: Subject:  2 - Subject from Message 1,  Subject from Message 2

Here is the file Im using.

Code:

<?php

define
&#40;"MNOTIFY_CFG_PATH", "./"&#41;;
define&#40;"MNOTIFY_CFG_NAME", "phpMailNotification.cfg"&#41;;
define&#40;"MNOTIFY_SUBJECT", "%new% new from %total% total email&#40;s&#41; in %account%."&#41;;
define&#40;"MNOTIFY_MESSAGE", "%emails%"&#41;;

//
// __mnotify_read_file - read content from file
//
function __mnotify_read_file&#40;$name&#41;
&#123;
$contents false;

if&#40;file_exists&#40;$name&#41;&#41;
&#123;
if&#40;$fd = @fopen&#40;$name, "r"&#41;&#41;
&#123;
$contents fread&#40;$fd, filesize&#40;$name&#41;&#41;;

fclose&#40;$fd&#41;;
&#125;
&#125;

return $contents;
&
#125;

//
// __mnotify_write_file - write data to file
//
function __mnotify_write_file&#40;$name, $data&#41;
&#123;
if&#40;$fd = @fopen&#40;$name, "w"&#41;&#41;
&#123;
fwrite&#40;$fd, $data&#41;;
fclose&#40;$fd&#41;;

return true;
&#125;
else
&#123;
return false;
&#125;
&#125;

function __mnotify_get_simple_email&#40;$str&#41;
&#123;
$open strpos&#40;$str, "<"&#41;;
$close strpos&#40;$str, ">"&#41;;

if&#40;&#40;$open === false&#41; && &#40;$close === false&#41;&#41;
&#123;
return $str;
&#125;

return substr&#40;$str, $open + 1, $close - $open - 1&#41;;
&#125;

function __mnotify_get_from_mail&#40;$fp, $number, $size&#41;
&#123;
fputs&#40;$fp, "RETR $number\n"&#41;;
$line fgets&#40;$fp, 200&#41;;

$line fread&#40;$fp, $size&#41;;
$line explode&#40;"|", str_replace&#40;"\n", "|", $line&#41;&#41;;

if&#40;is_array&#40;$line&#41; && &#40;count&#40;$line&#41; > 0&#41;&#41;
&#123;
foreach&#40;$line as $v&#41;
&#123;
$pos strpos&#40;$v, "Subject&#58;"&#41;;

if&#40;$pos === false&#41;
&#123;
// noop
&#125;
else
&#123;
return trim&#40;substr&#40;$v, 8&#41;&#41;;
&#125;
&#125;
&#125;

return false;
&
#125;

function __mnotify_read_mails&#40;$fp, $list, $msgs&#41;
&#123;
$count $list[0&#93;;
unset&#40;$list[0&#93;&#41;;

$get true;

if&#40;$count > $msgs&#41;
&#123;
for&#40;$i = 1; $i <= $msgs; $i++&#41;
&#123;
unset&#40;$list[$i&#93;&#41;;
&#125;
&#125;
else if&#40;$count < $msgs&#41;
&#123;
&#125;
else
&#123;
$get false;
&#125;

if&#40;is_array&#40;$list&#41; && &#40;count&#40;$list&#41; > 0&#41; && $get&#41;
&#123;
$mlist "";

foreach&#40;$list as $v&#41;
&#123;
$vv explode&#40;" ", $v&#41;;

if&#40;is_array&#40;$vv&#41; && &#40;count&#40;$vv&#41; == 2&#41;&#41;
&#123;
$sep = &#40;strlen&#40;$mlist&#41; > 0&#41;? ", "&#58; "";
$from __mnotify_get_from_mail&#40;$fp, $vv[0&#93;, $vv[1&#93;&#41;;
// $from = __mnotify_get_simple_email&#40;$from&#41;;

$mlist $mlist.$sep.$from;
&#125;
&#125;

return $mlist;
&#125;
else
&#123;
return "";
&#125;
&#125;

//
// __mnotify_check_mail - check email account
//
function __mnotify_check_mail&#40;$host, $port, $user, $pass, $msgs&#41;
&#123;
$count 0;

$fp fsockopen&#40;$host, $port, $errno, $errstr&#41;;

if&#40;!$fp&#41;
&#123;
// could not open socket connection
return false;
&#125;
else
&#123;
$r fgets&#40;$fp, 200&#41;;

if&#40;substr&#40;$r, 0, 3&#41; == "+OK"&#41;
&#123;
fputs&#40;$fp, "USER $user\n"&#41;;
fgets&#40;$fp, 200&#41;;

   fputs&#40;$fp, "PASS $pass\n"&#41;;
$r fgets&#40;$fp, 200&#41;;

if&#40;substr&#40;$r, 0, 3&#41; == "+OK"&#41;
&#123;
   fputs&#40;$fp, "STAT\n"&#41;;
$r fgets&#40;$fp, 200&#41;;

if&#40;substr&#40;$r, 0, 3&#41; == "+OK"&#41;
&#123;
$r explode&#40;" ", $r&#41;;
$count $r[1&#93;;

   fputs&#40;$fp, "LIST\n"&#41;;
$list fgets&#40;$fp, 200&#41;;

$mlist "";

if&#40;substr&#40;$list, 0, 3&#41; == "+OK"&#41;
&#123;
$list = array&#40;&#41;;
$list[0&#93; = $count;

for&#40;$i = 0; $i < $count; $i++&#41;
&#123;
$list[&#93; = fgets&#40;$fp, 200&#41;;
&#125;

$mlist __mnotify_read_mails&#40;$fp, $list, $msgs&#41;;
&#125;

fputs&#40;$fp, "QUIT\n"&#41;;
   fclose&#40;$fp&#41;;

   if&#40;$mlist != ""&#41;
   &#123;
return array&#40;$count, $mlist&#41;;
   &#125;
   else
   &#123;
return $count;
   &#125;
&#125;
else
&#123;
// server said&#58; $r
return false;
&#125;
&#125;
else
&#123;
// server said&#58; $r
return false;
&#125;
&#125;
else
&#123;
// bad connection string
return false;
&#125;
&#125;
&#125;

// read cfg file
$contents __mnotify_read_file&#40;MNOTIFY_CFG_PATH.MNOTIFY_CFG_NAME&#41;;

if&#40;$contents === false&#41;
&#123;
echo "Can't read config file.<b>\n";
&
#125;
else
&
#123;
// parse and explode account
$accounts explode&#40;"\n", $contents&#41;;

if &#40;count&#40;$accounts&#41; > 0&#41;
&#123;
foreach&#40;$accounts as $v&#41;
&#123;
// skip comment accounts
if&#40;substr&#40;$v, 0, 1&#41; == "#"&#41;
&#123;
continue;
&#125;

// parse account on tokens
$tokens explode&#40;";", $v&#41;;

// prepare account name
$account $tokens[1&#93;;

if&#40;strpos&#40;$account, "@"&#41; === false&#41;
&#123;
$account $tokens[1&#93;."@".$tokens[0&#93;;
&#125;

// prepare account password
$password $tokens[2&#93;;

if&#40;substr&#40;$password, 0, 1&#41; == "#"&#41;
&#123;
$password base64_decode&#40;substr&#40;$password, 1&#41;&#41;;
&#125;

// compose account file name
$file_name $tokens[1&#93;."@".$tokens[0&#93;.".cfg";

// get subject template text
if&#40;!isset&#40;$tokens[4&#93;&#41;&#41;
&#123;
$tokens[4&#93; = MNOTIFY_SUBJECT;
&#125;

// get body template text
if&#40;!isset&#40;$tokens[5&#93;&#41;&#41;
&#123;
$tokens[5&#93; = MNOTIFY_MESSAGE;
&#125;

// read old email count
$msg_old __mnotify_read_file&#40;MNOTIFY_CFG_PATH.$file_name&#41;;

// check email
$new __mnotify_check_mail&#40;$tokens[0&#93;, 110, $tokens[1&#93;, $password, $msg_old&#41;;

if&#40;$new === false&#41;
&#123;
echo "Can't check $account account.<b>\n";
&#125;
else
&#123;
if&#40;is_array&#40;$new&#41; && &#40;count&#40;$new&#41; == 2&#41;&#41;
&#123;
$msg_new $new[0&#93;;
$body $new[1&#93;;
&#125;
else
&#123;
$msg_new $new;
$body ".";
&#125;

if&#40;$msg_new > $msg_old&#41;
&#123;
$msg_dif $msg_new $msg_old;

$_msg $tokens[4&#93;;
$_msg str_replace&#40;"%new%", $msg_dif, $_msg&#41;;
$_msg str_replace&#40;"%total%", $msg_new, $_msg&#41;;
$_msg str_replace&#40;"%account%", $account, $_msg&#41;;
$_msg str_replace&#40;"%emails%", $body, $_msg&#41;;

$_body $tokens[5&#93;;
$_body str_replace&#40;"%new%", $msg_dif, $_body&#41;;
$_body str_replace&#40;"%total%", $msg_new, $_body&#41;;
$_body str_replace&#40;"%account%", $account, $_body&#41;;
$_body str_replace&#40;"%emails%", $body, $_body&#41;;

if&#40;mail&#40;$tokens[3&#93;, $_msg, $_body, "From&#58; $account"&#41;&#41;
&#123;
echo "[$_msg&#93;, [$_body&#93;<b>\n";
&#125;
else
&#123;
echo "Can't send email to ".$tokens[3&#93;." successfully.<b>\n";
&#125;
&#125;
__mnotify_write_file&#40;MNOTIFY_CFG_PATH.$file_name, $msg_new&#41;;
&#125;
&#125;
&#125;
else
&#123;
echo "Can't extract account from config file.<b>\n";
&#125;
&#125;

?>


Title: subject of email
Post by: ptonev on September 30, 2004, 06:43:14 pm
Hi,

this is a very strange.

Now I tested it and all work fine. I received fine subject when I have one email.

You are sure that you not have a email with empty subject.

Best regards,


Title: subject of email
Post by: Patches on September 30, 2004, 09:25:34 pm
Im sure the emails dont have blank subject lines,  cause when I actually get to a computer, they are there.

After some testing, its not just a single email, where it doesnt send the subject,  it skips the first subject, no matter how many emails I have.

1 email - No subject displayed

3 emails - 2 Subjects displayed,  skipping the first one..

etc


Title: subject of email
Post by: ptonev on October 02, 2004, 07:30:10 pm
Hi,

really script have sometime problem with received on subjects.

You can try and IMAP-i version. It work really better.

Try it and if it work I will be say what you must fix that you read subject not email.


Title: subject of email
Post by: Patches on October 06, 2004, 08:03:05 pm
Ok, I switched to the IMAP one,  any tips on making it show the subject instead of sender?


Title: subject of email
Post by: ptonev on October 06, 2004, 10:58:07 pm
Hi,

Open phpIMAPMailNotification.php file:

Code:
find in line 102:
$header = imap_headerinfo($mbox, $i, 80, 80);
$froms = $header->from;

replace with:
$header = imap_headerinfo($mbox, $i, 1024, 1024);
$fetchsubject = $header->fetchsubject;

find in line 105:
foreach($froms AS $from)
{
$sep = (strlen($email_list) > 0)? ", ": "";
$email_list = $email_list.$sep.$from->mailbox."@".$from->host;
}

replace with:
$sep = (strlen($email_list) > 0)? ", ": "";
$email_list = $email_list.$sep.$fetchsubject;


that is, try it.


Title: subject of email
Post by: Anonymous on October 07, 2004, 09:32:36 am
Ok,  changed those things,  now all I get is the count,  no matter how many emails.  I never receive the subject.  :-(    If I make the changes back,  I get the sender's email addy no problem


Title: subject of email
Post by: Patches on October 07, 2004, 09:37:52 am
Opps forgot to say Im the one above...


Title: subject of email
Post by: ptonev on October 07, 2004, 08:48:28 pm
Hummmm,

Now I test is and all work fine.

When I send 1 email I received this:

[1 - test 1], []
[N - xxx], []

where N is email count and xxx is subject.

Then I send 2 new email and received this:
[2 - test 2, test 3], []

all is ok for me...

check __mnotify_check_mail function:
Code:

function __mnotify_check_mail($host, $port, $user, $pass, $email_old)
{
$read_header = true;
$email_count = 0;
$email_first = 1;
$email_list  = "";

// open mailbox
$mbox = imap_open("{".$host.":".$port."/pop3/notls}INBOX", "$user", "$pass");

if($mbox === false)
{
return false;
}

// get emial count
$email_count = imap_num_msg($mbox);

// find & calc new emails
if($email_old == 0)
{
}
elseif($email_count > $email_old)
{
$email_first = $email_old + 1;
}
else if($email_count < $email_old)
{
}
else
{
$read_header = false;
}

if($read_header)
{
// get emails header
for ($i = $email_first; $i <= $email_count; $i++)
{
$header = imap_headerinfo($mbox, $i, 1024, 1024);
$fetchsubject = $header->fetchsubject;

$sep = (strlen($email_list) > 0)? ", ": "";
$email_list = $email_list.$sep.$fetchsubject;
}
}

// close mailbox
imap_close($mbox);

// return data
if($email_list != "")
{
return array($email_count, $email_list);
}
else
{
return $email_count;
}
}


best regards,


Title: subject of email
Post by: Patches on October 17, 2004, 12:22:29 pm
Well I kinda got it working..  The problem I am having now is the message send routine is only sending 32 characters no matter what.    

So if I have a few messages,   I typically only get the first subject.   :-(


Title: subject of email
Post by: ptonev on October 21, 2004, 12:54:51 am
Hi,

> The problem I am having now is the message send routine is only sending 32 characters no matter what.

I don't understand you.

> So if I have a few messages, I typically only get the first subject. :-(

Strange, I received all.

Example - this is a subject of text. Here is a 3 messages with long site:
Subject: 3 - Nepoznat GSM: SEC-SGHP510/1.0 UP.Browser/6.2.2.6 (GUI) MMP/1.0, Nevalidna zaiavka. Greshen WAP key: , Nevalidna zaiavka. Greshen WAP key:

best regards,


Title: subject of email
Post by: Patches on October 21, 2004, 03:30:19 am
Lets do it by example:

Lets say I get this message into my mailbox

From: bob@jones.com
Subject: What are you going to do for dinner tonnite?

Well what will come across throught the program is this:

1 - What are you going to do for dinner t

Then lets say I get the above message and another message:

From: bob@jones.com
Subject: What are you going to do for dinner tonnite?

From: Janet@business.com
Subject: Here is your report
             
Well now all I will get is:

2 -  What are you going to do for dinner t

Now if the same above two messages come into the mailbox in reverse order then I get:

2 - Here is your report, Wha

As you can see I only get 32 characters no matter what.  At first I thought it was cell phone provider,  so I tested by pointing the program to regular email address.  Same thing happens.   I dont see anything in the script that should be parsing the values,  but I do not know php that well.


Title: subject of email
Post by: ptonev on October 21, 2004, 08:15:34 am
Hi,

let make test.

Send email on test [put here correct char]ptonev[put here correct char]com

On URL you can see what script must be send:

http://www.ptonev.com/test1/phpIMAPMailNotification.php

I test and all work fine.


Title: subject of email
Post by: Patches on November 03, 2004, 12:59:19 pm
Sorry I was gone for a couple weeks...

Anyways,  it must be something with my server....  

Cause no matter what I do, the above happens....


Title: subject of email
Post by: ptonev on November 03, 2004, 06:54:22 pm
hi,

read my previous posting and try/test it.


Title: subject of email
Post by: Patches on November 10, 2004, 03:10:41 pm
Well I figured it out..  Neither one of us is crazy.

I have SprintPCS cell phone service here in the US.  I am sending the messages to my cell phone.  Well Sprint had decided to parse text messages sent via email.  If I go in and read the actual email before its sent to my phone as a text message its completely there.   Thanks for all your help

Patches


Title: subject of email
Post by: ptonev on November 11, 2004, 08:19:18 am
> Thanks for all your help

I hope that can help you.