Yes, but if the whole reason for talking directly to the mailserver is to avoid the overhead of calling mail() each time, let's make that explicit in the pseudocode:
$query="select * from table where order is ready for sending";
if (there is at least one result)
{
#open socket to smtp server, say HELO
while ($row=mysql_fetch_array())
{
# create email text.
# send email
## consisting of
## MAIL FROM: ttttt
## RCPT TO: ttttt
## DATA
## the body of the mail,
## making sure you don't have a bare '.' line
## . on a line by itself to terminate the data
## but DON'T exit from the mailserver yet
}
#disconnect from smtp server
}