php.ini & SMTP= - how do you pass username & password

PhpSmtpEmail

Php Problem Overview


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= ).

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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.

Solution 3 - Php

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 > auth_username=[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.

Solution 4 - Php

PHP does have authentication on the mail-command!

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]

Solution 5 - Php

  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

3. Create /etc/postfix/sasl_passwd, enter:

smtp.server.net username:password

4. Type # /usr/sbin/postmap sasl_passwd

  1. 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.

Solution 6 - Php

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.

Solution 7 - Php

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.

> $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:

Solution 8 - Php

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.

Solution 9 - Php

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

Solution 10 - Php

  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: https://stackoverflow.com/questions/600725/how-to-configure-wamp-localhost-to-send-email-using-gmail/16814318#16814318

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCharles FaigaView Question on Stackoverflow
Solution 1 - PhpdaremonView Answer on Stackoverflow
Solution 2 - PhpsugunanView Answer on Stackoverflow
Solution 3 - PhpHenrik RosvallView Answer on Stackoverflow
Solution 4 - PhpblavlaView Answer on Stackoverflow
Solution 5 - PhpJay SudoView Answer on Stackoverflow
Solution 6 - PhpEric_WVGGView Answer on Stackoverflow
Solution 7 - PhpB SevenView Answer on Stackoverflow
Solution 8 - PhpCodedreamerView Answer on Stackoverflow
Solution 9 - PhpWilliam KellerView Answer on Stackoverflow
Solution 10 - PhpTamás BolváriView Answer on Stackoverflow