PHPMailer Not working on Infinityfree but works offline

thecassava.co/phpmailer.php

my gmail login is failing (it shows that i might be using wrong password but my password is 100% correct as it works offline ):
also if i remove the $mail->isSMTP(true); it shows message sent successfully but i don’t recieve anything

My code
<?php
// use PHPMailer\PHPMailer\PHPMailer;
// use PHPMailer\PHPMailer\Exception;

// require 'PHPMailer/Exception.php';
// require 'PHPMailer/PHPMailer.php';
// require 'PHPMailer/SMTP.php';

date_default_timezone_set('Asia/Kolkata');

// Edit this path if PHPMailer is in a different location.
require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->isSMTP(true);

/*
 * Server Configuration
 */

$mail->Host = 'smtp.gmail.com'; // Which SMTP server to use.
$mail->Port = 587; // Which port to use, 587 is the default port for TLS security.
$mail->SMTPSecure = 'tls'; // Which security method to use. TLS is most secure.
$mail->SMTPAuth = true; // Whether you need to login. This is almost always required.
$mail->Username = "[email protected]"; // Your Gmail address.
$mail->Password = "validpassword"; // Your Gmail login password or App Specific Password.


/*
 * Message Configuration
 */

$mail->setFrom('[email protected]', 'The Cassava'); // Set the sender of the message.
$mail->addAddress('[email protected]', 'John Doe'); // Set the recipient of the message.
$mail->Subject = 'PHPMailer GMail SMTP test'; // The subject of the message.
// $mail->SMTPDebug = 2;
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
/*
 * Message Content - Choose simple text or HTML email
 */
 
// Choose to send either a simple text email... .
// $mail->Body = 'This is a plain-text message body'; // Set a plain text body.

// ... or send an email with HTML.
$html="<html>
                        <head>
                        <title>Cassava Team</title>
                        </head>
                        <body>
                        <h1> Welcome To Cassava </h1>
                        <p>Hi vikds,<br> We have received your query. Our representative will contact you soon.</p>
                        <p>You wrote: </p>
                        <blockquote style='background:#ddd;width:max-content; color:#2a2a2a; padding:5px; '>thisi is osme wmesage
                        </blockquote>

                        </body>
                        </html>";
$mail->msgHTML($html);
// Optional when using HTML: Set an alternative plain text message for email clients who prefer that.
//$mail->AltBody = 'This is a plain-text message body'; 

// Optional: attach a file
// $mail->addAttachment('images/phpmailer_mini.png');

if ($mail->send()) {
    echo "Your message was sent successfully!";
} else {
    echo "Mailer Error: " . $mail->ErrorInfo;
}

indent preformatted text by 4 spaces
$mail->SMTPOptions = array(
   'ssl' => array(
     'verify_peer' => false,
     'verify_peer_name' => false,
     'allow_self_signed' => true
    )
);

adding the above code helped me solve this…though i’m not sure…:expressionless:

1 Like

now i have another issue…
when i received the mail, in the from section it show my “[email protected]” rather than “[email protected]

This is normal. Gmail does not allow you to send email with a sender address which is not on the account. Which effectively means you will always send with an @gmail.com address. There is no way to “fix” this, other than using a different email provider which does allow you to use your own sender address.

5 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.