[thelist] Sending e-mails

Liam Delahunty liam at megaproducts.co.uk
Mon Nov 21 07:19:46 CST 2005


On 21/11/05, Tim Burgan <email at timburgan.com> wrote:
>
>  From what I understand (others may be able to assist more): I'd write a
> PHP script where you input all the email addresses you want to send to,
> the subject, message and so on. Then when you click send, it adds that
> data to a database and doesn't send any email. Then have a second PHP
> file that just sits on the web server that none of your pages link to.
> This PHP script (called by the CRON jobs every X minutes or hours, or
> whatever) connects to the database, sees what emails in the queue to
> send and sends 20. Then the PHP script waits for the CRON job to open it
> again and repeats the process.
>

Where you can't use a cron job, you can have a very simple redirect
and send one at a time. Here follows a simple overview of the function
with the important sql.

So, you have a table of who's getting the mail, and a table that
tracks who it has been sent to.

The sending page is called with the newsletter id (this_nid), and you
check the second table to see who's NOT had it. In this case
email_style is a numeric indicating plain or HTML and we write the
appropriate headers for the newsletter. In this case the customer has
stated that they want plain or html.

SELECT c.id, first_name, email
FROM contact_tbl c
LEFT OUTER JOIN newsletters_contacts_tbl nct ON nct.contact_id =c.id
AND newsletter_id = '$this_nid'
WHERE nct.contact_id IS NULL
AND newsletter = '$email_style'
GROUP BY email
LIMIT 1

// send one with the right headers
if ($email_style == "1"){
  // insert plain text header.
  $header = "From: $shop_name <$email_shop>\r\n";
  $email_body = html_entity_decode($email_body);
}elseif ($email_style == "2"){
  // INSERT HTML MIME HEADERS
  $header = "From: $shop_name <$email_shop>\r\n" .
  "MIME-Version: 1.0\r\n" .
  "Content-type: text/html; charset=iso-8859-1\r\n";
}

$header .= "Return-path: <$email_shop>\r\n";  // Return path for errors
$header .= "Reply-To: $email_shop\r\n";
$header .= "X-Mailer: Britstream.com/";

//send it however you want...


// Write to database,
INTO newsletters_contacts_tbl (newsletter_id, contact_id, sent_date)
VALUES ('$newsletter_id', '$uid', NOW());


and then reload the page
<script type="text/javascript">
window.location.reload(true);
</script>

it'll happily loop around for hours and send the mail to everyone on
the list, one at a time


--
Kind regards,
Liam Delahunty
http://www.liamdelahunty.com/tips/



More information about the thelist mailing list