Setting fungsi email kat Xampp mengunakan akaun gmail

Salam Aidilfitri!

Ramai yang tanya boleh tak send email menggunakan xampp yang kita install kat pc/laptop kita. Jawapannya boleh dengan syarat anda ada internet akses. Jadi untuk kali ini, PakCu akan buat sedikit tutorial bagaimana untuk membuat tetapan agar script email yang kita buat di localhost (menggunakan xampp) dapat menghantar email dengan menggunakan akaun gmail anda.

Buka fail sendmail.ini (C:\xampp\sendmail) anda. Buat tetapan seperti berikut:

[sendmail]
smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=username@gmail.com
auth_password=katalaluan
force_sender=username@gmail.com
smtp_ssl=auto

Kod-kod yang lain dalam fail sendmail.php tu boleh delete atau save fail berkenaan dengan nama lain (untuk backup jika ada permasalahan nanti).

Ok! Sekarang bukak fail php.ini (C:\xampp\php) pula dan komen (disable) kod seperti berikut:

[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
; SMTP = localhost
; smtp_port = 25

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

; XAMPP IMPORTANT NOTE (1): If XAMPP is installed in a base directory with spaces (e.g. c:\program filesD:\xampp) fakemail and mailtodisk do not work correctly.
; XAMPP IMPORTANT NOTE (2): In this case please copy the sendmail or mailtodisk folder in your root folder (e.g. C:\sendmail) and use this for sendmail_path.

; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
sendmail_path = “\”C:\xampp\sendmail\sendmail.exe\” -t”

; XAMPP: Comment out this if you want to work with mailToDisk, It writes all mails in the D:\xampp\mailoutput folder
; sendmail_path = “C:\xampp\mailtodisk\mailtodisk.exe”

; 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”

Jika diperhatikan, hanya baris ‘sendmail_path = “\”C:\xampp\sendmail\sendmail.exe\” -t”‘ dan ‘mail.add_x_header = Off’ sahaja yand diaktifkan (enable). Save fail berkenaan dan restart apache anda.

Jadi tetapan untuk fungsi email menggunakan sendmail telah kita buat. Sekarang kita buat contoh script mail pula. Buat satu fail baru dan namakan test.php dan simpan dalam folder web root (htdocs) anda. Masukkan kode di bawah:

<?php
$from = “Abu Haziq”;
$headers = “From: “.$from;
$body = “Salam sejahtera!, \nIni adalah cubaan menghantar email!”;
$subject = “Cubaan Menghantar Email”;
$to = “pro@ppukm.ukm.edu.my”;
if  (mail($to, $subject, $body, $headers)) {
echo “Berjaya!”;
}
else {
echo “Gagal!”;
}
?>

p/s: PakCu menggunakan XAMPP v1.8.1

Comments

Published by

PakCu

I'm a simple person