开发者

How can I have an array of emails and make sure they all send?

I would like to be able to get an array of emails and make sure each email is sent. (e.g. Array > send each email > result) I kind of changed the question here because this is more important, plus I have 开发者_开发问答added a 50 rep. point. Codewise how can I do this?


Apart from still using the mail() function, you probably want to setup a cron job for sending out the mails. For spooling mail send jobs use a separate database table. Or if it's about some sort of mailing list functionality, then a simple recipient list will do.


If you just want to send out a bunch of the same email at once, you could call implode() on your array of emails to turn it into a string:

$to_string = implode(', ', $to_array);

Or, if you want to try something more complicated, you could use a foreach loop to cycle through each email and to keep track of successes and failures:

$success = array();
$failure = array();

foreach ($to_array as $to_email)
{
  if (mail($to_email, $subject, $message, ...))
    $success[] = $to_email;
  else
    $failure[] = $to_email;
}

I'm guessing your original question had to do with sending out all these emails every day or something without necessitating you hitting a button. If you have ssh access, see what happens if you type:

crontab -e

If you get some sort of error, you will have to speak with your system administrator about cron. If you get a file, then you can use cron. This is not a part of your current question, though, so I'll leave it.


The same way. You must have code that sends an email to an email address. Whether they are on the site or not, it is the same code. You just need to know their email address.

EDIT: If you are wondering how you would trigger the email to be sent, maybe you want to schedule it using a cron job, for example send an email every day at midnight.


This says it all, really: http://php.net/manual/en/function.mail.php

You just need an outgoing mail server installed (postfix, exim, sendmail)


Easy way to send an email:

$to = "usermail@test.com";
$from = "my_email@mydomain.com";
$subject = "Hello!";
$contents = "This is an test mail!";

mail($to, $subject, $contents, "From: $from");


If you don't have access to cron jobs then you will probably struggle to run without user interaction.

A common method for dealing with this is to run on every nth page load, or every so-often. This only works if you have a site that's visited about as often as you want to send email. You'll also want to use an ACID-compliant database. Pseudo-code follows.

if (1 == rand(1,100)) { // run once every 100 page loads
    $emails = get_emails_to_send();
    mark_emails_as_sent($emails);
    $results = send_emails($emails);
    mark_failures_as_needing_to_be_sent($results);
}

Alternately, you can run it on a timer:

if (time() - get_last_time_run() > $run_at_least_once_every_this_many_seconds) {
    $emails = get_emails_to_send();
    mark_emails_as_sent($emails);
    $results = send_emails($emails);
    mark_failures_as_needing_to_be_sent($results);
}

Or you can combine both with a &&. This depends on how often your page gets hit.

If you want to send email more often than you get page hits... too bad ;). Or have an external service call the page every so often.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜