Wednesday, 15 April 2009

Solving: "Must issue a STARTTLS command first" for PHP's mail function (using Google's SMTP Server)

My ISP previously provided their own SMTP Server. The PHP configuration for sending e-mails was then relatively simple (php.ini file):

[mail function]
; For Win32 only.
SMTP = smtp.freesurf.ch
smtp_port = 25

; For Win32 only.
sendmail_from = <e-mail username>@freesurf.ch

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"

Unfortunately my ISP switched their e-mail accounts to Google's Gmail. I tried to use a similar configuration:

[mail function]
; For Win32 only.
SMTP = smtp.gmail.com
smtp_port = 25

; For Win32 only.
sendmail_from = <e-mail username>@gmail.com

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"

The problem was that I kept getting the following message from Google's SMTP Server:
"530 5.7.0 Must issue a STARTTLS command first."

The reason for this is that gmail uses secure (TLS) servers to prevent spam.

There are several ways to resolve this problem (one way is by installing a SMTP server on your localhost) but here is the easiest way I found:

As I was using XAMPP I had already installed the "fake sendmail for windows". If you are not using XAMPP you can download it here:
http://glob.com.au/sendmail/sendmail.zip

I then modified the php.ini file to use it (commented out the other lines):

[mail function]
; For Win32 only.
; SMTP = smtp.gmail.com
; smtp_port = 25

; For Win32 only.
; sendmail_from = <e-mail username>@gmail.com

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"

Ignore the "For Unix only" comment, as this version of sendmail works for Windows.

You then have to configure the "sendmail.ini" file in the directory where sendmail was installed:

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=<username>
auth_password=<password>
force_sender=<e-mail username>@gmail.com


The configuration is nearly over but not completely, you will still get the message: "Must issue a STARTTLS command first".

To solve this problem you need to download the following zip file:
http://glob.com.au/sendmail/sendmail-SSL.zip
and install its contents in the sendmail directory. That will make sendmail.exe TLS compatible.

That's it. I hope this will be useful to some people.

Please bookmark, your votes are noticed and appreciated:
Bookmark and Share

27 comments:

  1. pretty good job, helped me a lot!

    Thanks!

    ReplyDelete
  2. Please, I don't know anything about configuring the ""sendmail.ini" file in the directory where sendmail was installed" or "using XAMPP" or installing "fake sendmail for windows". Is there an easier way (or at least a clearer way to explain how) to fix this problem with Google Mail?

    BTW, I have one Gmail account that works fine, but the second one gives me the STARTTLS message.

    ReplyDelete
  3. i don't get any errors when i'm running my php script that simply sends an email to m gmail account but the mail never comes!!

    Any help?

    ReplyDelete
  4. Hi

    Its working for sendmail , In my case i am having a dedicated server from 1and1.com and they gave email server for free (not configured in dedicated server , separately configgured in 1and1.com). for this how i have to write the code

    please help me out , thanks in advance

    Regards
    Arun

    ReplyDelete
  5. This solved my sky broadband issue, thank you :)

    ReplyDelete
  6. It has saved me. Thanks a lot!

    ReplyDelete
  7. After a couple of days hard work, trying, reinstalling, reading, searching - Heureka !!!
    THANK YOU for a very comprehensive yet simple to use solution !!!

    ReplyDelete
  8. Oh man. Thanks a lot. I also figured out that few ISPs block 25. Cheap trick. You should try 587 at those places. You saved a lot of time for me. Thanks again.

    ReplyDelete
  9. Thanks SO much.. you resolved an age old problem for me! I hope it lasts :)

    ReplyDelete
  10. Thank you guys. With this help now the error :"Must issue a STARTTLS command first" has been disappear. But another error comes up : "Socket Error # 10054Connection reset by peer."

    What I have to do? I use port 587 for smtp.gmail.com . Is it wrong? I've try the other port ; 25 and 465, but both was failed. Which port exactly for smtp.gmail.com?

    ReplyDelete
  11. Thank you soo much!!

    I had this prob for a long time and you really helped me!

    Thanks!

    - Noam from The Netherlands

    ReplyDelete
  12. i am not getting http://glob.com.au/sendmail/sendmail-SSL.zip please help me thanks in advance

    ReplyDelete
  13. you no longer need sendmail-SSL.zip, the SSL support files are included in the main zip (read the changelog.)

    ReplyDelete
  14. Mine currently is letting the form details through as an email to my account.
    I have a php file linked to a form.htm file where details are entered and my sendmail.ini file is as below

    # Example for a user configuration file

    # Set default values for all following accounts.
    defaults
    logfile "\xampp\sendmail\sendmail.log"

    # Mercury
    account Mercury
    host localhost
    from postmaster@localhost
    auth off

    # A freemail service example
    account Hotmail
    tls on
    tls_certcheck off
    host smtp.live.com
    auth on
    user [exampleuser]@hotmail.com
    password [examplepassword]

    # Set a default account
    account default : Mercury

    How should I edit this?

    ReplyDelete
  15. You are the greatest mannnnnnnnnnnnnnnnnnn
    You saved me a lot of trouble............
    Thank you mannnnnnnnnnnn
    May God Bless You......................

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. Am still getting an issue in error.log file( 530 5.7.0 Must issue a STARTTLS command first. s1sm2202918iba.7 )
    Can any one help please?

    ReplyDelete
  18. I have done absolutely EVERYTHING i have seen on the web. When following the steps i found here i started to have data inserted into my debug and error files, and crash files started to appear. Thats a good sign. However, the same message over and over again: "Must issue a STARTTLS command first. r10sm898373ybl.24" (actually, the number at the end seems to be random each time). Anybody could actually fix this?

    ReplyDelete
  19. Thank you, You made my day :D

    @sofi: eh?
    i guess you can check the sendmail.ini file
    smtp_server=smtp.gmail.com
    smtp_port=465
    smtp_ssl=auto

    Those are my settings now
    But i'll assume you fixed it by yourself.

    ReplyDelete
  20. I'd like to add that some servers may need issuing EHLO command again after STARTTLS command. More information can be found here: http://forum.powweb.com/showthread.php?t=73406 - this post saved my life ;)

    ReplyDelete
  21. sendmail-SSL.zip link is broken, can you provide the correct link?

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete
  23. sendmail-SSL.zip link is broken, can you provide the correct link?
    same problem here...

    ReplyDelete
  24. The correct link is http://glob.com.au/sendmail/ --> Download sendMail. I extracted everything in this folder except of the ini file and everything is ok.

    I would like to mention that - in the php ini file - the send mail path is:

    sendmail_path = "D:\xampp\sendmail\sendmail.exe -t"

    and not sendmail_path = “\”D:\xampp\sendmail\sendmail.exe\” -t”.

    Thanks a lot!! ;)

    ReplyDelete
  25. thank you very much :)
    my problem solved

    ReplyDelete
  26. how to slove this problem?
    please help me.

    Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. wi6sm12115494pbc.22 - gsmtp in C:\wamp\www\work\SendMail.php on line 9

    ReplyDelete
  27. How do I go about this?

    Warning: mail() [function.mail]: SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. pi6sm33180978wic.3 - gsmtp in C:\xampp\htdocs\sendmail\sendmail.php on line 34

    ReplyDelete