Search code examples
phphtmlphpmailer

i am not receving the mail


$to = 'someone.com';
$firstname = $_POST["fname"];
$lastname = $_POST["fname"];
$subject= $_POST["subject"];
$email= $_POST["email"];
$text= $_POST["message"];



$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: " . $email . "\r\n"; // Sender's E-mail
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

this is my code for my php page. can you let me know what is wrong with my code. thank you for your help

if (mail($to,$subject,$text,$headers))
{
  echo "Check Email please";
}
 else
{
    echo 'failed';
}

Solution

  • Please make sure you are adding Phpmailer classes on the top.

    This is a proper working mailer file, put your credentials here and send the email.

    <!DOCTYPE html>
    <html>
    <head>
        <title>Php Mailer</title>
        <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
    </head>
    <body>
            <?php 
        // require 'PHPMailerAutoload.php';
    
            require("PHPMailer-master/src/PHPMailer.php");
            require("PHPMailer-master/src/SMTP.php");
            require("PHPMailer-master/src/Exception.php");
    
    
            $mail = new PHPMailer\PHPMailer\PHPMailer();
            $SendMailTo = "[email protected]";
    
            $mail->SMTPDebug = 0;                       
    
            $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]";                 // SMTP username
            $mail->Password = "your_user_Password";                           // SMTP password
            // $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 587;                                    // TCP port to connect to
    
            $mail->setFrom("[email protected]");
            $mail->addAddress($SendMailTo);     // Add a recipient             // Name is optional
            $mail->addReplyTo("[email protected]");
    
            // $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
    
            ///send custom code in email
            $code = 242343;
            $mail->Subject = 'TeachMe verification email';
            $mail->Body    = 'Hi <br><br> <b>Thanks for registeration,Your verification code is </b>'.$code;
            $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';
            }
        ?>
    </body>
    </html>