Whats wrong with this code!

OK I’ve realised the issue, those two lines were missing the ; at the end of each one. the page now loads then I get another page after submitting the form ‘Attempting to send e-mail…’ but that’s all it goes no further

I had commented that out, but chances are you are still getting an error of some sort. Try using the following:


  try {
    include_once('class.phpmailer.php');
    $mail = new PHPMailer(true);
    $mail->IsSMTP();
    $mail->Host = ""; // place your smtp host here
    $mail->SMTPAuth = true;
    $mail->Username = ""; // place your smtp username here
    $mail->Password = ""; // place your smtp password here

    $mail->From = $_POST["email"];
    $mail->FromName = $_POST["name"];
    $mail->AddAddress("enquiries@.co.uk");

    $mail->Subject = "Enquiry from the  Website";

    // build the email
    $s = "The following contact form has been submitted:\
\
";

    $s .= "=== Customer Details =====\
";
    $s .= "Name: " .$_POST["name"]. "\
";
    $s .= "Company: " .$_POST["company"]. "\
";
    $s .= "Telephone: " .$_POST["telephone"]. "\
";
    $s .= "Email Address: " .$_POST["email"]. "\
\
";

    $s .= "=== Enquiry Information =====\
";
    $s .= trim($_POST["comments"])."\
\
";

    $s.= "Generated: " .date("Y-m-d H:i:s");

    $mail->Body = $s;
    $mail->WordWrap = 72;

//    echo "Attempting to send e-mail...";
    if ($mail->Send()) {
        $SENT = true;
//        echo "Success!";
    } else {
        $SENT = false;
//        echo "Failed!";
    }
  } catch (phpmailerException $e) {
    echo $e->errorMessage();
  } catch (Exception $e) {
    echo $e->errorMessage();
  }

Now I get a period of the page reloading (10 secs maybe) then goes back to the contact page with the original error there was a problem sending the form…

If I uncomment out the quotes, I receive Attempting to send e-mail…Failed!

I’m going to assume the username and password are correct (along with the Host) as that should have generated a specific type of error. So I’d take a look at

$mail->AddAddress("enquiries@.co.uk");

That doesn’t look like a valid e-mail address.

I have just deleted the domain from that E-mail address, - the actual address is correct in my uploaded script. Could there be something missing in the code that’s causing it not to send? I noticed the following in the sample script from the host:

// Send Message and Report
if(!$mail->Send()) { echo "Message could not be sent.
"; echo “Mailer Error: " .
$mail->ErrorInfo; exit; } echo “Mail was sent thank you!”; echo sprintf(”
Go Back ",$_SERVER[‘HTTP_REFERER’]); ?>

Ok so ive managed to get this working by creating a pop email address on the host & using that. I cannot get it to send to their server direct though as I would like, - when I change the E-mail address to one of their own on their mail server, the email fails to send!?

If you’re using SMTP Authentication (which you appear to be) then the email address, username and password for the sending account must exist. You also must have permission to send (ie host or port isn’t blocked somewhere along the line)

The E-mail account I want to use does exist, its just not setup on the host as they are using mx records & have their own mail server. As I say I can create a temporary pop account on the host & it works fine, but obviously its just not sending to the right address!

Them having their own MX records and mail server shouldn’t matter. The web server should be connecting to an SMTP server that you specify and just sending the email direct to there. The MX record shouldn’t make any difference at this point. It’s after you’ve connected to the SMTP server and delivered the email that the MX server will even be looked up, and that would be by the SMTP server itself, not the webserver that is sending the mail.

For example, if you’re sending to antnee@mydomain.com, you may have the following configuration:

  • SMTP server: smtp.yourdomain.com
  • SMTP username: youruser
  • SMTP password: yourpass
  • SMTP authentication: TRUE
  • SMTP secure: FALSE

So, your mailer code knows that it needs to connect to smtp.yourdomain.com and to authenticate using the username and password. The email will have a to-address of antnee@mydomain.com. The SMTP server should accept this as you’ve successfully authenticated. It should not fail at this point.

What happens next is that it looks at the to-address, and picks out the domain, ie mydomain.com. It now needs to look for an MX record in the DNS for mydomain.com, and not for yourdomain.com. It would hopefully find something like mx1.mydomain.com and the SMTP server would send the mail via this exchanger. Your webserver is not a part of this loop at all. If that fails (mailbox doesn’t exist, quota full etc) it will return an error to the SMTP server, which will usually keep retrying until eventually it gives up and sends an email to your return email address, which if not set explicitly will be something like webmaster@serverhost.com

So, unless my understanding of the mail system is completely flawed, I’m not sure how them having their own mail server and MX records will be the problem. It’s more likely that you’ve not authenticated correctly, and that could be username, password, whether authentication is even required or not, if you need to be using a secure connection and which port you should connect to the SMTP server on (they’re not always on the port you expect).

Ok so it looks like I’m currently setting this up with an email address & credentials from the webhost (eurofasthost) rather than the actual server which I guess is why its not working.

The problem is when I put the server details in, its not working but I don’t know why. We have an A record setup on the host pointing to our static IP address which is mail.ourdomain.co.uk, so I’m assuming that would be the smtp host. We use a Windows SBS server with Exchange so I’ve set the host as mail.ourdomain.co.uk, and then used one of our E-mail addresses setup on the server for authentication (enquiries@ourdomain.co.uk). this is the setup that’s not working & I don’t know why, - should I be using different ports for this or some kind of different authentication?

I can access Outlook web access by typing https://mail.ourdomain.co.uk/exchange so that bits all working but I just cant figure out what else needs to be changed. All E-mails in & out of the server are also scanned by a 3rd party spam washer company, so we use an smtp smarthost but that shouldn’t have any effect should it?

I’m no expert on PHPMailer, but I don’t think so, no. It sounds like you’re just not authenticating. If you have IMAP/POP email set up anywhere (like on your phone) you might want to check your settings, or ask your sysadmins what settings you need. That said, if you’re using Exchange you might not have SMTP available IIRC. I think Exchange has its own protocols and SMTP is only enabled on demand. Not something I’ve had to work with personally mind you