Asked  7 Months ago    Answers:  5   Viewed   52 times

SMTP server response: 530 5.7.0 Must issue a STARTTLS command first

I get this error message when i use mail() function in php script file...

I m using gmail SMTP server and gmail using STARTTLS which is secure SSL and i already use these commands in my contact.php file

ini_set("SMTP","smtp.gmail.com");
ini_set("sendmail_from","<email-address>@gmail.com>");

so what command i can use to enable STARTTLS or configure in php,ini file??

 Answers

93

First, make sure you PHP installation has SSL support (look for an "openssl" section in the output from phpinfo()).

You can set the following settings in your PHP.ini:

ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
Wednesday, March 31, 2021
 
Bere
answered 7 Months ago
80

It sounds like your web host is blocking outbound connections to smtp.gmail.com:465. Suggestions:

  1. Verify: If you have shell/terminal access to your web hosting server, try a telnet test to verify that they are in fact blocking this. Run telnet smtp.gmail.com 465

  2. Contact: Call or email your hosting provider and find out what SMTP server they provide for outbound relay. Make sure they know you want to use your @gmail.com address as the From/Reply-to address.

  3. Update code: Once your host provides you with a different mail server, update your code and try again.

If your web host doesn't allow outbound relay from its servers at all, then you need to look at switching hosts, if this is a requirement for your application.

Saturday, May 29, 2021
 
lewiguez
answered 5 Months ago
92

You are probably attempting to use Gmail's servers on port 25 to deliver mail to a third party over an unauthenticated connection. Gmail doesn't let you do this, because then anybody could use Gmail's servers to send mail to anybody else. This is called an open relay and was a common enabler of spam in the early days. Open relays are no longer acceptable on the Internet.

You will need to ask your SMTP client to connect to Gmail using an authenticated connection, probably on port 587.

Thursday, June 17, 2021
 
steros
answered 4 Months ago
39

I used Alexander Pomozov's solution to talk to Gmail from my Rails app. I believe the original article is gone but someone has reproduced the Google cache over here.

lib/smtp_tls.rb

require "openssl"
require "net/smtp"

Net::SMTP.class_eval do
  private
  def do_start(helodomain, user, secret, authtype)
    raise IOError, 'SMTP session already started' if @started
    check_auth_args user, secret, authtype if user or secret

    sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
    @socket = Net::InternetMessageIO.new(sock)
    @socket.read_timeout = 60 #@read_timeout
    #@socket.debug_output = STDERR #@debug_output

    check_response(critical { recv_response() })
    do_helo(helodomain)

    if starttls
      raise 'openssl library not installed' unless defined?(OpenSSL)
      ssl = OpenSSL::SSL::SSLSocket.new(sock)
      ssl.sync_close = true
      ssl.connect
      @socket = Net::InternetMessageIO.new(ssl)
      @socket.read_timeout = 60 #@read_timeout
      #@socket.debug_output = STDERR #@debug_output
      do_helo(helodomain)
    end

    authenticate user, secret, authtype if user
    @started = true
  ensure
    unless @started
      # authentication failed, cancel connection.
      @socket.close if not @started and @socket and not @socket.closed?
      @socket = nil
    end
  end

  def do_helo(helodomain)
    begin
      if @esmtp
        ehlo helodomain
      else
        helo helodomain
      end
    rescue Net::ProtocolError
      if @esmtp
        @esmtp = false
        @error_occured = false
        retry
      end
      raise
    end
  end

  def starttls
    getok('STARTTLS') rescue return false
    return true
  end

  def quit
    begin
      getok('QUIT')
    rescue EOFError, OpenSSL::SSL::SSLError
    end
  end
end

config/environment.rb

(add after everything else)

    require “smtp_tls”

    ActionMailer::Base.smtp_settings = {
    :address => “smtp.gmail.com”,
    :port => 587,
    :authentication => :plain,
    :user_name => “someone@openrain.com”,
    :password => ’someonesPassword’
    } 

Use ActionMailer as normal.

Thursday, July 29, 2021
 
Markol
answered 3 Months ago
55

Your server cannot connect to smtp.gmail.com on port 587. I have the same problem from a testing tool:

Resolving hostname...
Connecting...
SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
Message sending failed.

From my local machine, it works perfectly:

Trying 74.125.195.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP be3sm8900765wib.21 - gsmtp

Maybe there are temporary problems at Google from the US (I am in Germany here) or something like this. There is no obvious mistake in your code. You can only try it again later or with another SMTP server.

Furthermore, you can try to connect with 74.125.195.108 directly (just deactivate SSL certificate validation).


EDIT: Just also try tls://smtp.gmail.com as host.

Saturday, August 7, 2021
 
mertak
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :