skip to Main Content

I’m encountering an issue while trying to send emails using PHP’s PHPMailer library. I’m getting an error that says ‘Cannot Send Mail.’ and I’ve tried different ways to send mail even I got helped from ChatGpt but I could not. I’ve followed the documentation and my code looks like this:

<form action="mail.php" method="post">
    <input type="text" name="name">
    <input type="email" name="email" id="">
    <input type="submit" value="submit">
</form>

<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

require __DIR__ . '/PHPMailer-master/PHPMailer.php';
require __DIR__ . '/PHPMailer-master/Exception.php';
require __DIR__ . '/PHPMailer-master/SMTP.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $email = $_POST['email'];

    $mail = new PHPMailer(true);

    try {
        // SMTP configuration
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = '[email protected]';
        $mail->Password = 'mypassword'; // Replace with your Gmail password
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;

        // Sender and recipient details
        $mail->setFrom('[email protected]', 'Ahmed Maajid');
        $mail->addAddress('[email protected]'); // Set your Gmail address as both sender and recipient

        // Email content
        $mail->isHTML(false);
        $mail->Subject = 'New submission from your website';
        $mail->Body = 'Name: ' . $name . "nn" . 'Email: ' . $email;

        $mail->send();
        echo 'Email sent successfully!';
    } catch (Exception $e) {
        echo 'Oops! There was a problem: ' . $mail->ErrorInfo;
    }
}
?>

2

Answers


  1. Composer is the easiest way to get PHPMailer up and running. Just open your terminal and type: composer require phpmailer/phpmailer. Here’s the code snippet to send an email:

    require("./vendor/phpmailer/phpmailer/src/PHPMailer.php");
    require("./vendor/phpmailer/phpmailer/src/SMTP.php");
    
    
    // make all the setup to send email
    $mail = new PHPMailerPHPMailerPHPMailer();
    $mail->IsSmtp();
    $mail->SMTPDebug = 0;
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 587; 
    $mail->IsHTML(true);
    $mail->Username = "[email protected]";
    $mail->Password = "your_password";
    $mail->setFrom("[email protected]");
    // make email subject & body
    $subject = "testing subject";
    $messageBody = "testing message";
    $mail->Subject = $subject;
    $mail->Body = $messageBody;
    // send to
    $sendTo = "[email protected]";
    $mail->AddAddress($sendTo);
    // send request
    if($mail->Send()) {
        echo 'Email has been sent!';
    } else {
        echo $mail->ErrorInfo;
    }
    
    Login or Signup to reply.
  2. You may recheck the following

    • Your gmail password isn’t actually the password you use to sign in to your gmail account. If you’re using Google’s SMTP, it should be the password generated duration App password creating.

    • If you’re using localhost, you still need to be connected to the internet (if you’re using SMTP)

    • I hope you’ve modified the … example fields with your own configuration.

    • You may refer to the web for how set up Google SMTP for PHPMailer

    Bonus Temporary enable the debug true so you can know more about the error

    $mail->SMTPDebug = true;
    

    Thanks!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search