UnitedForums - UK Web Hosting Forum UnitedHosting Community Hosting Forums
Network and Server StatusCustomer SupportUK Web Hosting
UnitedHostingUnitedHosting Sitemap UK Hosting ForumUK Web HostingWeb Hosting ForumsUK Reseller HostingWeb Host CommunityUK Managed Dedicated ServersHosting Help and SupportUK Domain Name Registration

Go Back   UnitedForums.co.uk > UnitedHosting Community > Website Development & Scripting

Reply
 
Thread Tools Rating: Thread Rating: 15 votes, 5.00 average. Display Modes
Old 20th January 2003, 06:10 PM   #1 (permalink)
ack
Senior Member
 
Join Date: Dec 2002
Posts: 101
script to check for new mail

I currently have a forwarder set up to forward e-mail to my mobile phone's address (so I can be notified of new mail) and it works great, but I don't want it to forward the whole message because long e-mails cost me money.

I want to write some sort of script that would be run as a cron job to send an e-mail to my mobile phone when I get new e-mail. I want it to just check for new e-mail and if it finds any, to send an e-mail containing the subject and who it's from to my phone's address.

I need advice on how to check for new mail through the script. Can I do this with php? How?

Hopefully one of you php coders has some ideas for me.
ack is offline   Reply With Quote
Old 20th January 2003, 06:20 PM   #2 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:script to check for new mail

Good! I was getting board! Let me see what I can dig up.
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 20th January 2003, 08:11 PM   #3 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:script to check for new mail

If anyone else can come up with something, feel free. I am having troubles.
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 20th January 2003, 08:51 PM   #4 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:script to check for new mail

Well I go this far:

Code:
<?PHP

$mailbox = imap_open("{mail.webendeavorsusa.com}INBOX","myusername","mypassword");
$header = imap_header($mailbox, 2);
print ("Subject: " . $header->subject);
imap_close($mailbox);
        
?>
But I am getting an error:


Warning: Bad message number in /home/virtual/site9/fst/var/www/html/mailme.php on line 4


Hmm I have read somewhere that you will get that error when 2 imap clients are trying to access the same mailbox. Is squirrlmail an imap client? anyone?


I'm going to get some beer. That usually fixes everything. Anyone see my updated picture in my profile? I figure it fits me better
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 20th January 2003, 08:56 PM   #5 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:script to check for new mail

Ah! See the thought of going for some beer fixed it! Imagine if I actually had the beer!

I didn't have any messages that's why it was a bad message number. I need to add a catch in there. Now that I have this working I can pull the subject and sender's mail address and send it via a mailto (in PHP of course).

I should have something for you soon.
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 20th January 2003, 09:44 PM   #6 (permalink)
ack
Senior Member
 
Join Date: Dec 2002
Posts: 101
Re:script to check for new mail

Thanks for your help, dwilson.

How are you checking for new mail?

So far (with a little help from php.net)I have this example script working:

Quote:
<?php

$mbox = imap_open("{mail.mydomain.com}INBOX",&qu ot;userid#mydomain.com",
"password")
or die("can't connect: ".imap_last_error());

$check = imap_mailboxmsginfo($mbox);

if($check) {
print "Recent: " . $check->Recent ."<br>
" ;
print "Unread: " . $check->Unread ."<br>
" ;
} else {
print "imap_check() failed: ".imap_last_error(). "<br>
";
}

imap_close($mbox);

?>
Using this imap_mailboxmsginfo() function, I can see how many "unread" messages and how many "recent" messages there are.

I wanted to check recent as well as unread, so I don't get a notification every hour if I don't immediately go read the e-mail. I've been testing it and the recent flag only seems to stay set for less than a minute after new mail is recieved. Anyone know exactly how this flag works?

I guess it would be OK to just send out the notification every hour if there are unread messages, but I still need to figure out how to pick out and e-mail the from address and subject of just the unread messages.

Sorry, I'm rambling. Guess I'll shut up and keep working on it.
ack is offline   Reply With Quote
Old 20th January 2003, 10:19 PM   #7 (permalink)
aldworth bloke
Registered User
 
Join Date: Jan 2003
Location: Berkshire, UK
Posts: 62
Send a message via Yahoo to aldworth bloke
Re:script to check for new mail

There is some (oldish) info here that implies that the recent flag is not much use...

http://www.washington.edu/imap/lista.../msg00009.html

You could always write the message ids (of the unseen messages) to a file locally, and only do a notification if the list has changed - then you'd just have the problem of when to reset the list

To get the messages that you are interested in use:

$msgs = imap_search ($mbox, "UNSEEN" // returns array of messages
for each $msg in $msgs {
pick out from and subject // not sure what format the array is in; you'll have to work this out
build list of senders/subjects into $notifstring
}

imap_mail("yourphone address", "Email list", $notifstring);

----------------
I'll look into the message array format later...
__________________
Simon G
Things should be as simple as possible - and no simpler
aldworth bloke is offline   Reply With Quote
Old 20th January 2003, 10:37 PM   #8 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:script to check for new mail

<?PHP
// Open your mailbox
$mailbox = imap_open("{mail.yourdomain.com}INBOX",& quot;yourusername@yourserver.com","yourp assword"

// initialize $numMsg with the number of total messages in your box

$numMsg = imap_num_msg ($mailbox);

// If you have some then go ahead and process

if ( $numMsg > 0 )
{
// Loop until you equal or exceed the number of messages you have
for ($i = 1; $i <= imap_num_msg($mailbox); $i++)
{

$header = imap_header($mailbox, $i);

// This checks if you have recent mail or unread mail.
// Recent == 'N' means recent and unread. Unseen == 'U' means yes you have unread mail
if ($header->Recent == 'N' || $header->Unseen == 'U')
{
// you musta had some current unread messages so send yourself an email containing the subject and sender

$mailTo = "youremail@yourserver.com";
$mailSubject = "$header->subjectn";
$mailBody = $header->subject;
$mailBody .= $header->fromaddress;
$mailHeaders = "From: root@$SERVER_NAME.comn";
if (mail($mailTo, $mailSubject, $mailBody, $mailHeaders))
{
print("Mail successfull sent to $mailTo."
}
else
{
print("Mail not successfull sent to $mailTo."

}
}
}

}

imap_close($mailbox);

?>
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 20th January 2003, 10:41 PM   #9 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:script to check for new mail

I tried replacing youremail@yourserver.com with "youremail@" . $SERVER_NAME.com
but in my case it tried to mail to dwilson@www.webendeavorsusa.com so you'll have to figure a way to cut off the WWW. For your purpose you really don't need to worry about it since you know the address that it will be going to.


And the beer did help. LOL
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 20th January 2003, 10:43 PM   #10 (permalink)
ack
Senior Member
 
Join Date: Dec 2002
Posts: 101
Re:script to check for new mail

Thanks, this helps a lot.
ack is offline   Reply With Quote
Old 20th January 2003, 10:43 PM   #11 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:script to check for new mail

I guess you'll have to set up something to run that job every few minutes as well. Want me to look into that?
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 20th January 2003, 10:46 PM   #12 (permalink)
ack
Senior Member
 
Join Date: Dec 2002
Posts: 101
Re:script to check for new mail

No, I think I can just use cron. Can't wait to try it out.

Thank you both for your help.
ack is offline   Reply With Quote
Old 20th January 2003, 10:48 PM   #13 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:script to check for new mail

I ran through and commented it.
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 20th January 2003, 10:49 PM   #14 (permalink)
aldworth bloke
Registered User
 
Join Date: Jan 2003
Location: Berkshire, UK
Posts: 62
Send a message via Yahoo to aldworth bloke
Re:script to check for new mail


<?PHP

$mailbox = imap_open("{mail.yourdomain.com}INBOX",& quot;yourusername@yourserver.com","yourp assword"
$mailTo = "youremail@yourserver.com";
$mailSubject = "$header->subjectn";
$mailbody = "";

$numMsg = imap_num_msg ($mailbox);
if ( $numMsg > 0 )
{
for ($i = 1; $i <= imap_num_msg($mailbox); $i++)
{

$header = imap_header($mailbox, $i);

// This checks if you have recent mail or unread mail.

if ($header->Recent == 'N' || $header->Unseen == 'U')
{
$mailBody .= $header->subject;
$mailBody .= $header->fromaddress;
}
}
$mailHeaders = "From: root@$SERVER_NAME.comn";
if (mail($mailTo, $mailSubject, $mailBody, $mailHeaders))
{
print("Mail successfull sent to $mailTo."
}
else
{
print("Mail not successfull sent to $mailTo."
}

}

imap_close($mailbox);

?>


Just moved it around a bit so you get only one notification (I'm assuming you want as few as possible?)
__________________
Simon G
Things should be as simple as possible - and no simpler
aldworth bloke is offline   Reply With Quote
Old 20th January 2003, 10:55 PM   #15 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:script to check for new mail

True. Sorry about that. I had it sending a mail every time you received a new one. Thanks Aldworth
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 20th January 2003, 10:57 PM   #16 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:script to check for new mail

wouldn't you want a carriage control in there when you write that stuff to the variables? I woud think that it would line up one right after another.

like this

$mailBody .= "$header->subjectn";
$mailBody .= "$header->fromaddressn";
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 20th January 2003, 11:00 PM   #17 (permalink)
aldworth bloke
Registered User
 
Join Date: Jan 2003
Location: Berkshire, UK
Posts: 62
Send a message via Yahoo to aldworth bloke
Re:script to check for new mail

Yeah...I didn't really consider formatting, as ack needs to think about how to do it so that it looks good on his phone...
__________________
Simon G
Things should be as simple as possible - and no simpler
aldworth bloke is offline   Reply With Quote
Old 20th January 2003, 11:04 PM   #18 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:script to check for new mail

They do use XML. Maybe he can format it that way?
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 20th January 2003, 11:09 PM   #19 (permalink)
aldworth bloke
Registered User
 
Join Date: Jan 2003
Location: Berkshire, UK
Posts: 62
Send a message via Yahoo to aldworth bloke
Re:script to check for new mail

Stretching the boundaries of my knowledge there....but I daresay it could be done. I don't know enough about the way that specific system works. In the UK its usually SMTPd to the phone operator then sent as SMS to the phone
__________________
Simon G
Things should be as simple as possible - and no simpler
aldworth bloke is offline   Reply With Quote
Old 20th January 2003, 11:13 PM   #20 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:script to check for new mail

Ok. I think in the U.S. they were trying to send XML to the cell and wireless phones to make the language standard.

Well, I have never in my life done anything with the mail function so I hereby promote myself to the honorary title of "Not so new PHP newbie!"

Of course Aldworth helped with the loop thing. Otherwise that would've sent you many many emails.

Thanks Aldworth!

LOL That wore me out. I am going for some ghost recon now.

__________________
Derik Wilson
dwilson is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 03:31 PM.

UK Web Hosting  |  UK Reseller Hosting  |  UK Dedicated Servers UnitedHosting  |  UnitedSupport  |  UnitedForums  |  SEO by vBSEO 3.0.0
Copyright © 1998-2008 United Communications Limited. All Rights Reserved. Registered in England and Wales 3651923 - VAT Reg No. 737662309