Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: PHP and MySQL: Create-Modify-Reuse ISBN: 978-0-470-19242-9
This is the forum to discuss the Wrox book PHP and MySQL: Create-Modify-Reuse by Timothy Boronczyk, Martin E. Psinas; ISBN: 9780470192429
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: PHP and MySQL: Create-Modify-Reuse ISBN: 978-0-470-19242-9 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 2nd, 2009, 07:31 PM
Authorized User
 
Join Date: Dec 2009
Location: Inverness, Scotland
Posts: 16
Thanks: 0
Thanked 1 Time in 1 Post
Default Chapter 3 - Email List

Hi folks,

I'm trying desperately to get this script to work...

It's installed fine on my server and all connections seem to be working fine. However, I'mhaving real trouble getting the script to read data from emails (both subjects and message content etc).

It seems to read the sender's email address fine and gets the full body of any email sent to the list, but then seems to fail when trying to extract the subject of the email (i.e. when 'subscribe' commands or similar are sent to the list admin) and also, when running a cron-tab job on either individual.php or digest.php, it just doesn't seem to extract any data from the email to place in the digest .txt file...

I've tried debugging as far as I'm able with my limited knowledge, but now I'm stuck. I think I have narrowed it down to what the code is actually extracting by way of the preg_match_all command/or where in the array it's storing it. It either doesn't seem to be managing to locate a match in the email, or the part of the array where the extracted data is stored isn't being brought into play by the script...

In the manage.php file I changed a wee bit of code as follows:

Code:
$subject = trim($matches[2][1]);
changed to
Code:
$subject = trim($matches[2][2]);
This seemed to work and enabled the subject to be located and allow people to sign up via email etc...don'tthink I should really have needed to do this though??!!

As for the individual.php and digest.php files, I just can't get them to work via similar means...am I doing something obviously wrong? Anyone got any ideas?!

Getting frustrated now :(

Last edited by daibhidh; December 3rd, 2009 at 09:16 AM..
Reply With Quote
  #2 (permalink)  
Old December 3rd, 2009, 06:27 PM
Authorized User
Points: 374, Level: 6
Points: 374, Level: 6 Points: 374, Level: 6 Points: 374, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2009
Location: San Jose, Californina
Posts: 77
Thanks: 4
Thanked 6 Times in 6 Posts
Default

Hi Daibhidh,

I am subscribed to my own post so I got notified
right away of your response and I responded to it today.
That happened before I saw this original post here,
sorry.

Anyway, you have some more interesting information
here so I will try to add to it.

Given what you have said about the subject being in
[2][2] it might be interesting to look at both
$message and the $matches array.

Try putting in a statement to print out the
matches array.
print_r($matches);

My matches array looks something like this.

Array
(
[0] => Array
(
[0] => From: me@somewhere.net
[1] => Subject: HELP
)

[1] => Array
(
[0] => me@somewhere.net
[1] =>
)

[2] => Array
(
[0] =>
[1] => HELP
)

)

As you can see [1][0] has the from
and [2][1] has the subject which in this case is HELP.

****
Let me make a general comment for anyone else
working in this chapter.

A user may keep pouring over the manage.php
or individual.php file, but that may not be
the problem. There are a couple of tricky
things going on in this chapter.

1. manage.php and individual.php delete the mail each
time you run them. So you have to make sure you put
new mail into the account before running again, otherwise
you will have no mail. One way to get around this is
to comment out the line that deletes the mail. Then
uncomment once you have fixed the problem.

IMPORTANT: For now, you may want to cancel your cron job otherwise
it will delete all the mail every time it runs. This
will drive you crazy, particularly if it happens during troubleshooting.

2. Make sure you have both your MANAGE and LIST accounts set up
on your mail server.
Make sure your lib/config.php is pointing to these
accounts.
Reply With Quote
  #3 (permalink)  
Old December 3rd, 2009, 06:40 PM
Authorized User
 
Join Date: Dec 2009
Location: Inverness, Scotland
Posts: 16
Thanks: 0
Thanked 1 Time in 1 Post
Unhappy

Hi Kenji...

I've done this and this is the result (there are three dummy emails in the emaol inbox):

Array (
[0] => Array
(
[0] => From:
[1] => From: "Daibhidh Baldwin"
[2] => Subject: SUBSCRIBE
)

[1] => Array
(
[0] =>
[1] => "Daibhidh Baldwin"
[2] =>
)

[2] => Array
(
[0] =>
[1] =>
[2] => SUBSCRIBE
)
)

As you can see, the 'subject' is cropping up in [2][2] and the 'from' in [1][1]...but there's not actually an email address...this is quite different to your results...

I've also noticed, and this didn't seem to be happening last night, the the message data doesn't seem to have an email address in it!!! It is as follows:

---
X-Envelope-From: X-Envelope-To: X-Delivery-Time: 1259876410 X-UID: 30 Return-Path: X-RZG-CLASS-ID: mi Received: from mo-p01-ob.rzone.de ([81.169.146.166]) by mailin.webmailer.de (plinge mi3) (RZmta 22.5) with ESMTP id 00442alB3LYiGD for ; Thu, 3 Dec 2009 22:40:10 +0100 (MET) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; t=1259876410; l=4355; s=domk; d=xxx.com; h=Content-Type:MIME-Version:Date:Subject:To:From:Reply-To: X-RZG-CLASS-ID:X-RZG-AUTH; bh=nC2A4ndyYij8l0enqc2Yvr+ACn4=; b=VTarBU326XmFbZzSBlsb26N+FeT92zij0s2gvFPlSb4atEzR NdmJzNMDpNZkMQsTQtE bmR052G4gJil6VQFXcctPDxS4KYzfmtRoEWgSMEoLP60rX/SOH5cj6UtS6BjWa6UdJaMO 6+WrtkepAaYXT5U3dW7OCxkMxopIKmn1jTc= X-RZG-AUTH: :KG0SfETmef7m1lwFQYKU/j/sfYrhYdCzpdBYochLllXXqDh1JjMhyQjKPkfrxS/uSVKJ6FBxuQ== X-RZG-CLASS-ID: mo01 Received: from sleaghach (88-109-78-219.dynamic.dsl.as9105.com [88.109.78.219]) by post.strato.de (mrclete mo62) (RZmta 22.5) with ESMTP id v05b20lB3IQFTB for ; Thu, 3 Dec 2009 22:40:09 +0100 (MET) Return-Receipt-To: "Daibhidh Baldwin" Reply-To: From: "Daibhidh Baldwin" To: Subject: SUBSCRIBE Date: Thu, 3 Dec 2009 21:40:08 -0000 Message-ID: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0012_01CA7461.2F799F70" X-Mailer: Microsoft Office Outlook 11 Thread-Index: Acp0YS8J8iOUC3bVTqy3CXIYPgIsqg== X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6001.18049 Disposition-Notification-To: "Daibhidh Baldwin" This is a multi-part message in MIME format. ------=_NextPart_000_0012_01CA7461.2F799F70 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Daibhidh Baldwin www.xxx.net | info@xxx.net NB. This email and any files transmitted are confidential and intended solely for the use of the individual or entity to which they are addressed. Any opinions or information expressed within this email, or documents attached, must not be distributed without the express permission of the owner. If you have received this email in error, please delete it and notify: info@xxx.net. ------=_NextPart_000_0012_01CA7461.2F799F70 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable
---

This is really confusing me...why is it different on my server to yours??

Many thanks :)

Daibhidh
Reply With Quote
  #4 (permalink)  
Old December 3rd, 2009, 06:46 PM
Authorized User
 
Join Date: Dec 2009
Location: Inverness, Scotland
Posts: 16
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quick update, on the basis of the above posts, I've changed:

Code:
$from = trim($matches[1][0]);
$subject = trim($matches[2][1]);
to

Code:
$from = trim($matches[1][1]);
$subject = trim($matches[2][2]);
...and the code now seems to work in manage.php which is great...but is this suppost to be how it works?!? Why is it different for my to you?

Now going to see if I can apply the same principles to individual.php

Do you know why this is not the same for me? Confused.com!

Many thanks,

Daibhidh :)
Reply With Quote
  #5 (permalink)  
Old December 3rd, 2009, 11:54 PM
Authorized User
Points: 374, Level: 6
Points: 374, Level: 6 Points: 374, Level: 6 Points: 374, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2009
Location: San Jose, Californina
Posts: 77
Thanks: 4
Thanked 6 Times in 6 Posts
Default

Hi daibhidh,

The problem is you picked up an extra From: in your message,

preg_match_all searches for From: with a space after it.
You have one right at the beginning of your message, like this.

X-Envelope-From: X-Envelope-To

The preg_match_all counts this as a match, this gives you 2 From
matches and a subject match, which is why your subject match got
pushed down 1 slot in the array from [2][1] to [2][2]

The real problem is that the From in X-Envelope-From: should
not be getting picked up in the first place. There is a fix.
Change the existing preg_match_all to make sure there is not
a dash in front of the From:

In other words, change
Code:
preg_match_all('/From: (.+)|Subject: (.+)/i', $message, $matches);
to this.
Code:
preg_match_all('/[^-]From: (.+)|Subject: (.+)/i', $message, $matches);
This worked for me on my yahoo messages, and also in a test program with
your message. Try it out.

I think this may be a bug.

X-Envelope-From is a common header name, so for what we are doing
we need to parse it off.

I hope this helps.
Reply With Quote
  #6 (permalink)  
Old December 4th, 2009, 06:12 AM
Authorized User
 
Join Date: Dec 2009
Location: Inverness, Scotland
Posts: 16
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Kenji....excellent...that fixed the problem with the manage.php file nicely and it all seems to work fine. I've tired subscribing/unsubscribing etc. from a variety of different email accountsand all seem to work fine...even though the actually email address doesn't seem to appear in the header, it is somehow being processed by the script and email up in the mysql database and is available to send an acknowledgement email to the subscriber etc...bizare, but works...

Next problem...!

In the individual.php file (and I'm guessing also the digest file - although I haven't tried it yet) I'm having the same problem as for the manage.php vis-s-vie what the preg_match_all command is throwing up and storing in $matches.

It seems to store things differently depending on what email account I'm sending an email to the list from...

Here's two examples (sorry they're so long):

Email from Outlook (via my host’ email server)…

X-Envelope-From: X-Envelope-To: X-Delivery-Time: 1259918615 X-UID: 41 Return-Path: X-RZG-CLASS-ID: mi Received: from mo-p01-ob.rzone.de ([81.169.146.166]) by mailin.webmailer.de (bertie mi44) (RZmta 22.5) with ESMTP id m04ec6lB499fyF for ; Fri, 4 Dec 2009 10:23:35 +0100 (MET) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; t=1259918614; l=4356; s=domk; d=xxx.com; h=Content-Type:MIME-Version:Date:Subject:To:From:Reply-To: X-RZG-CLASS-ID:X-RZG-AUTH; bh=f2OrrgzYRpoiA7h51jp/RTREY/o=; b=YQnQrnP4+POrTXPJu4pscY9CqapnvQ15fdOG836o6x2dfDlp EZtQ4leb5EnUcQoVQ/6 jJWyoExaZH7cPCHsoLP7C1oVO0XXQLo+UM9w9vCUERv08eHuau RYAPYXkC9XRswRqVc0w ZCMWpQmdz/Dy/Zwuyiiicbw7IlR2GFJCUdY= X-RZG-AUTH: :KG0SfETmef7m1lwFQYKU/j/sfYrhYdCzpdBYochLllXXqDh1JjMhyQjKPkfrxS/uSVKJ6FBxuQ== X-RZG-CLASS-ID: mo01 Received: from sleaghach (88-109-78-219.dynamic.dsl.as9105.com [88.109.78.219]) by post.strato.de (mrclete mo56) (RZmta 22.5) with ESMTP id 505571lB48hlb5 for ; Fri, 4 Dec 2009 10:23:34 +0100 (MET) Return-Receipt-To: "David Baldwin" Reply-To: From: "Daibhidh Baldwin" To: Subject: test Date: Fri, 4 Dec 2009 09:23:33 -0000 Message-ID: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_003F_01CA74C3.73DB5840" X-Mailer: Microsoft Office Outlook 11 Thread-Index: Acp0w3NMfML0WdIrRjalPz6nffMccg== X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6001.18049 Disposition-Notification-To: "Daibhidh Baldwin" This is a multi-part message in MIME format. ------=_NextPart_000_003F_01CA74C3.73DB5840 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Daibhidh Baldwin www.xxx.net | info@xxx.net NB. This email and any files transmitted are confidential and intended solely for the use of the individual or entity to which they are addressed. Any opinions or information expressed within this email, or documents attached, must not be distributed without the express permission of the owner. If you have received this email in error, please delete it and notify: info@xxx.net. ------=_NextPart_000_003F_01CA74C3.73DB5840 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable
Test content
------=_NextPart_000_003F_01CA74C3.73DB5840--

Array
(
[0] => Array
(
[0] => From:
[1] => From: "Daibhidh Baldwin"
[2] => Subject: test
[3] => Date: Fri, 4 Dec 2009 09:23:33 -0000
[4] => boundary="----=_NextPart_000_003F_01CA74C3.73DB5840"
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
[3] => Fri, 4 Dec 2009 09:23:33 -0000
[4] =>
)
[2] => Array
(
[0] =>
[1] => "Daibhidh Baldwin"
[2] =>
[3] =>
[4] =>
)
[3] => Array
(
[0] =>
[1] =>
[2] => test
[3] => [4] =>
)
[4] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => ----=_NextPart_000_003F_01CA74C3.73DB5840
)
)


Email from Hotmail…

X-Envelope-From: X-Envelope-To: X-Delivery-Time: 1259918849 X-UID: 42 Return-Path: X-RZG-CLASS-ID: mi Received: from snt0-omc2-s29.snt0.hotmail.com ([65.55.90.104]) by mailin.webmailer.de (bertie mi11) (RZmta 22.5) with ESMTP id Z0264clB49EfVn for ; Fri, 4 Dec 2009 10:27:28 +0100 (MET) Received: from SNT107-W41 ([65.55.90.71]) by snt0-omc2-s29.snt0.hotmail.com with Microsoft SMTPSVC(6.0.3790.3959); Fri, 4 Dec 2009 01:27:28 -0800 Message-ID: Content-Type: multipart/alternative; boundary="_a16801be-552a-4aff-8a6d-b44f86777121_" X-Originating-IP: [88.109.78.219] From: c m To: Subject: test Date: Fri, 4 Dec 2009 09:27:28 +0000 Importance: Normal MIME-Version: 1.0 X-OriginalArrivalTime: 04 Dec 2009 09:27:28.0350 (UTC) FILETIME=[FF42E7E0:01CA74C3] --_a16801be-552a-4aff-8a6d-b44f86777121_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable =20 __________________________________________________ _______________ Have more than one Hotmail account? Link them together to easily access bot= h http://clk.atdmt.com/UKM/go/186394591/direct/01/= --_a16801be-552a-4aff-8a6d-b44f86777121_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
________________________________________
Add other email accounts to Hotmail in 3 easy steps= .. Find out how. = --_a16801be-552a-4aff-8a6d-b44f86777121_--

Array
(
[0] => Array
(
[0] => From:
[1] => boundary="_a16801be-552a-4aff-8a6d-b44f86777121_"
[2] => From: c m
[3] => Subject: test
[4] => Date: Fri, 4 Dec 2009 09:27:28 +0000
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => Fri, 4 Dec 2009 09:27:28 +0000
)
[2] => Array
(
[0] =>
[1] =>
[2] => c m
[3] =>
[4] => )
[3] => Array
(
[0] =>
[1] =>
[2] =>
[3] => test
[4] =>
)
[4] => Array
(
[0] =>
[1] => _a16801be-552a-4aff-8a6d-b44f86777121_
[2] =>
[3] =>
[4] =>
)
)



Now, I've tried tinkering a little with pattern matching to try to weed out the problem as you did for manage.php, but my skills aren't really good enough and I don't quite seem to get it to work...

Again, getting a bit confused...just don't understand what's going on here...any ideas?! :)

Do you think this could be an issue with my host's email server? I had problems last year installing squirrelmail onto the hosting package. All seemed to work fine, but whe you click on a NEW email message for the first time, while it displayed the message in the end, preceeding the message was a whole load of waffle about a mis-formed message...wouldn't happen the next time you click to view the email, only the first...do you think there is an issue at my host's end with how their mail server deals with email?

Hmmmm...

Thanks again,

David

Last edited by daibhidh; December 4th, 2009 at 06:16 AM..
Reply With Quote
  #7 (permalink)  
Old December 4th, 2009, 06:39 AM
Authorized User
 
Join Date: Dec 2009
Location: Inverness, Scotland
Posts: 16
Thanks: 0
Thanked 1 Time in 1 Post
Default

I think I may have found a work-around for this problem...I was studying the code and it seemed to me that pre_match_all was struggling to process all of those requests at once given the diversity of message headers etc. between mail servers, so I broke them down into bite-sized pieces:

Code:
    preg_match_all('/Date: (.+)/', $message, $date_matches);
    preg_match_all('/From: (.+)/', $message, $from_matches);
    preg_match_all('/Subject: (.+)/', $message, $subject_matches);
    preg_match_all('/boundary="(.+)"/', $message, $boundary_matches);	
	
    $date = trim($date_matches[1][0]);       
    $from = trim($from_matches[1][1]);       
    $subject = trim($subject_matches[1][0]);    
    $boundary = (isset($boundary_matches[1][0])) ? $boundary_matches[1][0] : false;
...now it seems to work for all emails I send, processed through individual.php, no matter what account (e.g. hotmail, yahoo) they comes from...

Will try this for the digest file too...

Also, your fix for the manage.php file worked fine until I tried my work email, which is yet another mail server. It returned an error everytime I tried to subscribe. I change the code along the lines of the above, and now it seems to work without any problems...

Code:
    preg_match_all('/From: (.+)/i', $message, $from_matches);
    preg_match_all('/Subject: (.+)/i', $message, $subject_matches);
    $from = trim($from_matches[1][1]);
    $subject = trim($subject_matches[1][0]);
Do you think this is all just beginner's luck, or is it a proper fix?! :)

Daibhidh

Last edited by daibhidh; December 4th, 2009 at 06:55 AM..
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 2 To Do List errors louie2107 BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 5 March 23rd, 2011 08:56 AM
Chapter 9 Trying to send email out digink BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 6 March 5th, 2011 05:42 AM
Chapter 11 - Email HELP! Nyika1203 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 July 28th, 2008 06:45 PM
Chapter 15 - EMail Validation studentinpain BOOK: Beginning ASP 3.0 1 March 19th, 2004 07:33 AM



All times are GMT -4. The time now is 06:30 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.