XAMPP send mail not working PHP - php

here are the details
[php.ini]
SMTP = smtp.gmail.com
smtp_port = 465
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
mail.add_x_header=Off
[sendmail.ini]
smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=ssl
error_logfile=error.log
debug_logfile=debug.log
auth_username=[email]#gmail.com
auth_password=[email password]
pop3_server=
pop3_username=
pop3_password=
force_sender=[email]#gmail.com
force_recipient=
hostname=smtp.gmail.com
[mail function]
mail('[email]#gmail.com','sample mail','sample content','From: anotheremail#gmail.com');
i have checked smtp settings properly, but its still not working please suggest.

php.ini
[mail function]
; For Win32 only.
; http://php.net/smtp
;SMTP=localhost
; http://php.net/smtp-port
;smtp_port=25
sendmail_path = C:/xampp/sendmail/sendmail.exe -t
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me#example.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail().
;mail.force_extra_parameters =
; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header=On
; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
;mail.log =
; Log mail to syslog (Event Log on Windows).
;mail.log = syslog
Working For me.

You have to configure SMTP on your server
You can use the googles free SMTP server Pretty easy to setup too.
<?php
$mail = new PHPMailer(true);
//Send mail using gmail
if($send_using_gmail){
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "your-gmail-account#gmail.com"; // GMAIL username
$mail->Password = "your-gmail-password"; // GMAIL password
}
//Typical mail data
$mail->AddAddress($email, $name);
$mail->SetFrom($email_from, $name_from);
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";
try{
$mail->Send();
echo "Success!";
} catch(Exception $e){
//Something went bad
echo "Fail :(";
}
?>
You can try below as well.
You can send mail from localhost with sendmail package , sendmail package is inbuild in XAMPP. So if you are using XAMPP then you can easily send mail from localhost.
for example you can configure
C:\xampp\php\php.iniandc:\xampp\sendmail\sendmail.ini for gmail to send mail.
in C:\xampp\php\php.ini find extension=php_openssl.dll and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.
in php.ini file find [mail function] and change
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = test#gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Now Open C:\xampp\sendmail\sendmail.ini. Replace all the existing code in sendmail.ini with following code
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=test#gmail.com
auth_password=my-gmail-password
force_sender=test#gmail.com
//mail function
$mail=test#test.com
mail($mail, 'Subject', 'sample mail', 'From: test#axxx.ba');

You should use PHPMailer instead:
download it from here
copy the PHPMailerAutoload.php from the downloaded Zip folder & paste into your directory where you've your contact us page
Then paste the following code in your contact page script:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Don't forget to change the dummy values

For those who are using Windows and XAMPP, you have to replace the backslash \ with / on your php.ini file. A sample value could be
sendmail_path=C:/xampp/sendmail/sendmail.exe -t -i
https://dzone.com/articles/how-send-emails-php-windows

Related

Mail() problem, the mail that i send from php doesnt arrive

I'm trying to send emails with php in local, i have modified php.ini and sendmail.ini but still still doesnt work.
The IDE doesn't even give me Errors or a Warnings like Failed to connect to mailserver at "localhost" port 25 or something like that
php.ini
SMTP=smtp.gmail.com
smtp_port=465
sendmail_from = biagiosani2005#gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
and sendmail.ini
smtp_server=smtp.gmail.com
smtp_port=465
error_logfile=error.log
debug_logfile=debug.log
auth_username=biagiosani2005#gmail.com
auth_password=xxxxxxxx
force_sender=biagiosani2005#gmail.com
<?php
$to = "luciasani2005#gmail.com";
$subject = "this is a subject";
$message = "this is a message";
mail($to, $subject, $message);
?>
It is not SMTP send mail. You have SMTP configurations, it is okay, but your sending way is wrong. You need to use PHPMailer package and to correct script such:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require './PHPMailer-master/src/Exception.php';
require './PHPMailer-master/src/PHPMailer.php';
require './PHPMailer-master/src/SMTP.php';
$html = "<h1>Hello </h1>";
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->Host = 'smtp.titan.email';
$mail->SMTPAuth = true;
$mail->Username = 'contact#domain.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
//Recipients
$mail->setFrom('contact#domain.com', 'domain Team');
$mail->addAddress('receiver1#gmail.com', 'Joe User');
$mail->addAddress('receiver2#gmail.com', 'Joe User');
// Attachments
// Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = $html;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo true;
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

PHP mailer smtp setup with gmail

Hello guys I have stuck with this problem for a while now. I am trying to send mail from xampp. I have went through several solutions posted here but none of them seem to work still. kind of need someone to point me to the right direction.
so in my php.ini
[mail function]
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = user#gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
I also have the sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t" commented with a ;
now in my sendmail.ini
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=user#gmail.com
auth_password=123456
force_sender=shadidhaque2014#gmail.com
P.s: I only have the above code in sendmail.ini
now for the php script I have something very simple:
$send=mail('rzs2009#yahoo.com', 'subject', 'blah blah blah');
if ($send) {
# code..
echo "yes";
}else
{
echo "no";
}
now everytime I try to run the program I get a no. So no email is being sent. Where might I be going wrong.
Thanks in advance.
Modify your code like this:
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/

how to send smtp mail from localhost

I always get this message when trying to send email from my local host.
SMTP Error: Could not connect to SMTP host. Message could not be sent.
Mailer Error: SMTP Error: Could not connect to SMTP host.
below is my code: please help
<?php
// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require("class.phpmailer.php");
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_PHPMailer#bradm.inmotiontesting.com
// pass: password
$mail->Username = "project#reliable.com.pk"; // SMTP username
$mail->Password = "Nov112014"; // SMTP password
// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("mani9418#gmail.com", "Usman Ali Siddiqui");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website Etutionhub!";
// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
Open the php.ini. For XAMPP, it is located in C:\XAMPP\php\php.ini. Find out if you are using WAMP or LAMP server.
Note: Make a backup of php.ini file.
Search [mail function] in the php.ini file.
You can find like below.
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster#localhost
Change the localhost to the smtp server name of your ISP. No need to change the smtp_port. Leave it as 25. Change sendmail_from from postmaster#localhost to your domain email address which will be used as from address.
So for me, it will become like this.
[mail function]
; For Win32 only.
SMTP = smtp.example.com
smtp_port = 25
; For Win32 only.
sendmail_from = info#example.com
Restart the XAMPP or WAMP(apache server) so that changes will start working.
Now try to send the mail using the mail() function.
mail("example#example.com","Success","Great, Localhost Mail works");
Mail will be sent to example#example.com from the localhost with Subject line "Success" and body "Great, Localhost Mail works".
You can add this line
$mail->SMTPSecure = "ssl";
after
$mail->SMTPAuth = true;

is mail() function of PHP encrypted already?

Please anybody tell me is mail() function of PHP is already encrypted or i have to make it encrypted my self, i want to make secure travel of email.
to use SSL or TLS with mail(), you will have to configure the "php.ini" (Require SSL on your server):
http://www.php.net/manual/en/mail.configuration.php
SMTP = YOUR_SERVER
smtp_port = SMTP_SECURE_PORT
sendmail_from = SEND_MAIL_FROM
sendmail_path = SEND_MAIL_APP
But if you change your server, you have to do it again, so I recommend instead of using mail() use SMTP, there are ready-made classes, see:
PHPMailer download: https://github.com/PHPMailer/PHPMailer
Example PHPMailer with TLS:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
The mail() function will either use the local mailer (Linux) or will connect to standard port 25 (Windows). There's absolutely no way to:
Make it use another port an encrypted channel
Provide authentication data
If security (or reliability) is a concern, avoid it at all cost.

how to send email from localhost "wamp" by php?

I try to send email from wampserver "localhost" by this php code
<?php
$to = 'test1#gmail.com';
$subject = 'Fake sendmail test';
$message = 'If we can read this, it means that our fake Sendmail setup works!';
$headers = 'From: test2#gmail.com' . "\r\n" .
'Reply-To: eng.jirjawi#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo 'Email sent successfully!';
} else {
die('Failure: Email was not sent!');
}
?>
I used the file "sendmail" and put it inside the file "C:\wamp\sendmail" i changed setting sendmail.ini file to -->
smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=ssl
default_domain=localhost
error_logfile=error.log
auth_username=test2#gmail.com
auth_password=xxxxxxx
pop3_server=
pop3_username=
pop3_password=
force_sender=
force_recipient=
hostname=localhost
i changed setting php.ini file in C:\wamp\bin\apache\apache2.2.22\bin
and
i changed setting php.ini file in C:\wamp\bin\php\php5.4.3
to
[mail function]
; For Win32 only.
; http://php.net/smtp
;SMTP =
; http://php.net/smtp-port
;smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you#yourdomain
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"
and i changed setting appach module "SSL module" checked
and i changed setting php extension "php openssl" & "php sockets"
And it did not work with the code did not send email
Discussed a lot in the internet and tried many ways
Why does not happen transmitter and what the correct way
Apologized for the mistakes my English language
Please help and thank you
PHPMailer has always worked for me on development servers (localhost), as well as on live websites.
Download PHPMailer here, unzip it and put it in your application's directory.
When you want to send a mail, use the following code :
require 'PHPMailerAutoload.php'; //Your path to PHPMailer's directory
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host = "smtp.gmail.com"; // Sets SMTP server for gmail
$Mail->SMTPDebug = 0; // 2 to enable SMTP debug information
$Mail->SMTPAuth = TRUE; // enable SMTP authentication
$Mail->SMTPSecure = "tls"; //Secure conection
$Mail->Port = 587; // set the SMTP port to gmail's port
$Mail->Username = 'yourusername#gmail.com'; // gmail account username
$Mail->Password = 'yourpassword'; // gmail account password
$Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet = 'UTF-8';
$Mail->Encoding = '8bit';
$Mail->Subject = 'Mail test';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From = 'test2#gmail.com'; //Your email adress (Gmail overwrites it anyway)
$Mail->FromName = 'Test';
$Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$Mail->addAddress('test1#gmail.com'); // To: test1#gmail.com
$Mail->isHTML( TRUE );
$Mail->Body = '<b>This is a test mail</b>';
$Mail->AltBody = 'This is a test mail';
$Mail->Send();
$Mail->SmtpClose();
if(!$Mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $Mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
Update : You should update your PHP installation for this to work correctly.
For the error you're getting, try this :
In your php.ini search for line
; extension=php_openssl.dll
and remove ; so it becomes:
extension=php_openssl.dll
salem alaykùm (hey)
check this it works for me,
<?php
if ($_SERVER["REQUEST_METHOD"] == 'POST'){
//print_r($_POST);
if (mail('name#example.com', 'New Test Email', htmlspecialchars($_POST['msg']))){
$cas = "thanks for the msg";
}
}
?>
<!doctype html>
<html>
<head> <title>my test company</title>
</head>
<body>
<h3>Contactez nous</h3>
<form action="" method="post">
<ul>
<li>
<label for="name">Name:</label>
<input type="text" name="name" id="name">
</li>
<li>
<label for="email">Email :</label>
<input type="text" name="email" id="email">
</li>
<li>
<label for="msg">Message:</label><br />
<textarea name="msg" id="msg"></textarea>
</li>
<li>
<input type="submit" value="Go!">
</li>
</ul>
</form>
<?php
if (isset($cas)) echo $cas;
?>
</body>
</html>

Categories