Page 1 of 1
Sending a replica of an existing email
Posted: Thu Feb 07, 2008 10:49 am
by TheAdmiralUK
Hi all,
OK, I'm trying a slightly odd thing.
Basically, I've already got a PHP class that opens up a pop3 mailbox and reads emails. I'm trying to use SwiftMailer to take those emails and send them to a list of addresses. So essentially, I want it to work like a mailinglist group... I send an email to an address, and my PHP app passes it on to many recipients.
But I've been running around for hours and tied myself in knots! I more-or-less want to take the raw email, and only replace the date it was sent, and the person it's being sent to. However as SwiftMailer is designed more around building email from scratch, I wonder whether I'm using the wrong tool for the job, really.
Anyway, If anyone has any thoughts, they'll be greatly appreciated!
-Ian
Re: Sending a replica of an existing email
Posted: Thu Feb 07, 2008 3:18 pm
by Chris Corbyn
The next version is due to have this feature. Unfortunately it requires parsing the message into Swift's format as soon as you want to change header fields. If you wanted to send it blindly with Swift (unchanged) the extending the Swift_Message class and overriding its build() to return the email (as a stream like Swift_Message does) will work.
v4 will have a far more elegant way of handling it though.
Re: Sending a replica of an existing email
Posted: Fri Feb 08, 2008 11:36 am
by TheAdmiralUK
Hi Chris,
Thanks for your reply... all hail version 4!
I tried your suggestion, but unfortunately I just got wrapped around my own head again trying to hack around with SwiftMailer source.
However, I did finally manage to do what I wanted to do!
Prepare yourself for some horrible coding.
Code: Select all
//Open connection
$smtp =& new Swift_Connection_SMTP("smtp.my-isp.com", 465, Swift_Connection_SMTP::ENC_SSL);
$swift =& new Swift($smtp);
//Now, run through each message we found in the pop3 mailbox.
//Each item in $messagearray is another array that contains 'Headers' (yet another array
// that contains each header and its value from the original email), and 'Body' (which is the
// undecoded original body of the message, with all MIME parts intact)
//Why am I using 'for' and not 'foreach'? Can't remember. I think there was some reason originally. I'll change it soon.
for ($i=0; $i < count($messagearray); $i++) {
//Create the new message
$message =& new Swift_Message();
//Set the body of the email to be exactly the same as the one we're copying
$message->setData($messagearray[$i]["Body"]);
//Now we're going to recreate the important parts of the headers: subject, MIME version, and content-type
$partheaders = new Swift_Message_Headers();
$partheaders->set("Subject",$messagearray[$i]["Headers"]["subject:"]);
$partheaders->set("Mime-Version",$messagearray[$i]["Headers"]["mime-version:"]); //Is this needed? No idea, but doesn't harm.
//The Content-Type is tricker because it is split into several parts.
$exploded_contenttype = explode(';',$messagearray[$i]["Headers"]["content-type:"]);
$partheaders->set("Content-Type",$exploded_contenttype[0]); //Set the main content type
//Now fill in the content type attributes from the rest of the $exploded_contenttype array
for ($part=1; $part < count($exploded_contenttype); $part++) {
$exploded_contentparts = explode('=',$exploded_contenttype[$part],2);
$partheaders->setAttribute("Content-Type", $exploded_contentparts[0], trim($exploded_contentparts[1],'"'));
}
//And finally, set our new headers.
$message->setHeaders($partheaders);
//Send the email. In the PHP app I'm developing, this will be sent to the members of a particular mailing list
$send_to = "someone@someone.co.uk";
if ($swift->send($message, $send_to, "me@me.com")){
echo "Sent to ".$send_to;
} else {
echo "Failed";
}
}
And it works! At least. As far as my testing shows, it works. Come to think of it, not sure I've tested just plain text yet... hmm...
But yeah, thanks again for the help. SwiftMailer is a great piece of software, and it's making my project possible!