开发者

PHP Send HTML Mail generated by Loop

I am pretty lost here.

I need to know if there is a way to send a PHP email with HTML contents generated by a loop (mySQL loop I think)

I do not have any code right now because I am still looking for a way to achieve this.

In few words I need to send an HTML email by a PHP code.

This HTML mail will contain some static contents.

Some开发者_如何学JAVA other contents will be instead taken from mySQL.

The problem is that I have some tables that could need to be duplicated for each content I have to send.

I will explain.

My users can submit a form.

This form is to provide the details of 1 or MORE authors.

When they submit the form all the details go to the Database.

If they enter the details for more authors, each author will have its own database table.

Now when there are more authors an HTML email should be sent including all the info for EACH author they have entered the info for.

Since the HTML mail has been made by simple HTML tables, this is the structure that the HTML mail will have to assume.

1° Table for 1° Author

2° Table for 2° Author

3° Table for 3° Author

etc..

Now my question is simple. Is this possible by simply using a mySQL loop?

If yes how?

Could you provide a very basic sample to let me understand?

Or should I use something else?

Thank you


I think you should consider a different DB layout, which also would kinda solve that problem: Instead of one table per author (which you should never do) create on table with all the authors and one "content-table" which stores basicaly the same information as your current "auther-tables", but each row is linked with a foreign key to an author in the authors-table. Then you can SELECT, ORDER and GROUP BY authors with a simple join and easily get all data with one query. With the result of that query you can then easily create your HTML-Tables in PHP:

If you execute the following query

SELECT a.author_name AS name, c.content_title AS title
    FROM content c JOIN author a ON a.author_id=c.author_id
    ORDER BY a.author_name

you can do something like that in PHP:

if(count($result) > 0)
{
    $currentName = $result[0]['name'];
    $tables = "<table><tr><th>" . $currentName . "</th></tr>";
    foreach($result as $row)
    {
        if($currentName != $row['name'])
        {
            $currentName = $row['name'];
            $table .= "</table><table><tr><th>" . $currentName . "</th></tr>";
        }
        $table .= "<tr><td>" . $row['title'] . "</td></tr>";
    }
    $table .= "</table>";
}


yes u can send html content through mails. phpmailer it contains code for mailing, it is configurable. built html content and pass it to body. It works. here is some of tutorials for phpmailer http://www.ineedtutorials.com/code/php/sending-emails-with-php-php-tutorial http://www.learnphp-tutorial.com/Email.cfm

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜