0

After referred a lot,

I have done following:

Here is php.ini:

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = xx@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

sendmail.ini:

smtp_server=smtp.gmail.com
smtp_port=25
smtp_ssl=auto
error_logfile=error.log
debug_logfile=debug.log
auth_username=xx@gmail.com
auth_password=xxyyxxyy
force_sender=xx@gmail.com

and here is front-end code:

        <form action="send.php" method="post" id="newsletter" name="newsletter">
            <input type="text" name="signup-email" id="signup-email" value="" />
        <button id="actbtn" class="btn btn-7 btn-7h icon-envelope">Subscribe!</button>
            <span class="arrow"></span>
        </form>
        <div id="response"></div>
    </div>

finally send.php:

<?php

$host   = "localhost";
$dbname = "database-name";
$user   = "";
$pass   = "";

$email    = filter_var($_POST['signup-email'], FILTER_SANITIZE_EMAIL);
$datetime = date('Y-m-d H:i:s');

try {
    $db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

    if (empty($email)) {
        $status = "error";
        $message = "The email address field must not be blank";
    } else if (!preg_match('/^[^0-9][A-z0-9._%+-]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/', $email)) {
        $status = "error";
        $message = "You must fill the field with a valid email address";
    } else {
        $existingSignup = $db->prepare("SELECT COUNT(*) FROM signups WHERE signup_email_address='$email'");
        $existingSignup->execute();
        $data_exists = ($existingSignup->fetchColumn() > 0) ? true : false;

        if (!$data_exists) {
            $sql = "INSERT INTO signups (signup_email_address, signup_date) VALUES (:email, :datetime)";
            $q = $db->prepare($sql);
            $q->execute(
                array(
                    ':email' => $email,
                    ':datetime' => $datetime
            ));

            if ($q) {
$status = "success";
$message = "You have been successfully subscribed";
} else {
$status = "error";
$message = "An error occurred, please try again";
}
} else {
$status = "error";
$message = "This email is already subscribed";
}
    }

    $data = array(
        'status' => $status,
        'message' => $message
    );

    echo json_encode($data);

    $db = null;
}
    catch(PDOException $e) {
    echo $e->getMessage();
}
?>

So when i enter email in my newsletter form, it didn't receive any mail in my mail account.

I m using xampp.

Can anybody please help me?

Still what i am missing? thanks.

sandhu
  • 117
  • 1
  • 3
  • 15

1 Answers1

0

Dont change anything in php.ini. Use phpmailer for sending mails from local.
Download phpmailer

Use this code-

require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose  debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'gmailemail';                 // SMTP username
$mail->Password = 'gmailpassword';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = 'from@gmail.com';
$mail->FromName = 'GOKU';
$mail->addAddress('qwe@qwe.com');     // Add a recipient
$mail->addAddress('qwe@qwe.com');               // Name is optional

// $mail->addReplyTo('info@example.com', 'Information');
// $mail->addCC('cc@example.com');
$mail->addBCC('qwe.qwe@gmail.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 = 'hello';
$mail->Body    = 'Hi<br>';
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

Hope this helps.. :)

Form-

<form action="send.php" method="post" id="newsletter" name="newsletter">
    <input type="text" name="signup-email" id="signup-email" value="" />
    <input type="submit" id="actbtn" class="btn btn-7 btn-7h icon-envelope" value="Subscribe!" name="subscribe">
    <span class="arrow"></span>
</form>

in send.php -

if(isset($_POST['subscribe'])){
    // write send mail code here
}
mohit
  • 1,878
  • 1
  • 16
  • 27
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/78805/discussion-on-answer-by-mohit-issues-in-sending-email-from-localhost-in-xampp). – Taryn May 26 '15 at 12:39