71

My ISP account requires that I send a username & password for outbound SMTP mail.

How do I get PHP to use this when executing php.mail()? The php.ini file only contains entries for the server (SMTP= ) and From: (sendmail_from= ).

1

10 Answers 10

40

PHP mail() command does not support authentication. Your options:

  1. PHPMailer- Tutorial
  2. PEAR - Tutorial
  3. Custom functions - See various solutions in the notes section: http://php.net/manual/en/ref.mail.php
4
  • 10
    Worth noting that PHP mail() does support authentication if (a) sendmail is installed on the server and (b) authentication details are provided in the php.ini file. See answers below for details. Commented Apr 11, 2014 at 14:44
  • @JamesMcCormack, if PHP only supports authentication in these two conditions, how does PHPMailer work? I it is just PHP and it allows the definition of authentication details in code.
    – dxvargas
    Commented Jan 28, 2015 at 6:41
  • @hiphip James McCormack is referring to PHP mail(), not to PHPMailer
    – mastazi
    Commented Jan 14, 2016 at 23:23
  • To make mail() support authentication, use hMailServer. It was 5 minutes for me to set it up. See my answer: stackoverflow.com/a/34818989/1293492 Commented Jan 15, 2016 at 21:37
38

I apply following details on php.ini file. its works fine.

SMTP = smtp.example.com
smtp_port = 25
username = [email protected]
password = yourmailpassord
sendmail_from = [email protected]

These details are same as on outlook settings.

7
  • 1
    Should it work using port 465 as well, if the SMTP server is using that port? Commented Jan 14, 2016 at 18:23
  • Ya. Any port you can specify.
    – sugunan
    Commented Jan 18, 2016 at 5:03
  • 3
    Did not work for me on PHP 5.6.20: Warning: mail(): SMTP server response: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
    – djk
    Commented May 9, 2016 at 12:49
  • 12
    Doesn't work for me and haven't been able to find any documentation whatsoever that would indicate that username or password are valid php.ini directives
    – wedstrom
    Commented Jun 6, 2016 at 22:10
  • @wedstrom After enter this did you get any error while reboot apache?
    – sugunan
    Commented Jun 9, 2016 at 12:49
21

Use Fake sendmail for Windows to send mail.

  1. Create a folder named sendmail in C:\wamp\.
  2. Extract these 4 files in sendmail folder: sendmail.exe, libeay32.dll, ssleay32.dll and sendmail.ini.
  3. Then configure C:\wamp\sendmail\sendmail.ini:
smtp_server=smtp.gmail.com
smtp_port=465
[email protected]
auth_password=your_password
  1. The above will work against a Gmail account. And then configure php.ini:

    sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"

  2. Now, restart Apache, and that is basically all you need to do.

3
  • 1
    It doesn't work for me. Windows 10, WampServer 2.5, PHP 5.5.12. Commented Jan 14, 2016 at 18:21
  • Thanks!!! This worked for me. After like a week of trying to set up my own SMTP server I got it bc of your answer, thanks really <3 Commented Feb 18, 2020 at 17:52
  • Use the latest sendmail for Windows and follow the basic installation instructions, make sure to remove the smtp configuration from your php.ini file because that isn't really required if you already handle those values in your sendmail.ini file. Also I was only able to get it to work if I use sendmail_path = "C:\Program Files (x86)\sendmail\sendmail.exe" without the -t parameter. If anyone is curious, I was using IIS on Windows 10.
    – haZh
    Commented Oct 17, 2020 at 13:54
15

PHP does have authentication on the mail-command for windows only!

The following is working for me on WAMPSERVER (windows, php 5.2.17)

php.ini

[mail function]
; For Win32 only.
SMTP = mail.yourserver.com
smtp_port = 25
auth_username = smtp-username
auth_password = smtp-password
sendmail_from = [email protected]
7
  • 2
    This is not working for me, PHP 5.3.8 on IIS 6. I had a look at the source code and it seems that smtp authentication is actually not implemented.
    – Zmaster
    Commented Oct 15, 2011 at 9:33
  • 3
    Doesn't work for me, I think you are using sendmail instead of smtp.
    – Draex_
    Commented Nov 29, 2012 at 22:59
  • 1
    Agreed - on Windows, this answer only works if "Fake Sendmail" is installed on the server. It can be downloaded from: glob.com.au/sendmail Commented Apr 11, 2014 at 14:41
  • Works on W8.1, PHP 5.2.17, using a remote SMTP server on port 587.
    – Stphane
    Commented Aug 19, 2015 at 15:20
  • It doesn't work for me using port 465. Windows 10, WampServer 2.5, PHP 5.5.12. Commented Jan 14, 2016 at 18:24
11
  1. Install Postfix (Sendmail-compatible).
  2. Edit /etc/postfix/main.cf to read:
#Relay config
relayhost = smtp.server.net
smtp_use_tls=yes
smtp_sasl_auth_enable=yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_sasl_security_options = noanonymous
  1. Create /etc/postfix/sasl_passwd, enter:
smtp.server.net username:password
  1. Type # /usr/sbin/postmap sasl_passwd

  2. Then run: service postfix reload

Now PHP will run mail as usual with the sendmail -t -i command and Postfix will intercept it and relay it to your SMTP server that you provided.

5

I prefer the PHPMailer tool as it doesn't require PEAR. But either way, you have a misunderstanding: you don't want a PHP-server-wide setting for the SMTP user and password. This should be a per-app (or per-page) setting. If you want to use the same account across different PHP pages, add it to some kind of settings.php file.

2
  • How to add username and password in a php file. Can I use this for sending mail using PHP mail function through developer.postmarkapp.com/developer-smtp.html Postmark
    – SCC
    Commented May 8, 2014 at 7:45
  • Some other popular apps may depend on PHPMailer, like WordPress internally, which may in turn be configured to use the internal mail() function to send the actual email. Therefore, PHP-wide configuration would be preferred. Commented Jul 3, 2014 at 3:26
5

After working all day on this, I finally found a solution. Here's how I send from Windows XP with WAMP.

  1. Use Google's SMTP server. You probably need an account.
  2. Download and install Fake Sendmail. I just downloaded it, unzipped it and put it in the WAMP folder.
  3. Create a test PHP file. See below.
<?php
    $message = "test message body";
    $result = mail('[email protected]', 'message subject', $message);
    echo "result: $result";
?>
  1. Update your php.ini file and your sendmail.ini file (sendmail.ini is in the sendmail folder).
  2. Check the error.log file in the sendmail folder that you just created if it doesn't work.

Reference:

1
  • It doesn't work for me. Windows 10, WampServer 2.5, PHP 5.5.12. Commented Jan 14, 2016 at 18:25
1

Use Mail::factory in the Mail PEAR package. Example.

0
1

These answers are outdated and deprecated. The actual best practice would be:

composer require phpmailer/phpmailer

As the next step, your sendmail.php file just requires the following:

# use namespace
use PHPMailer\PHPMailer\PHPMailer;

# require php mailer
require_once "../vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer;

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}

This can be configured however you like.

0
  1. Install the latest hMailServer. "Run hMailServer Administrator" in the last step.
  2. Connect to "localhost".
  3. "Add domain..."
  4. Set "127.0.0.1." as the "Domain", click "Save".
  5. "Settings" > "Protocols" > "SMTP" > "Delivery of e-mail"
  6. Set "localhost" as the "Local host name", provide your data in the "SMTP Relayer" section, click "Save".
  7. "Settings" > "Advanced" > "IP Ranges" > "My Computer"
  8. Disable the "External to external e-mail addresses" checkbox in the "Require SMTP authentication" group.
  9. If you have modified php.ini, rewrite these 3 values:

"SMTP = localhost",

"smtp_port = 25",

"; sendmail_path = ".

Credit: How to configure WAMP (localhost) to send email using Gmail?

2
  • This works perfectly on Windows 10, using WampServer 2.5, with PHP 5.5.12. Commented Jan 16, 2016 at 19:23
  • Hi @Tamas In SMTP Relayer I set up my credentials from AWS SES host name, port, username and password. But it is not working fine. Do you know if i should do something else ?
    – Zenit
    Commented Mar 2, 2019 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.