1

I have 2 mail functions, like this:

mail( $from, '', 'mail1', $headers );
mail( $from, '', 'mail2', $headers );

If I test it with my own email address in the $from, sometimes I receive mail1 first and sometimes I receive mail2 first. I want to always receive mail1 first. I tried putting a usleep in between the 2 mail functions, but it didn't work.

user617123
  • 463
  • 2
  • 9
  • 26
  • Are you sure that this isn't just happening due to random latency and delays in the mail delivery servers? – Jazz Apr 16 '12 at 20:21
  • Are the addresses on two different domains? There's a lot more to this than just the order of the mail calls. – Cfreak Apr 16 '12 at 20:21

2 Answers2

6

You can't. The order in which you receive mails isn't up to you. Mail may take many paths, and be delayed at any SMTP server along the way.

The best you could do would be to forge the date headers, making one e-mail appear to be sent a minute earlier than the other. Most mail clients will order them by date sent. Be cautious of this though, and don't go too far with it. Spammers use the same tactics, and e-mail providers know this.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • 2
    It's the same with the postal service.. just b/c you drop one letter in the box before another doesn't mean the postal carrier will deliver it in the same order. – Mike B Apr 16 '12 at 20:21
  • 2
    @MikeB, I suppose we could always encapsulate regular postal mail, writing seq numbers on the envelopes, so that we could re-order them upon reception. If anything got lost, we could always mail a NACK envelope back. :-D – Brad Apr 16 '12 at 20:25
  • I've had issues in other programming languages where delay functions didn't operate as expected because everything was on the same thread or whatever. I thought something similar might be happening here. Guess not. I was able to achieve desired affect almost 100% of the time just by upping the usleep value. – user617123 Apr 16 '12 at 20:51
1

After your mail function executes, the actual delivery of the email is left to Mailservers. That is, you cannot control which comes first to the inbox after releasing control of the email from your server/PHP code.

Read up on the Mail Transfer Agents and how they operate.

PenguinCoder
  • 4,335
  • 1
  • 26
  • 37