Rabu, 10 April 2013

PHP send email via GMAIL SMTP from localhost

At this time i want to share about how to send email using sendmail tools (in XAMPP) via GMAIL SMTP from localhost.

First we must download sendmail tools with ssl (GMAIL SMTP recommend to use ssl connection). We can donwload at glob.com.au/sendmail or in my backup here. Then extract the file to C:\\XAMPP\sendmail
Open sendmail.ini and edit code like this
smtp_server=smtp.gmail.com
smtp_port=587

error_logfile=error.log
debug_logfile=debug.log

auth_username=xxx@gmail.com
auth_password=xxx

you can ignore other code in sendmail.ini,

Open php.ini, find word with key "[mail function]" then edit like this
[mail function]
; For Win32 only.
; http://php.net/smtp
;SMTP = localhost

; http://php.net/smtp-port
;smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster@localhost

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

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = Off

; Log all mail() calls including the full path of the script, line #, to address and headers
;mail.log = "C:\xampp\php\logs\php_mail.log"

save php.ini and restart your apache.

Now, test to send an email
<?php
 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers  = 'From: administrator<sender@test.jp>' . "\r\n";
 $headers .= 'Reply To: no-reply@test.jp' . "\r\n";
 $message  = "仮登録いただき、ありがとうございます。";
 mail('receiver', '仮登録完了のお知らせ', $message, $headers); // using sendmail + ssl for send an email.
?>
that's all.
I hope it can help you for send email via GMAIL SMTP from localhost. :)

Sorry about my english :D

Keyword : php mail, sendmail, email

To appreciate the IPR (Intellectual Property Rights), reference sources that I use and I have learned will be displayed.
Referensi    : forums.devshed.com/mail-server-help-111, php.net, stackoverflow.com, blog.rosihanari.net/cara-mengirim-email-dengan-script-php-di-localhost-via-xampp
Author        : Moch. Zawaruddin Abdullah, www.zawaruddin.blogspot.com

7 komentar:

  1. gan tutorial ini pernah km coba praktekin sndiri blm?..klo udh berarti udh berhasil dong,..kan udh bkin postingan. ane dah nyoba berkali2 dari tutorial2 yg ada di google tp blm berhasil. mungkin agan bsa bantu saya...tlg di respon.

    BalasHapus
    Balasan
    1. semua tutorial di blog ane adalah hasil pengalaman ane gan, jd dah ane praktekin...
      dan alhamdulillah berhasil...
      silakan coba dulu gan...

      Hapus
  2. saya coba tapi masih ada error seperti ini

    Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port"

    padahal udah ikutin step by step dan gak ada port 25 yg ter setup , mohon bantuan nya
    terima kasih

    BalasHapus
    Balasan
    1. maaf baru balas, restart dulu gan apache nya....

      Hapus
  3. gan, ane coba juga tetep aja error gan, error nya sama Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port"

    BalasHapus
    Balasan
    1. udah d save php.ini dan udah d restart apachenya?

      Hapus