0

I have created a new page contact us i'm getting trouble with this error encounters.

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in contact_me.php on line 41

My PHP file is

<?php
// check if fields passed are empty

if(empty($_POST['fname'])   ||
   empty($_POST['lname'])       ||
   empty($_POST['phone'])       ||
   empty($_POST['email'])      ||
   empty($_POST['dateTime'])     ||
   empty($_POST['heading'])     ||
   empty($_POST['companyName'])     ||
   empty($_POST['address'])     ||
   empty($_POST['postalCode'])     ||
   empty($_POST['city'])     ||
   empty($_POST['country'])     ||
   empty($_POST['message'])     ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$phone = $_POST['phone'];
$dateTime = $_POST['dateTime'];
$heading = $_POST['heading'];
$companyName = $_POST['companyName'];
$address = $_POST['address'];
$postalCode = $_POST['postalCode'];
$city = $_POST['city'];
$country = $_POST['country'];
$message = $_POST['message'];
$email_address = $_POST['email'];

// create email body and send it    
$to = 'ram@gmail.com'; // PUT YOUR EMAIL ADDRESS HERE
$email_subject = "Modern Business Contact Form:  $fname"; // EDIT THE EMAIL SUBJECT LINE HERE
$email_body = "You have received a new message from your website's contact form.\n\n"."Here are the details:\n\nFirst Name: $fname\n\nLast Name: $lname\n\nPhone: $phone\n\nContact Date Time:\n$dateTime\n\nHeading: $heading\n\nCompany Name: $companyName\n\nAddress: $address\n\nPostal Code: $postalCode\n\nCity: $city\n\nCountry: $country\n\nMessage: $message\n\nEmail ID: $email_address";
$headers = "From: rohitnewicvs@gmail.com\n";
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

Please let me know what is problem.

Rohit Azad Malik
  • 31,410
  • 17
  • 69
  • 97

7 Answers7

2

You need to setup a mail server on your machine for the mail function to work. If you are on Windows (which I am guessing you are from your use of WAMP) you can setup a Pegasus mail server.

Other options include using a wrapper class such as SwiftMailer or PHPMailer and using them to connect to another SMTP server such as your GMail account.

Please see this Question it will solve your problem.

Community
  • 1
  • 1
OBAID
  • 1,299
  • 2
  • 21
  • 43
0

There is not possible to send email from local PC before doing some configuration. I suggest you upload your script to a web hosting server (Free or paid). I think it will work. Because SMTP already configured in web hosting server.

Habibur Rahman
  • 71
  • 1
  • 10
0

I strongly suggest to use the code mentioned in this web site to help you send from a gmail account:

http://phpmailer.worxware.com/?pg=examplebgmail

PHPMailer is commonly used by PHP developers and have lots of functionalities and also support SSL/TLS

Pooya
  • 6,083
  • 3
  • 23
  • 43
0

May be you are using wamp server or any other local development server where the SMTP settings are not defined and mail function needs SMTP to work. Define the SMTP settings in PHP.ini . You could use your GMAIL or any other mail provider SMTP settings for that to work too.

If you are using a VPS you could either use GMAIL OR anyother mail SMTP or you could install and run your OWN SMTP SERVICE. please follow this link https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send-only-smtp-server-on-ubuntu-14-04

Consider using swift mailer it offers more flexibility in configuaration, and you can set the SMTP setting in the script itself

0

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
  4. setup of smtp php.ini
Gautam Jha
  • 1,445
  • 1
  • 15
  • 24
0

I think You are using on local machine and you have to configure your mail function in php.ini or try on your live server by adding this code I hope this will help you..

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <nb158f@gmail.com>' . "\r\n";
$headers .= 'From: mysite.com <admin@site.com>' . "\r\n";
//$headers .= 'Cc: cc@gmail.com' . "\r\n";

// Mail it
$flag = mail($to, $subject, $message, $headers);
JH_
  • 406
  • 1
  • 4
  • 15
Divakarcool
  • 473
  • 6
  • 20
0

Modify the php.ini file to use it (commented out the other lines):

[mail function]
; For Win32 only.
; SMTP = smtp.gmail.com
; smtp_port = 25

; For Win32 only.
; sendmail_from = <e-mail username>@gmail.com

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
Mateo
  • 1,920
  • 2
  • 21
  • 27
RKM
  • 16