Question

I have some problems with sending and receiving emails with my code..

When I try this code on my localhost(XAMPP) it says: Email send, but when i look in my mail i didn't receive the mail.. can someone help? My code:

<?php

// $EmailFrom = "ME";
$EmailTo = "My_amail_adress";
$Subject = "My_subject";
$Name = stripslashes($_POST['Name']); 
$Email = stripslashes($_POST['Email']); 
$Message = stripslashes($_POST['Message']); 

//setup the email
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

$success = mail($EmailTo, $Subject, $Body); //"From: <$EmailFrom>"

if ($success) {
    echo "Email send";
}
else{
    echo "Error, please try again";
}

?>
Was it helpful?

Solution

Do you have SMTP server configured in your localhost ?

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.ini and c:\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 = my-gmail-id@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=my-gmail-id@gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id@gmail.com

Now you have done!! create php file with mail function and send mail from localhost.

PS: don't forgot to replace my-gmail-id and my-gmail-password in above code. Also, don't forget to remove duplicate keys if you copied settings from above. For example comment following line if there is another sendmail_path : sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe" in the php.ini file

OTHER TIPS

If you are working on localhost you can not send emails by default , you need to configure sendmail

Try this tutorial

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top