Sending 2 parts of a body with Swiftmailer?

I have an email that is being sent to users. They receive the mail every time something is happening. Now I’m planning to make something, so that users can select if they only want these emails once a week. Let’s say it is like a forum. You write your thing and then you get an email as soon as someone is writing something in your thread.
But, if you want to get these emails once a week I would like to add them all into one single email.

So, my plan is to have one header (with my logo, all the info and also an option to read the e-mail in your browser). This part includes a unique code in the browser link (this is for statistics).
So, Im thinking about making the header as a web page (which is what I use for the regular emails). So there I set up the text and the link to use for reading in the browser. This part I like to include in my email as

$message1 = file_get_contents('http://www.webpage.com/newsletter_7_1.php?'.$links);

Then I make the same thing with the footer (which includes unique links to unsubscribe and update info). This is also something I like to put on a separate page and import it here as

$message3 = file_get_contents('http://www.webpage.com/newsletter_7_3.php?'.$links);

Then it’s time for the body part. This is built as a web page as well. Every thread looks different. So they have more like a newsletter style.
I want to include this (with all the links and images and whatever) as

$message2 = file_get_contents('http://www.webpage.com/newsletter_7_2.php?'.$links);

Then put together all these parts to

$message = $message1.$message2.$message3;

But before I do this, I want it to loop in my table to get the correct body parts and put them together one by one until it’s time for the final footer.
So it might be something like this: header,body1,body2,body3,footer. Or if just two things happened the last week: header,body1,body2,footer.

I have a table with the e-mail and things about every part and if they want an email weekly send_weekly is set to 7.
So first I select my table to get the stuff:

"SELECT * FROM to_send_table WHERE sent <> 1 AND send_weekly ='7' LIMIT 0,50"

But I guess I need to find out the number of body parts the actual user has. But I don’t know how to make this work for me.

This is parts of what I’ve got:


$to = $row['email'];
$subject = $row['subject'];
//Then I have all the links to the unique codes.

// And then the Swift mailer parts
$mailer = Swift_Mailer::newInstance($transport);

Where do I put the loops? And, should there be a loop inside one loop to first set the header for the user and then a loop selecting all the body parts for the specific user before going to the footer and after that run the loop again to see if there are more users marked with weekly emails?

I’m just stuck here. But the original emails are working fine.

In pseudo code:


users = selectAllUsersThatWantAWeeklyUpdate();
for (user in users)
{
    body = 'your email header here'
    threads = selectThreadsRelevantForUserForLastWeek(user)
    for (thread in threads)
    {
         body = body + getEmailSummaryForThread(thread)
    }
    body = body + 'your footer here'
    email = new Email(body)
    email->send(user)
}

Very abstract, but it should give you some initial ideas to go by - hopefully :slight_smile:

Maybe I’m too much of a beginner in php, but what are these?

for (user in users)

and

for (thread in threads)

Never seen these in a loop with the word in.

Will that one count the amount of hits?

It’s not PHP, it’s psuedo code. In PHP it would be foreach ($users as $user) and foreach ($threads as $thread)