-1

I am using mail() function to sent mails when an event happening. But it is not working as I expected. I tried to get the return of the function also. Some one please suggst what may be the issue.

        $msg = "Your password has been changed.is'".$data['password']."'";
        $to = $data['email'];
        $subject = "password changed";
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: info@hia.com'; 
        $send = mail($to, $subject, $msg, $headers);
        if($send){
            echo "successful";
        }
        else{
            echo "error";
        }
jumban
  • 59
  • 1
  • 7
  • 2
    I suggest using PHPmailer – MuthaFury Jun 27 '16 at 10:29
  • Change `echo "error";` to `var_dump(error_get_last());` and see what it outputs. – Styphon Jun 27 '16 at 10:30
  • Sure. let me try and will let you know. thanks for your valid reply. – jumban Jun 27 '16 at 10:33
  • @Styphon array(4) { ["type"]=> int(2) ["message"]=> string(164) "mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()" ["file"]=> string(39) " – jumban Jun 27 '16 at 10:36
  • @jumban from the error apparently you're connecting to your localhost. Does your server has smtp set-up? – MuthaFury Jun 27 '16 at 10:37
  • @Jumban your local server doesn't have mail available or configured correctly. Contact your host. – Styphon Jun 27 '16 at 10:40

2 Answers2

0

Don't use simple mail(), i prefer for PHPmailer function. Here is a Example. First - download phpmailer in your project directory. second - create send_mail.php in your directory(you can change you name).

  • send_mail.php file code.

    require_once "PHPMailer/PHPMailerAutoload.php";
    
    $mail = new PHPMailer(true);
    if (!isset($_POST['send'])) {
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the enquiry form!";
    die;
    }
        $to="abc@xyz.com";
        $senderName = "xyz";
        //send the mail
        $fname = $_POST["fname"];
        $lname = $_POST["lname"];
        $zcode = $_POST["zcode"];
        $email = $_POST["email"];
        $phone = $_POST["phone"];
        //Enable SMTP debugging.
        $mail->SMTPDebug = 0;
        //Set PHPMailer to use SMTP.
        $mail->isSMTP();
        //Set SMTP host name
        $mail->Host = "smtp.gmail.com";
        //Set this to true if SMTP host requires authentication to send email
        $mail->SMTPAuth = true;
        //Provide username and password
        $mail->Username = "abc@xyz.com";
        $mail->Password = "########";
        //If SMTP requires TLS encryption then set it
        $mail->SMTPSecure = "ssl";
        //Set TCP port to connect to
        $mail->Port = 465;
        $mail->From = $to;
        $mail->FromName = $senderName;
        $mail->addAddress($to, $senderName);
        $mail->addReplyTo($email, $name);
        $mail->isHTML(true);
        $mail->Subject = "bfuiebfiaif";
        $mail->Body = "as per your needs";
    if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
    die;
    }
    header('Location: index.php');
    die;
    
  • #chiragpatel simply dont copy from online..give required answer –  Jun 27 '16 at 10:48
  • #Ashu - i am just giving proper answer with example.. thats it. –  Jun 27 '16 at 10:50
  • [link](http://crazynhappy.com/sending-email-php-phpmailer-function/) Complete Guide for phpmailer. –  Jun 28 '16 at 06:21
0

Problems arising from the php setting. Closing function for security by some service provider. You must send mail with SMTP.

Example several SMTP class on github; hujuice/smtp - snipworks/php-smtp - PHPMailer/PHPMailer

Example code a PHPMailer according to your code;

<?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';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'info@hia.com';                 // SMTP username
$mail->Password = 'yourmailpassword';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('info@hia.com', 'Mailer');
$mail->addAddress($data['email']);               // Name is optional
$mail->addReplyTo('info@hia.com', 'Information');
$mail->addCC('cc@hia.com');
$mail->addBCC('bcc@hia.com');
$mail->CharSet = 'ISO-8859-1';
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'password changed';
$mail->Body    = 'Your password has been changed.is "'.$data['password'].'"';

if(!$mail->send()) {
    echo 'error';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'successful';
}
Hüseyin ASLIM
  • 153
  • 1
  • 15