I'm trying to use PHPMailer to send an email using smtp and php. The server I use needs to have this type of connection:

Server name: mail.example.com
Port: 587
User Name: user
Authentication method: Normal Password
Connection Security: STARTTLS

I can seem to get this to work with phpmailer, I've tried several variations but nothing seems to work. here's the code I'm using:

$mail = new phpmailer;

$mail->IsSMTP(); // set mailer to use SMTP
$mail->From        = "email@example.com";
$mail->FromName    = "example.com";
$mail->Host        = "mail.example.com";  // specify main and backup server
$mail->SMTPSecure  = 'tls';
$mail->SMTPDebug   = 1;
$mail->Port        = 587;                    // set the SMTP port for the server
$mail->SMTPAuth    = false; 
$mail->Username    = "user";
$mail->Password    = "password";
$mail->CharSet     = 'ISO-8859-1';  // so it interprets foreign characters
$mail->AddAddress("otheremail@example.com", "Other");
$mail->AddReplyTo("email@example.com", "example.com");

$mail->IsHTML(true);    // set email format to HTML
$mail->Subject = "Here is the subject";
$mail->Body = "This is the message body";
$mail->Send(); // send message

This code gives me this error:

SMTP -> ERROR: MAIL not accepted from server: 530 5.7.0 Must issue a STARTTLS command first

SMTP -> ERROR: RCPT not accepted from server: 530 5.7.0 Must issue a STARTTLS command first

SMTP -> ERROR: RCPT not accepted from server: 530 5.7.0 Must issue a STARTTLS command first

SMTP -> ERROR: DATA command not accepted from server: 530 5.7.0 Must issue a STARTTLS command first

    For one, you're setting SMTPAuth to false even though you're specifying a username and password. That's rather contradictory.

    As for the problem at hand, try changing the SMTPSecure value to "ssl" instead. You might also try prepending 'ssl://' to the host value.

    If none of that helps... are you using the latest version of PHPMailer?

    EDIT: Also, welcome to PHPBuilder!

      Thanks for the quick reply and the welcome.

      I tried both true and false for SMTPAuth.

      When I change the port to 465, SMTPAuth to true, SMTPSecure to ssl I get the following error in the mail.log file:

      postfix/smtpd[8089]: warning: TLS library problem: 8089:error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol:s23_srvr.c:578:

        Clarification:
        (I changed the name of the server and IPs in the following code)

        $mail = new phpmailer;

        $mail->IsSMTP(); // set mailer to use SMTP
        $mail->From = "email@example.com";
        $mail->FromName = "example.com";
        $mail->Host = "mail.example.com"; // specify main and backup server
        $mail->SMTPSecure = 'ssl';
        $mail->SMTPDebug = 1;
        $mail->Port = 465; // set the SMTP port for the server
        $mail->SMTPAuth = true;
        $mail->Username = "user";
        $mail->Password = "password";
        $mail->CharSet = 'ISO-8859-1'; // so it interprets foreign characters
        $mail->AddAddress("otheremail@example.com", "Other");
        $mail->AddReplyTo("email@example.com", "example.com");

        $mail->IsHTML(true); // set email format to HTML
        $mail->Subject = "Here is the subject";
        $mail->Body = "This is the message body";
        $mail->Send(); // send message

        With the above settings, when set to Port 465, PHPMailer give me these errors:

        SMTP -> ERROR: HELO not accepted from server:
        SMTP -> NOTICE: EOF caught while checking if connected

        mail.log give me:
        postfix/smtpd[8089]: warning: TLS library problem: 8089:error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol:s23_srvr.c:578:

        When I change the port to 587, I get:

        SMTP -> ERROR: MAIL not accepted from server: 530 5.7.0 Must issue a STARTTLS command first
        SMTP -> ERROR: RCPT not accepted from server: 530 5.7.0 Must issue a STARTTLS command first
        SMTP -> ERROR: RCPT not accepted from server: 530 5.7.0 Must issue a STARTTLS command first
        SMTP -> ERROR: DATA command not accepted from server: 530 5.7.0 Must issue a STARTTLS command first

        mail.log:
        postfix/smtpd[8380]: connect from example.com[ip.ip.ip.ip]
        postfix/smtpd[8380]: disconnect from example.com[ip.ip.ip.ip]

          I'm sorry for the multiple posts, but there doesn't seem to be a way to edit my posts.

          When I set the server to: ssl://mail.example.com, I get:

          SMTP -> ERROR: Failed to connect to server: (0)
          Mailer Error

          Description:
          SMTP Error: could not connect to SMTP host server(s)

          Yes, I have the newest version of PHPmailer.

          Thanks!

            5 days later

            You might also try

            $mail->SMTPSecure = "tls";

            I recall having a very difficult time using PHPMailer to send email via gmail. I solved the problem by using PEAR::Mail.

              Write a Reply...