I am trying to use PHPMailer
(for the first time) in my project and I guess I have stuck.
What I am trying to achieve is upload the .txt
file which contains the list of users' email address and extract that into an array and send them the mail. I want to use built in php mail
function with the help of PHPMailer
.
Here's the code that I have tried so far:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once 'PHPMailer-master/PHPMailerAutoload.php';
if (isset($_POST['btnSendMail'])) {
$message = '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Testing E-Mail</title>
</head>
<body>
<h4>Testing</h4>
<a href="https://www.facebook.com">
<img src="http://www.example.in/images/unnamed-1.jpg" alt="Image" />
</a>
</body>
</html>';
$mail = new PHPMailer;
$mail->IsMAIL();
$mail->setFrom($_POST['emailFrom']);
$mail->Subject = $_POST['subject'];
$mail->msgHTML($message);
$fp = fopen($_FILES['emailTxtFile']['tmp_name'], 'rb');
while (($line = fgets($fp)) !== false) {
$mail->addAddress($line);
}
fclose($fp);
if (! $mail->send()) {
echo 'Message could not be sent. <br /><br />';
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
?>
<form method="POST" action="http://www.example.in/testmail.php" enctype="multipart/form-data">
<input type="text" name="subject" placeholder="Email Subject"/>
<input type="text" name="emailFrom" placeholder="From Email Address"/>
<input type="file" name="emailTxtFile" />
<input type="submit" name="btnSendMail" value="Submit">
</form>
I always end up getting error:
Message could not be sent.
Mailer Error: Could not instantiate mail function.
And the uploaded .txt
file contains:
eamil-1@example.com
email-temp@example.com
exple@email.com
//... Can contain n number of email address...
I am pretty sure this must be a minor mistake that I am unable to locate.
Kindly help me out with this.