[php] Debugging PHP Mail() and/or PHPMailer

I'm quite stuck with a problem sending mail from a PHP script. Some data:

  • Shared hosting, no SSH access, only hosting provider panel
  • PHP version 5.2.5
  • Last year I built a site which had no problems sending mail with the same hosting
  • Let's say the domain is “domain.com” and my private address is “[email protected]” for anonimity's sake in the following code.

Here's the code:

<?php
error_reporting(E_ALL); 
ini_set("display_errors", 1);

$to = "[email protected]";
$subject = "Hi";
$body = "Test 1\nTest 2\nTest 3";
$headers = 'From: [email protected]' . "\r\n" .
    'errors-to: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

if (mail($to, $subject, $body, $headers)) {
    echo("Message successfully sent");
} else {
    echo("Message sending failed");
}

require('class.phpmailer.php');
$message = "Hello world";
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
$mail->AddAddress("[email protected]", "Agos");
$mail->SetFrom("[email protected]","My Site");
$mail->Subject = "Test Message";
$mail->Body = $message;
$mail->Send();
?>

And here is what I get:

Message sending failed Could not instantiate mail function.

Which is baffling to say the least. Is there anything I can do to get at least some more meaningful errors? Why is code from the class showing up in my file?

This question is related to php email sendmail

The answer is


It looks like the class.phpmailer.php file is corrupt. I would download the latest version and try again.

I've always used phpMailer's SMTP feature:

$mail->IsSMTP();
$mail->Host = "localhost";

And if you need debug info:

$mail->SMTPDebug  = 2; // enables SMTP debug information (for testing)
                       // 1 = errors and messages
                       // 2 = messages only

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to email

Monitoring the Full Disclosure mailinglist require(vendor/autoload.php): failed to open stream Failed to authenticate on SMTP server error using gmail Expected response code 220 but got code "", with message "" in Laravel How to to send mail using gmail in Laravel? Laravel Mail::send() sending to multiple to or bcc addresses Getting "The remote certificate is invalid according to the validation procedure" when SMTP server has a valid certificate How to validate an e-mail address in swift? PHP mail function doesn't complete sending of e-mail How to validate email id in angularJs using ng-pattern

Examples related to sendmail

Send mail via CMD console Relay access denied on sending mail, Other domain outside of network Using sendmail from bash script for multiple recipients sendmail: how to configure sendmail on ubuntu? VBScript to send email without running Outlook Send SMTP email using System.Net.Mail via Exchange Online (Office 365) Sending a mail from a linux shell script Sending HTML mail using a shell script Debugging PHP Mail() and/or PHPMailer How to send a html email with the bash command "sendmail"?