how to send smtp mail from localhost - php

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;

Related

Is there something wrong with this email function?

I have written a function to send a confirmation email for a hobby app I am working on. I thought I set everything up properly, but I am getting the following error when I call the function:
2019-05-08 02:43:30 Connection failed. Error #2: failed loading cafile stream: `C:\xampp\apache\bin\curl-ca-bundle.crt' [C:\Users\Michael\Desktop\InviteMe Application\vendor\phpmailer\src\SMTP.php line 405]
SMTP Error: Could not connect to SMTP host.
I have already set up my gmail account to allow less secure apps to access the account and I have a password generated.
I have tried reading the troubleshooting documentation for the library I am using but have not been able to find anything that addresses the specific problem I am having.
Here is the email function:
function send_confirmation_email($name, $email, $user_id) {
$confirmationUrl = generate_confirmation_url($user_id);
$body = "";
$body .= "<html><body>";
$body .="<h1>Confirm your account</h1>";
$body .= "<p>Follow the link below to confirm your account:</p>";
$body .= "<a href='" . $confirmationUrl . "'>confirm account</a>";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 3; //debugging purposes
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "invitemedemo#gmail.com";
$mail->Password = "password";
$mail->setFrom('invitemedemo#gmail.com', "InviteMe");
$mail->addAddress($email, $name);
$mail->Subject = "Confirm Account";
$mail->Body = $body;
$mail->isHTML(true);
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
return false;
} else {
return true;
}
}
Rather than the email being sent, I see the error I pasted above.
Validate your smtp email service.
If you have MAC OS, see how to enable postfix for Gmail SMTP using your gmail account at: https://gist.github.com/loziju/66d3f024e102704ff5222e54a4bfd50e
Using Ubuntu Linux reference: https://gist.github.com/adamstac/7462202
Check the PHP settings in the php.ini file for the active php version you are using.
i.e. Using MAMP for MAC OS, gt to Applications -> MAMP -> conf -> php7.0.32 (must be the active version you are using, so if you are using php 5.3.14, then go to that directory) ->php.ini
Edit the php.ini file with:
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25
; For Win32 only.
;sendmail_from = me#example.com
; For Unix only. You may supply arguments as well
(default: "sendmail -t -i").
;sendmail_path = sendmail -t -i -f example#gmail.com
sendmail_path = "env -i /usr/sbin/sendmail -t -I"
Restart apache2 after updating the php.ini file.
If using MAC OS to stop and restart mail service: sudo postfix stop && sudo postfix start sudo postfix reload
Test Email
Send a test email from the terminal CLI:
echo "Test sending email from Postfix" | mail -s "Test Postfix" example#gmail.com
Alternatively, I find I can send a test message from the terminal CLI using this:
echo "test message" | sendmail -v example#gmail.com
Check errors for sendmail use the terminal CLI command:
mailq
You can test the SMTP feature using php such as:
<?php
// testemail.php
$to_email = 'example#gmail.com';
$subject = 'Testing PHP Mail';
$message = 'This mail is sent using the PHP mail function';
$message = wordwrap($message, 70);
$headers = 'From: yoursending#email.com';
mail($to_email,$subject,$message,$headers);
$mail = mail($to_email,$subject,$message,$headers);
if ($mail) { echo "Thank you for using mail";}
else { echo "Mail sending failed.";}
ini_set('display_errors',1);
?>

XAMPP send mail not working 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

Getting SMTP error while using PHpmailer to send mail through my website

I am trying to send email from my website through PHP Mailer class in PHP.
The code is given below but when I am trying to send the email, I am getting these two errors:
SMTP Error: Could not connect to SMTP host. Message could not be sent.
Mailer Error: SMTP Error: Could not connect to SMTP host.
What is going wrong. 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("lib/PHPMailer/PHPMailerAutoload.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 = "send_from_myemail#host.com"; // SMTP username
$mail->Password = "password"; // 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("myemail#host.com", "namit pathak");
// 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!";
// $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";
?>
You need to include your email's username and password on this line, currently you are using the original dummy settings:
$mail->Username = "send_from_myemail#host.com"; // SMTP username
$mail->Password = "password";
you only put the host as localhost at a time you must configured your local smtp server
$mail->Host = "localhost";
if you have google accounts means try it
$mail->Host = "smtp.google.com";

Trying to test send emails on localhost -> SMTP Error: Could not authenticate

Im trying to test my emails functions in localhost with phpmailer class.
But I´m getting this error: SMTP Error: Could not authenticate..
And this error its because I dont have a localhost email configured.
I already did this configuration in my Xampp php.ini to try test emails on localhost:
[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
; SMTP = localhost
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = myemail#hotmail.com
But its not working, Somebody there knows How to do this work on windows 8 with xampp?
<?php
define('MAILUSER','myemail#hotmail.com');
define('MAILPASS','');
define('MAILPORT','587');
define('MAILHOST','smtp.live.com');
define('SITENAME', 'Site Name');
function sendMail($subject,$message,$sender,$senderName,$destination,$destinationName, $reply = NULL, $replyNome = NULL){
require_once('mail/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->IsHTML(true);
$mail->Host = MAILHOST;
$mail->Port = MAILPORT;
$mail->Username = MAILUSER;
$mail->Password = MAILPASS;
$mail->From = utf8_decode($sender);
$mail->FromName = utf8_decode($senderName);
if($reply != NULL){
$mail->AddReplyTo(utf8_decode($reply),utf8_decode($replyNome));
}
$mail->Subject = utf8_decode($assunto);
$mail->Body = utf8_decode($mensagem);
$mail->AddAddress(utf8_decode($destino),utf8_decode($destinationName));
if($mail->Send()){
return true;
}else{
return false;
}
}
?>
Here I send the email:
$msg = 'hello';
sendMail('Sent email!',$msg,MAILUSER,SITENAME,$assoc['email'],$assoc['name']);
Maybe your SMTP configuration needs to be updated? See this article about the Hotmail SMTP settings:
http://www.serversmtp.com/en/smtp-hotmail

SMTP Error on sending mail

I have been trying to send mail using wamp server in my laptop. The SMTP server is presented in online.
Here is my php code to send mail:
<?php
ini_set( 'SMTP', "mail.vickey1192.co.in" );
ini_set( 'smtp_port', 26 );
ini_set( 'sendmail_from', "admin#vickey1192.co.in" );
$to = "balavickey1192#gmail.com";
$subject = "Acknowledgement";
$message = "Thank you for registering with us<br>";
$from = "no-reply#vickey1192.co.in";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
I also set my php.ini file like this:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = mail.vickey1192.co.in
; http://php.net/smtp-port
smtp_port = 26
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = admin#vickey1192.co.in
This is the error I am getting:
Warning: mail() [function.mail]: SMTP server response: 550-Please turn on SMTP
Authentication in your mail client, or login to the 550-IMAP/POP3
server before sending your message. (vignesh-PC)
550-[115.118.170.201]:23328 is not permitted to relay through this
server 550 without authentication. in C:\wamp\www\mailtofunc.php on
line 12
What do I do now? Please help me guys...
I think its a problem with authentication. You need to add the SMTP user's username and password to the mail function to send the email.
//Using built in mail method
$mail = new PHPMailer();
$mail->Host = 'smtp.example.com'
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = 'your_username#example.com'; // a valid email here
$mail->Password = 'replace_with_your_password';
$mail->From = 'from#example.com';
$mail->AddReplyTo('from#example.com', 'Test');
$mail->FromName = 'Test SMTP';
$mail->AddAddress('test1#example.com', 'test2#example.com');
$mail->Subject = 'Test SMTP';
$mail->Body = 'Hello World';
$mail->Send();
You might be better off trying the PHP's Pear mail function, if you know how to use it.
//Using PEAR's mail function
<?php
include('Mail.php');
/* mail setup recipients, subject etc */
$recipients = "your_recipients#example.com";
$headers["From"] = "user#example.com";
$headers["To"] = "feedback#example.com";
$headers["Subject"] = "Some Subject";
$mailmsg = "Hello, This is a test.";
/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "smtp.example.com";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "smtpusername";
$smtpinfo["password"] = "smtpPassword";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
/* Ok send mail */
$mail_object->send($recipients, $headers, $mailmsg);
?>
Your mail server requires authentication (username + password) before it will accept email from you. It is suggesting you either provide it via your SMTP connection (using SMTP AUTH, hopefully with TLS), or that you do a technique called POP before SMTP where you first login and 'check' your mail which will cause a temporary whitelisting of your host so it can send mail for a brief while afterward.

Categories