0

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.

Saiyan Prince
  • 3,930
  • 4
  • 28
  • 71
  • comment out `/*$fp = fopen($_FILES['emailTxtFile']['tmp_name'], 'rb'); while (($line = fgets($fp)) !== false) { $mail->addAddress($line); } fclose($fp);*/` and just do `$mail->addAddress("temp@example.com");`, obviously change the email address, and let us know what happens. – Billy Dec 31 '15 at 10:48
  • Nope.. Still the same error... – Saiyan Prince Dec 31 '15 at 10:51
  • deleted my answer, no the space doesn't matter,Thought it did, sorry, thought I'd cracked it....... – Billy Dec 31 '15 at 11:06
  • are you on localhost ? I know xampp needs extra settings to send mail. – Billy Dec 31 '15 at 11:14
  • Nope.. Testing it on the server.. – Saiyan Prince Dec 31 '15 at 11:15
  • if you comment it all out and just do `mail("someone@example.com","test subject","test message");` does that work, obviously send yourself an email so change the email address – Billy Dec 31 '15 at 11:18
  • Yeah.. That does work without any issue.. – Saiyan Prince Dec 31 '15 at 11:18
  • Possible solution http://stackoverflow.com/a/7145096/2746541 – Mario Werner Dec 31 '15 at 11:21
  • @MarioWerner Thanks for the link, but I have clearly said that I want to use PHP's built in mail function.. – Saiyan Prince Dec 31 '15 at 11:22
  • `mail()` operations are set in your php.ini - read the PHP docs. – Synchro Dec 31 '15 at 11:26
  • been nosing through the docs, looking at possible solutions but am unable to test at the mo, setFrom looks like it takes 2 args, not sure if they're optional, the other thing i would try is commenting out the loop and hardcoding the values into all the fields,subject,from,to address and message in case the problem lies there, sorry can't help more. – Billy Dec 31 '15 at 11:32
  • It's okay.. I appreciate your help.. No issues.. Even I am scratching my head since 5 hours, but couldn't get it work.. Thanks once again. :) – Saiyan Prince Dec 31 '15 at 11:34
  • @user3514160 PHP has no "built in mail function". It always uses the mail function of the operating system, which in most cases is `sendmail`. If you want to use `sendmail` thats ok. But you should at least test if it also works with SMTP. – Mario Werner Dec 31 '15 at 11:46
  • Okay.. I will surely look into that for sure.. But can you please guide me how to incorporate SMTP host, username, password ? I am fairly new to this thing.. I have this website setup on shared hosting server.. – Saiyan Prince Dec 31 '15 at 11:48
  • How to setup `PHPMailer` to use `STMP` is shown in the link I posted you. You should get the specific SMTP settings in the backend of your hosting provider's customer panel. SMTP username is usually the full email address, like `jondoe@somedomain.com` – Mario Werner Dec 31 '15 at 11:51

0 Answers0