php.ini & SMTP= - how do you pass username & password
PHP mail()
command does not support authentication. Your options:
- PHPMailer- Tutorial
- PEAR - Tutorial
- Custom functions - See various solutions in the notes section: http://php.net/manual/en/ref.mail.php
How to set smtp username and password using ini_set
As you may read in the PHP manual, the SMTP functionaliy for PHP is only available on Windows and it only has very basic functionality. If you need to use it on Linux and / or need username and password authentication, SMTPS, etc you will need to use libraries like SwiftMailer, PHP Mailer, etc. or you need to set up an external SMTP server on your own host like Exim.
You should, however, not attempt to set up an SMTP server unless you are experienced in such matters, or you will make your server a nest for spammers in a matter of days.
SMTP Authentication with PHP mail() function
Why don't you try the Pear Mail interface something like this:
require_once "Mail.php";
$username = 'user@gmail.com';
$password = 'password';
$smtpHost = 'ssl://smtp.gmail.com';
$smtpPort = '465';
$to = 'mail@to.com';
$from = 'user@gmail.com';
$subject = 'Contact Form';
$successMessage = 'Message successfully sent!';
$replyTo = '';
$name = '';
$body = '';
$headers = array(
'From' => $name . " <" . $from . ">",
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => $smtpHost,
'port' => $smtpPort,
'auth' => true,
'username' => $username,
'password' => $password
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo($successMessage);
}
More information at https://goo.gl/HjffYA
EDIT:
The only way to this without more coding or using external library, is to update sendmail:
Define SMTP Server
smtp_server=mail.mydomain.com
If you need to change the smtp and SSL ports
; smtp port (normally 25)
smtp_port=25
; SMTPS (SSL) support
; auto = use SSL for port 465, otherwise try to use TLS
; ssl = alway use SSL
; tls = always use TLS
; none = never try to use SSL
smtp_ssl=auto
And finally your authentication credentials for SMTP server:
auth_username=username
auth_password=password
Ref: http://php.net/manual/en/ref.mail.php
Is it possible to change the SMTP information in the php.ini file to point to Microsoft's Live email SMTP servers?
No, that won't work. smtp.live.com requires authentication, and the PHP mail() command does not support authentication. As Pascamel suggested, you should use a library that supports sending mail through a remote SMTP server using authentication. phpmailer is very good.
Mail.php & Smtp Authentication Issue
For sending mails, try PHPMailer, it's tested, everybody uses it, and it just works.
It also has a lot of features and configuration options.
The latest version is this one, as for sending mails using SMTP with PHPMailer this is all the code you need
// Data received from POST request
$name = stripcslashes($_POST['name']);
$emailAddr = stripcslashes($_POST['email']);
$issue = stripcslashes($_POST['issue']);
$comment = stripcslashes($_POST['message']);
$subject = stripcslashes($_POST['subject']);
// Send mail
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
// SMTP Configuration
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "myhost"; // SMTP server
$mail->Username = "yourusername@gmail.com";
$mail->Password = "yourpassword";
//$mail->Port = 465; // optional if you don't want to use the default
$mail->From = "my@email.com";
$mail->FromName = "My Name";
$mail->Subject = $subject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($issue . "<br /><br />" . $comment);
// Add as many as you want
$mail->AddAddress($emailAddr, $name);
// If you want to attach a file, relative path to it
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
$response= NULL;
if(!$mail->Send()) {
$response = "Mailer Error: " . $mail->ErrorInfo;
} else {
$response = "Message sent!";
}
$output = json_encode(array("response" => $response));
header('content-type: application/json; charset=utf-8');
echo($output);
phpmailer secure approach by placing credentials out of the web root in INI file
<?php
if (empty($_REQUEST['demail']) || empty($_REQUEST['dmessage'])) {
die();
}
$constants = parse_ini_file("/outside/web/sample.ini");
header('Content-type: text/plain; charset=utf-8');
$email = $_REQUEST['demail'] ;
$message = $_REQUEST['dmessage'] ;
$ddomain = $_REQUEST['ddomain'] ;
require("PHPMailer_v5.1/class.phpmailer.php");
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->Username = $constants['username']; // SMTP username
$mail->Password = $constants['password']; // SMTP password
$mail->From = $email;
$mail->AddAddress("info@example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $ddomain;
$mail->Body = $message;
$mail->AltBody = $ddomain;
if(!$mail->Send())
{
echo "Error <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "we have received your email";
?>
INI file (sample.ini) looks like:
username = "info@example.com"
password = "PASSW0RD"
Related Topics
What Are the Disadvantages of Using Persistent Connection in Pdo
How to Remove All Non Printable Characters in a String
Simple Jquery, PHP and Jsonp Example
Search With Comma-Separated Value MySQL
What Kind of String Is This? How to Unserialize This String
Why Is MySQLi Giving a "Commands Out of Sync" Error
Storing Files in Database VS File System
How to Test a Url For 404 in PHP
PHP If Statement With Multiple Conditions
How to Check If a Date Is in a Given Range
How to Remove Line Breaks (No Characters!) from the String
Issue Reading Http Request Body from a Json Post in PHP
How to Write SQL For a Table That Shares the Same Name as a Protected Keyword in MySQL
PHPexcel Runs Out of 256, 512 and Also 1024Mb of Ram