简体   繁体   中英

How can I send a confirmation mail to an email ID from local host without a domain name?

Once my code is run in PHP, it says that the confirmation mail has been sent successfully, but the mail has not been received at the target mail ID. I have used the mail() function in PHP to send the confirmation mail and I have also installed Postfix on my Ubuntu. What is the problem here?

<?php
     include('config.php');
     $tb_name = temp_members_db; 
     $confirm_code = md5(uniqid(rand()));
     $name = $_POST['name'];
     $email = $_POST['email'];
     $pass = $_POST['pass'];
     $country = $_POST['country'];
     $sql = "INSERT INTO  $tb_name(confirm_code,name,email,password,country) VALUES ('$confirm_code','$name','$email','$pass','$country')"; 
$result = mysql_query($sql);

     if($result) {
          $to = $email;
          $sub = "Your Confirmation Code";
          $message = "Your confirmation code is" . $confirm_code;
          $send = mail($to,$sub,$message);
          var_export($send);
    } else {
          echo "Havent found email ID in our database";
    }

   if($send) {
         echo "Sent the confirmation link to your email ID";
   } else {
         echo "Sending failed";
   } 
?>

Have you switched on the SMTP function on the localhost server?

If not enabled the SMTP and mail functions

Use PHPMailer class. Very easy to install and use. The PHP mail() function usually sends via a local mail server, typically fronted by a sendmail binary on Linux, BSD and OS X platforms, however, Windows usually doesn't include a local mail server; PHPMailer's integrated SMTP implementation allows email sending on Windows platforms without a local mail server.

Just download the class files from here: https://github.com/PHPMailer/PHPMailer

Example:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

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

Set SMTP and if you want to use secure encryption (ssl,tls and ports)

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // If you want to use encryption, `ssl` also accepted

$mail->Port = 587;                                    // TCP port to connect to(or port 25,465 etc)

Set fields like from, to, bcc, subject, email body etc.

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
                                                      // Name is optional
$mail->addReplyTo('info@example.com', 'Information');


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

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM