How do I set up PHP to send email on Apache? (Windows)

36,718

After further searching and testing, it appears that:

  1. On Windows, PHP's mail() function doesn't necessarily need to hand email to another program on the same computer; it can connect to a mail server by itself

  2. This, however, is inefficient and slow.

  3. Setting up fake sendmail on the server machine and pointing php.ini's sendmail_path directive to it makes sending email much faster. Personally, I imagine sendmail.exe chewing up the data into nice, digestible bits, and spitting into the mail server's mouth like a mother bird feeding her baby. Mmmm! But maybe I'm crazy.

  4. If you put sendmail.exe into C:\usr\bin, this allows some programs that expect to be run on a Unix-based OS to run on Windows

Here is some info in the PHP manual that helped me work this out.

Note: The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).

And...

Note: It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient. For the sending of large amounts of email, see the » PEAR::Mail, and » PEAR::Mail_Queue packages.

Share:
36,718
resotpvl
Author by

resotpvl

I code mostly in Ruby and Elixir. More about me at nathanmlong.com and Stackoverflow Careers.

Updated on September 17, 2022

Comments

  • resotpvl
    resotpvl over 1 year

    I am setting up an Apache server (2.2.14) on Windows, and borrowing ideas from an existing XAMPP configuration. I am now trying to set up email functionality for PHP (5.3.1). (It needs to be able to handle attachments, just like the XAMPP server can.)

    I've noticed that XAMPP comes with fake sendmail for Windows, but several things about the configuration don't make sense to me.

    Here are a couple of lines from XAMPP's php.ini:

    ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    ;sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
    

    This is weird because:

    • It explicitly says "for Unix only," yet
      • I keep seeing tutorials that say to use this line for Windows, and
      • the example file path wouldn't even make sense in Unix
    • sendmail_path is commented out by default

    I wondered if sendmail.exe is even necessary. So on my custom Apache server, which doesn't have sendmail.exe, I edited php.ini to add the correct information for SMTP, smtp_port, and sendmail_from. With that, it was able to send mail! But it's very slow.

    Then I went back to the XAMPP server and renamed the sendmail directory, hoping to disable sendmail. That sort of worked - my PHP script claimed to have sent, but nothing happened.

    So I'm very confused.

    • My XAMPP server has sendmail_path commented out, but apparently still uses the program it points to. How does it know?
    • My Apache server has nothing in sendmail_path, but can somehow send mail without it. How does it do that?

    What do I actually need to do to configure the Apache server to send emails?

    • Scott Alan Miller
      Scott Alan Miller over 14 years
      So what platform are you trying to use now? Is it Windows or UNIX currently?
    • resotpvl
      resotpvl over 14 years
      Windows - sorry, I should have said. Editing question.
  • resotpvl
    resotpvl over 14 years
    MTA = 'mail transfer agent?' I know about sendmail_path; I wrote a whole paragraph about it and asked for some explanation.
  • Scott Alan Miller
    Scott Alan Miller over 14 years
    Thought that you were looking at a different variable in a package rather than in php.ini
  • François Breton
    François Breton over 5 years
    Still the best (only ?) way to email with php mail(). Sad that the fake sendmail.exe isn't developed anymore but it support TLS so its fine. I just wanted to say to exclude the folder from the anti-virus else it will be REALLY long to send an attachment.