Phpmail

Hi Admin,

I am unable to send email from phpmailer using gmail smtp. I am not getting debug messages also. below is my code. please help.

	require 'phpmailer/PHPMailerAutoload.php';
	$mail=new PHPMailer;
	$mail->isSMTP();
            $Mail->SMTPDebug = 4;
	$mail->Host='smtp.gmail.com';
	$mail->Port=587;
	$mail->SMTPAuth=true;
	$mail->SMTPSecure='tls';
	$mail->Username='******@gmail.com';
	$mail->Password='********';
	$mail->setFrom('******@gmail.com','Pipelines CP Portal');
	$mail->addAddress($email);
	$mail->addReplyTo('******@gmail.com');
	$mail->isHTML(true);
	$mail->Subject='Verification Mail';
	$mail->Body=$mail_cont;
    $mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";};
    //...later

	if(!$mail->send()){
        echo ''Mail not sent";
	}
	else{
		echo 'Mail sent';
	}

should be

if(!$mail->send()){
        echo "Mail not sent";
	}
	else{
		echo 'Mail sent';
	}

Two quotes are different than double quote, meaning '' != "

1 Like

Hi,

here is my updated code,

function smtp_mailer($email, $mail_cont)
{
	require 'phpmailer/PHPMailerAutoload.php';
	$mail=new PHPMailer;
	$mail->isSMTP();
    $Mail->SMTPDebug = 4;
	$mail->Host='smtp.gmail.com';
	$mail->Port=587;
	$mail->SMTPAuth=true;
	$mail->SMTPSecure='tls';
	$mail->Username='*******@gmail.com';
	$mail->Password='*********';
	$mail->setFrom('*******@gmail.com','Pipelines CP Portal');
	$mail->addAddress($email);
	$mail->addReplyTo('*******@gmail.com');
	$mail->isHTML(true);
	$mail->Subject='Verification Mail';
	$mail->Body=$mail_cont;
    //$mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";};
    //...later

	if(!$mail->send()){
        echo 'Unable to send mail. '. $mail->ErrorInfo;
	}
	else{
		echo 'Mail sent';
	}
}

I am only receiving smtp connect error. No details.
I have allowed less secure apps on gmail also.

same code successfully in localhost on my pc.

same code successfully sends mail in localhost on my pc.

please reply

Please check your browsers Network tab to see the response code of the page. If it says status code 500, it means your code crashed and you need to debug it:

Also, “works on my machine” doesn’t really give us anything to check. Could you share the URL to the contact page?

Finally, if you’re looking to setup a contact form, you may also want to use our example form for this instead of trying to cobble something yourself:

2 Likes

Hi Admin,

there is no 500 error.

I have tried 2 different smtp services . I can send mail from both in localhost but not from infinity free hosting site.
link to my page http://opsteam.rf.gd on this page below links are there
click Gmail and click SendinBlue

after clicking on link following error is shown on page
“Message could not be sent due to SMTP connect() failed. Troubleshooting · PHPMailer/PHPMailer Wiki · GitHub”

and in network tab below error is shown
“Cross-Origin Read Blocking (CORB) blocked cross-origin response https://infinityfree.net/errors/404/ with MIME type text/html. See Chrome Platform Status for more details.”

I have set debug to 4 but no error other error messages are shown.

on click below php file opens

<?php
	require 'phpmailer/PHPMailerAutoload.php';
	$mail=new PHPMailer;
	$mail->isSMTP();
    $Mail->SMTPDebug = 4;
	$mail->Host='smtp.gmail.com';
	$mail->Port=587;
	$mail->SMTPAuth=true;
	$mail->SMTPSecure='tls';
	$mail->Username='[email protected]';
	$mail->Password='********';
	$mail->setFrom('[email protected]','Great Guys');
	$mail->addAddress('[email protected]');
	$mail->addReplyTo('[email protected]');
	$mail->isHTML(true);
	$mail->Subject='Login Mail';
	$mail->Body='<h2>Hello Boss gmail</h2>';
	if(!$mail->send()){
		echo 'Message could not be sent due to '.$mail->ErrorInfo;
	}
	else{
		echo 'Message sent successfully from gmail';
	}
	
?>

Please guide

Could you please change that second line to $mail->SMTPDebug = 4? PHP variables are case sensitive, which means you’re trying to set the debug property on a non-existent object.

1 Like
<?php
//main configs
use PHPMailer\PHPMailer\PHPMailer;
require 'path/to/composer/vendor/autoload.php';
//PHPMailer call
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 4; 
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = '***********';
$mail->SMTPSecure = 'tls';
$mail->Port = 587; //verify this port
//sender and reply config
$mail->setFrom('[email protected]', 'Great  Guys');
$mail->addReplyTo('[email protected]', 'add a name if wanted');
$mail->addAddress('[email protected]');
$mail->isHTML(true);

$mail->Subject = "Login Mail";
//$mail->addEmbeddedImage('path/to/image_file.jpg', 'image_cid');
$mail->Body = '<h2>Hello Boss gmail</H2>';
//sending config
if(!$mail->send()){
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}else{
    echo 'Message has been sent';
}
?>

This code snippet is incomplete.

@anon19508339, yeh did with haste my mistake, after your edit decided to check it out withan editor, alas found another, but can confirm now its bug free

Can you please set the SMTPDebug property on the $mail object to level 4? We’re trying to figure out why the SMTP connection doesn’t work, and it would help a lot to have debug logging. In your latest code snippet, I don’t see the debug flag at all.

4 Likes

plus $mail=new PHPMailer; is missing, it means PHP cannot execute and get data from an undefined variable named $mail

3 Likes

4 very low not very good for debuging in my opinion,
3 really ojust also includes connection issues
2 is best my opinion, as you get the main bugs from the client outputs and the server responces without all the extra clutter

but edited as requested

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