I cannot send email

<?php
	require 'PHPMailer-master/src/Exception.php';
	require 'PHPMailer-master/src/PHPMailer.php';
	require 'PHPMailer-master/src/SMTP.php';

	use PHPMailer\PHPMailer\PHPMailer;
	use PHPMailer\PHPMailer\Exception;

	if (isset($_POST['sendmail'])) {

		$mail = new PHPMailer(true);

		try{
			$mail->isSMTP();
			$mail->Host = 'smtp.gmail.com';
			$mail->SMTPAuth = true;
			$mail->Username = 'XXXX@gmail.com';
			$mail->Password = 'XXXX';
			$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
			$mail->Port = 587;
			$mail->setFrom('XXXX@gmail.com');
			$mail->addAddress($_POST['email']);
			$mail->Subject = $_POST['subject'];
			$mail->isHTML(true);
			$mail->Body = $_POST['message'];


			$mail->send();
			echo '<script>alert("Successfully Send!")</script>';

		}catch(Exception $e){
			echo '<script>alert("Message Error!")</script>';
		}
	}
?>

<!DOCTYPE html>
<html>
<head>
	<title>Notification</title>
	<link rel="stylesheet" type="text/css" href="message_style.css">


</head>
<body>
	
</body>
<center>
<div>

	<div>
		<div>
			<div class="header" style="margin-top: 4%;">
			<h2 style="font-size: 25px;">Message</h2>
			</div>
			<form role="form" method="post" enctype="multipart/form-data">
				<div>
					<div >
					<label for="email">Email To:</label>
					<input type="email" class="form-control" id="email" name="email" placeholder="Email address" maxlength="50" autocomplete="off">
				</div>
			</div>

			<div class="row">
				<div>
					<label class="subject" for="subject">Subject:</label>
					<input type="text" class="form-control" id="subject" name="subject" placeholder="subject" maxlength="50" autocomplete="off">
				</div>				
			</div>

			<div>
				<div>
					<label class="message" for="name">Message:</label>
					<textarea class="form-control" type="textarea" id="message" name="message" maxlength="6000" rows="4">Please Confirm my Payment...</textarea>
				</div>
			</div>

			<div>
				<div class="col-sm-9 form-group">
					<button type="submit" name="sendmail" class="btn">Send</button>
				</div>
			</div>	
		</form>	
		</div>
	</div>
</div>
</center>

<br><br><br><br><br><br><br><br>

</body>
</html>

“Message Error!” is showing when I try to send.
I would like also to attach an image or a file.

echo the error message and tell us what it says.

catch(Exception $e){
			echo $e->getMessage();
		}

SMTP Error: Could not authenticate.

Have you turned on “Access for less secure apps” in your Gmail account?

thanks finally working

how about I can insert an image or file?

You mean Attach an image?

yes attach image/file

What have you tried so far to send an attachment? There seem to be hundreds of examples out there. From a quick search it looks to me that it’s nothing more complex than

$mail->addAttachment($pathname, $name)

where $pathname is the file location, and $name is the name you give it inside the email.

Off-topic:
When posting things like usernames, passwords, email addresses etc, please redact them (e.g. XXXX) to prevent other people from trying to use them. It is important to show that you set them, the actual values don’t matter.

2 Likes

I am asking because I don’t know for sure, but it might apply to this topic. If the image is a longblob and doesn’t have a physical path then it wouldn’t be an attachment but instead it should be rendered to an html type email much like he does on his website. Isn’t that the correct approach?

<img src="data:'.$row['imageType'].'; base64,'.base64_encode($row['imageData']).'"/>

If it is a file, then yes an attachment is the way to do it…

Oh, that might be the case. While I’ve seen the other thread about storing images directly in a database, and might even have posted on it, I didn’t realise this is the same poster. The thread was talking about attachments, although the OP did originally ask about inserting rather than attaching. Perhaps the OP could clarify the situation in the question, or as it’s a different topic, perhaps start a new thread.

1 Like

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