Mengirim email menggunakan php mail dari localhost

Filed under PHP dan MySQL

Mau aplikasi PHP kamu bisa mengirim email walaupun kamu menjalankannya di localhost dengan OS Windows? Mau tau caranya? silahkan teruskan membacanya :D .

Untuk mengirim email menggunakan fungsi mail dari localhost yang perlu dilakukan adalah mengubah konfigurasi PHP (file php.ini) pada bagian [mail function] seperti berikut.

[mail function]
; For Win32 only.
;SMTP = localhost
;smtp_port = 25
 
; For Win32 only.
;sendmail_from = webmaster@localhost
 
; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path = 
 
; 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 =

Syarat untuk melakukan perubahan ini adalah kamu harus punya smtp server. Bagaimana jika tidak punya? Jika kamu punya website, kamu bisa menggunakan smtp server website kamu, misalnya mail.domain-anda.tld. Pada bagian SMTP ganti nilai localhost dengan mail server kamu, misalnya mail.domain-anda.tld, sedangkan pada bagian smtp_port ganti dengan port smtp server kamu, standarnya 25. Jangan lupa uncomment kedua baris tersebut dengan menghapus tanda titik koma (;) sehingga setingannya akan seperti berikut.

[mail function]
; For Win32 only.
SMTP = mail.domain-anda.tld
smtp_port = 25
 
; For Win32 only.
;sendmail_from = webmaster@localhost
 
; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path = 
 
; 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 =

Simpan dan restart apache kamu dan selamat mencoba. Saya sendiri sudah mencobanya dan berhasil.

Cara yang lain yang bisa digunakan untuk mengirim email menggunakan php mail dari windows adalah dengan menggunakan fake sendmail yang dibuat oleh Byron Jones. Fake sendmail mirip dengan sendmail pada linux. Pertama unduh fake sendmail untuk windows di sini atau di sini, kemudian ekstrak. Lalu buka file sendmail.ini dengan text editor, kemudian cari baris smtp_server, smtp_port. Ganti nilai smtp_server, smtp_port dengan smtp server dan smtp port website kamu. Contohnya seperti berikut.

[sendmail]
 
; you must change mail.mydomain.com to your smtp server,
; or to IIS's "pickup" directory.  (generally C:\Inetpub\mailroot\Pickup)
; emails delivered via IIS's pickup directory cause sendmail to
; run quicker, but you won't get error messages back to the calling
; application.
 
smtp_server=mail.domain-anda.tld
 
; smtp port (normally 25)
 
smtp_port=25

Kemudian ubah konfigurasi PHP (php.ini) pada bagian [mail function] pada baris sendmail_path. Isi nilai sendmail_path dengan “path\to\sendmail.ext -t”. Misalkan kamu menyimpan file sendmail.exe pada folder C:\sendmail\sendmail.exe maka isikan “C:\sendmail\sendmail.exe -t” pada sendmail_path. Contohnya sebagai berikut.

[mail function]
; For Win32 only.
;SMTP = localhost
;smtp_port = 25
 
; For Win32 only.
;sendmail_from = webmaster@localhost
 
; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "C:\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 =

Kemudian restart apache dan siap untuk dicoba.

Dengan menggunakan fake sendmail kamu bisa menggunakan smtp server nya google asalkan kamu punya account google mail (gmail). Caranya ubah nilai smpt_server menjadi smtp.gmail.com, smtp_port menjadi 587 dan juga pada baris auth_username dan auth_password isi dengan email dan password gmail kamu. Contoh konfigurasinya seperti berikut.

[sendmail]
 
; you must change mail.mydomain.com to your smtp server,
; or to IIS's "pickup" directory.  (generally C:\Inetpub\mailroot\Pickup)
; emails delivered via IIS's pickup directory cause sendmail to
; run quicker, but you won't get error messages back to the calling
; application.
 
smtp_server=smtp.gmail.com
 
; smtp port (normally 25)
 
smtp_port=587
 
; the default domain for this server will be read from the registry
; this will be appended to email addresses when one isn't provided
; if you want to override the value in the registry, uncomment and modify
 
;default_domain=mydomain.com
 
; log smtp errors to error.log (defaults to same directory as sendmail.exe)
; uncomment to enable logging
 
error_logfile=error.log
 
; create debug log as debug.log (defaults to same directory as sendmail.exe)
; uncomment to enable debugging
 
;debug_logfile=debug.log
 
; if your smtp server requires authentication, modify the following two lines
 
auth_username=youremail@gmail.com
auth_password=password

Selamat mencoba, jika berhasil selamat ya, jika gagal mungkin ada yang salah dengan konfigurasinya atau kamu belum terhubung dengan internet, silahkan diperiksa dan dicoba lagi :D

9 Comments

  1. Posted 16 Februari 2009 at 10:40 | Permalink

    oo ngono to

  2. Posted 19 Februari 2009 at 07:54 | Permalink

    iyo ternyata mung ngono thok :D

  3. Posted 6 Maret 2009 at 07:55 | Permalink

    oh…. gtu ya. tapi saya masih bingung gimana caranya dapatin file php.ini,
    kalo web na dah di upload gimana caranya ngubah file php.ini?
    thank’s

  4. Posted 7 Maret 2009 at 07:34 | Permalink

    @oka
    kalo udah di upload gak perlu lagi nyeting2 udah otomatis bisa ngirim email. Kalo di shared hosting biasanya kita gak bisa akses php.ini, yang bisa akses hanya administratornya aja.

  5. Posted 2 April 2009 at 17:42 | Permalink

    mas ali….
    keren keren……

  6. zova
    Posted 13 Juni 2009 at 10:41 | Permalink

    thx, akhirnya script php kirim email dr pc local gmail sy berjalan lancar
    thx ya

  7. Posted 11 Agustus 2009 at 00:55 | Permalink

    Mas saya udah setting pake smtp gmail nya semua spt postingnya.
    tp koq ga nyampe ke email tujuan ?
    padahal dr source dibawah ini hasilnya email terkirim..
    Gmana nih mas?

    <?php
     
    $nama_admin="Yudy";
    $email_admin="yudy_nakata@yahoo.co.id";
     
    $headers .= 'To: '.$nama_admin."\r\n";
     
    $send = mail($email_admin, $_POST["subjek"], $_POST["pesan"], $headers);
     
    if ($send)
    echo "Email terkirim";
     
    ?>
  8. Posted 23 Agustus 2009 at 22:17 | Permalink

    @Yudy
    mungkin bisa dilihat di error.log-nya sendmail, fungsi mail di php returnnya emang kadang gak sesuai atau bisa juga emailnya masuk ke folder spam apalagi kalo penerimanya yahoo lebih sering masuk ke folder spam

  9. Posted 12 November 2009 at 14:22 | Permalink

    kenapa tetep ga bs mas, aq ud coba pake cara mas, di script aq keterangannya berhasil tp aq cek di email tujuan ga ada email masuk, aq liat di debug_log keterangannya disconnect to smtp.gmail.com.

    plis help me..

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*