0
votes

I have a php code for sending confirmation email. But how to send this email to registered user using my mail server. Example using gmail to send confirmarion email.

<?php   
    if(isset($_SESSION['error'])) {
         header("Location: index.php");
         exit; 
        } else { 
           $username = $_POST['username']; 
           $email = $_POST['email']; 
           $password = $_POST['password']; 
           $com_code = md5(uniqid(rand()));

        $sql2 = "INSERT INTO user (username, email, password, com_code) VALUES ('$username', '$email', '$password', '$com_code')"; $result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error());

        if($result2) { 
            $to = $email; 
            $subject = "Confirmation from  MyName to $username"; 
            $header = "TutsforWeb: Confirmation from TutsforWeb"; 
            $message = "Please click the link below to verify and activate your account. rn"; $message .= "http://www.yourname.com/confirm.php?passkey=$com_code";

        $sentmail = mail($to,$subject,$message,$header);

        if($sentmail) { 
           echo "Your Confirmation link Has Been Sent To Your Email Address."; 
        } else { 
          echo "Cannot send Confirmation link to your e-mail address"; 
        } 
     } 
  } 
 } 
?>
5
First format your question and make it a little more easy to readDeepu

5 Answers

0
votes

If you have mail server then you follow the your mail server rules.

You can use PHPMailer and follow the rule of phpmailer function.

0
votes

Fix : You have to find out whether you have installed a mail server in your server instance. This depends on server environment. You can find how to with simple web search. Tip: If just get the response from the operation as normal your mail server is not connected. But if it's keep waiting for like 4 to 5 seconds means most of the time server is there issue is something in it. Ubuntu - [How to install postfix][1] Windows - [SMTP E-mail][2]

Further issues : Once you can send the mail using php mail function but still you have give the accurate header information otherwise the mail will send to spam folder.

Wrong: $header = "TutsforWeb: Confirmation from TutsforWeb"; Correct:
$headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

If you want to do it gmail just refer this : Send email using the GMail SMTP server from a PHP page

0
votes

You first fetch data from table where registered users are stored then you can send mail to registered users

0
votes

If you have your mail servers credentials then you can use SMTP to send emails. You can also use PHPMailer which is very easy to use.

First thing is you need to install PHPMailer from above link after that use following code `

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;


$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 = '[email protected]';                 // Your gmail username
$mail->Password = 'your_gmail_password';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = '[email protected]'; // from email address
$mail->FromName = 'User1'; // whatever is the name of sender
$mail->addAddress($_POST['email'], $_POST['username']);     // Add a recipient

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = $subject;
$mail->Body    =  $message ;

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>`
0
votes
Download PHPMailerAutoload 

link here

<?php

// 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();

// we are setting the HOST to localhost
$mail->Host = "mail.example.com";  // 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:
$mail->Username = "[email protected]";  // SMTP username
$mail->Password = "password"; // SMTP password

$mail->From = "[email protected]";

// below we want to set the email address we will be sending our email to.
$mail->AddAddress("[email protected]", "To whom");

// 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 = "Text 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";
?>