0
votes

So I have this function:

$toAddress = $sendTo;
try {
    $mail = new PHPMailer(); // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true; // authentication enabled
    $mail->Host = self::$host;
    $mail->Port = 587;//self::$port; // or 587
    $mail->SMTPSecure = 'tls';
    $mail->IsHTML(true);
    $mail->CharSet = self::$charset;
    $mail->Username = self::$username;
    $mail->Password = self::$password;
    $mail->SetFrom(self::$from, self::$fromAlias);
    $mail->Subject = trim("Here's your csv.");
    $mail->Body = trim("Thank you for using List Master Application. Please download your csv on this link. <a href='http://36.55.238.182:81/pagination/download?file=".$fileName."'>Here!</a>");
    if(is_array($toAddress))
    {
        foreach($toAddress as $to_line)
        {
            $mail->AddAddress($to_line);
        }
    }
    else
    {
        $mail->AddAddress($toAddress);
    }

    if ($mail->send())
    {
          log_message('debug', 'sucessfully send email to ' . $sendTo);
          return TRUE;
    }
    else
    {
         log_message('debug', 'Failed to send email to ' . $sendTo);
         log_message('debug', 'Failed message is : ' . $mail->ErrorInfo);
         return FALSE;
    }

    } catch (phpmailerException $e) {
          log_message('debug', $e->errorMessage()); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
          log_message('debug', $e->getMessage()); //Boring error messages from anything else!
    }

I have been told previously that I need to upgrade the PhpMailer.

But I need to know why does it sometimes send successfully, and also it sometimes fail in this phpmailer 5.2 version.

Could there be any mail() settings missing?

Is there any reason for it to time-out?

Could it be that server mail regarded it as spam mail?

It's good that mostly it sends successfully but when it fails, I check the log and it gives me this:

The following From address failed: [email protected] : Called Mail() without being connected

Why is this showing when email fails to send?

What does this mean?

1
This is covered in the troubleshooting guide, as I said the last time you asked. The best way to solve this is not to send directly, but install a local mail server and relay through that. Mail servers are specifically designed to cope with intermittent connections, and it's also by far the fastest way to handle sending email during page submissions in your app. This is also covered in the guide and other articles in the PHPMailer wiki. - Synchro
Also you're still basing your code on an obsolete example (even for 5.2), and because you've not enabled exceptions (by passing true to the constructor), your try/catch will do nothing. - Synchro
@Synchro have you any links that I can base latest code for sending mail? - John Dale Ocaya Andil Dale
This really isn't rocket science you know: github.com/PHPMailer/PHPMailer - Synchro

1 Answers

-1
votes

PHPMailer is the easy system to send mail one to another from your PHP web page and it is a code library. It is so simple system to send emails with safely and easily via PHP or PHP code from a web server.

<?php
    $subject ="Send Mail";
    $receive_email = "[email protected]";
    $full_name=$_POST['full_name'];
    $phone=$_POST['phone'];
    $email=$_POST['email'];
    $address=$_POST['address'];
    $user_details="Full Name: ". $full_name. "\n Phone No: ". $phone."\n Address: ".$address;

    require_once("class.phpmailer.php");
    $mail = new PHPMailer();

    $body = "</pre>
    <div>";
    $body .= "<p>Full Name :".$full_name."</p>";
    $body .= "<p>Phone :".$phone."</p>";
    $body .= "<p>Address :".$address."</p>";
    $body .= "</div>" ;

    // And the absolute required configurations for sending HTML with attachement

    $mail->AddAddress("[email protected]", "My Website Name");
    $mail->Subject = "Send Mail";
    $mail->MsgHTML($body);

    if(!$mail->Send()) {
        echo "There was an error sending the message";
        exit;
    }
    echo "Thank you for contacting us!";
?>