Thứ Tư, 4 tháng 11, 2009

Gởi email text, HTML và tiếng Việt Unicode

HAM MAIL() C?A PHP


Send email trong PHP r?t don gi?n, ta ch? c?n dung duy nh?t 1 ham mail() du?c PHP cung c?p s?n la d?. Cu phap c?a ham mail() nhu sau:


bool mail(string to, string subject, string message[, string additional_headers])


Cac tham s? c?a ham mail() co y nghia nhu sau:



  • to - d?a ch? ngu?i nh?n email, la 1 string. Cac gia tr? h?p l? c?a tham s? to co d?ng nhu sau:
    'user@domain.com'
    'user1@domain.com, user2@domain.com'
    'User Name <user@example.com>'
    'User Name 1 <user1@example.com>, User Name 2 <user2@example.com>'
    'User Name 1 <user@example.com>, user2@domain.com, User Name 3 <user3@example.com>'

  • subject - tieu d? c?a email, la 1 string. Tieu d? c?a email khong du?c ch?a ky t? xu?ng dong (\r ho?c \n).

  • message - n?i dung email, la 1 string. N?i dung email co th? do nhi?u dong ghep l?i v?i nhau, m?i dong du?c phan cach b?ng ky t? \n.

  • additional_headers - (tu? ch?n) dung d? chen them cac header vao email. Cac header nay khong n?m trong ph?n n?i dung c?a email ma dung d? qu?n ly vi?c g?i email (vi d? chen them cac tru?ng CC, BCC khi g?i email). Nhi?u header co th? cung ghep l?i thanh 1 string trong additional_headers, cac header du?c phan cach nhau b?ng chu?i \r\n.

 


Ham mail() s? tr? v? gia tr? TRUE n?u nhu email du?c ch?p nh?n g?i di va FALSE trong tru?ng h?p ngu?c l?i.
Luu y: Ham mail() tr? v? gia tr? TRUE khong co nghia la email s? du?c g?i d?n noi nh?n thanh cong. Gia tr? TRUE du?c tr? v? ch? co y nghia la email du?c server ch?p nh?n g?i di, con khi nao thi email th?c s? du?c g?i, g?i co t?i noi hay khong thi chuong trinh khong bi?t du?c (nhu v?y n?u b?n nh?p sau d?a ch? ngu?i nh?n ? tham s? To thi s? khong co cach nao chuong trinh bi?t du?c d?a ch? To co dung ho?c t?n t?i hay khong!).


M?T VI D? G?I EMAIL V?I HAM MAIL()


Ta hay t?o 1 chuong trinh PHP nh? d? g?i email nhu sau:

//n?i dung file example1.php
<?php
$from = 'from@domain.com';
$to = 'to@domain.com';
$subject = 'Example 1: Send simple text email';
$message = 'A plain text email.';
$header = "From: $from\r\nReply-to: $from";
 
if ( mail($to, $subject, $message, $header) ) {
echo "Email sent to $to!";
} else {
echo "Error occured while sending email to $to!";
} //end if
?>

 


Luu chuong trinh vao file example1.php va ch?y th?. N?u qua trinh g?i email thanh cong, b?n s? nh?n du?c cau thong bao "Email sent to to@domain.com!", n?u co l?i b?n s? nh?n du?c cau thong bao "Error occured while sending email to to@domain.com!"


Luu y:



  • Nh? thay to@domain.com va from@domain.com thanh d?a ch? email c?a b?n tru?c khi ch?y th? chuong trinh!

  • Co th? ph?i m?t t?i 5-10 phut email m?i t?i noi, va b?n nh? ki?m tra thu m?c Bulk email vi nhi?u khi email g?i t?i b? d? trong m?c Bulk.

 


Trong cu phap c?a ham mail(), tham s? additional_headers la tu? ch?n, t?c la b? qua cung du?c. Tuy nhien, tren th?c t? b?n nen cung c?p tham s? additional_headers cho ham mail() v?i it nh?t 2 header From va Reply-to. Ly do thi co nhi?u, m?t trong s? cac ly do ph?i cung c?p header From va Reply-to la vi n?u ph?n d?a ch? ngu?i g?i (From) la r?ng, cac chuong trinh l?c email co th? cho r?ng day la emai spam va th? la email du?c t? d?ng chuy?n sang "Thung rac"!.


G?I HTML EMAIL V?I HAM MAIL()


Chung ta da g?i du?c email text v?i ham mail(), nhung nhi?u luc ta c?n "trang di?m" mau me cho n?i dung email du?c b?t m?t hon. Th? vi?t 1 chuong trinh nh? g?i email HTML:

//n?i dung file example2.php
<?php
$from = 'from@domain.com';
$to = 'to@domain.com';
$subject = 'Example 2: Try a simple HTML email';
$message = 'A HTML email: <b>bold</b>, <i>italic</i>, <u>underline</u>.';
$header = "From: $from\r\nReply-to: $from";
 
if ( mail($to, $subject, $message, $header) ) {
echo "Email sent to $to!";
} else {
echo "Error occured while sending email to $to!";
} //end if
?>

 


Luu chuong trinh vao file example2.php va ch?y th?. Email g?i thanh cong, t?i noi an toan, nhung khi m? ra xem thi n?i dung email l?i nhu v?y:
A HTML email: <b>bold</b>, <i>italic</i>, <u>underline</u>.
Th? co t?c khong?


Th?c ra b?n ch? c?n s?a l?i chuong trinh "1 chut xiu" la dau vao d?y ngay:

//n?i dung file example3.php
<?php
$from = 'from@domain.com';
$to = 'to@domain.com';
$subject = 'Example 3: Send HTML email';
$message = 'A HTML email: <b>bold</b>, <i>italic</i>, <u>underline</u>.';
$header = "Content-type: text/html\r\nFrom: $from\r\nReply-to: $from";
 
if ( mail($to, $subject, $message, $header) ) {
echo "Email sent to $to!";
} else {
echo "Error occured while sending email to $to!";
} //end if
?>

 


Luu chuong trinh vao file example3.php va ch?y th?. L?n nay thi n?i dung email nh?n du?c nhu mong mu?n:
A HTML email: bold, italic, underline..


Nhu v?y b?n s? nh?n th?y r?ng g?i HTML email cung don gi?n khong khac gi g?i text email thong thu?ng. Ch? c?n them 1 header Content-type: text/html la n?i dung email s? du?c hi?u la HTML ngay.


Luu y: Rieng ph?n tieu d? (subject) c?a email v?n ph?i la text thong thu?ng! B?n khong th? s? d?ng ma HTML trong ph?n tieu d? (subject) c?a email du?c. Vi th?c ch?t ph?n tieu d? c?a email cung la 1 header d?t bi?c ch? khong n?m trong ph?n n?i dung chinh c?a email.


G?I EMAIL V?I TI?NG VI?T UNICODE


G?i email ti?ng Vi?t Unicode cung y chang nhu g?i email HTML vi b?n than HTML da h? tr? Unicode r?i, ta ch? c?n chu y d?t charset c?a email cho dung la du?c:

//n?i dung file example3.php
<?php
$from = 'from@domain.com';
$to = 'to@domain.com';
$subject = 'Example 4: Send Unicode email';
$message = 'Email ti?ng Vi?t Unicode: <b>in d?m</b>, <i>in nghieng</i>, <u>g?ch chan</u>.';
$header = "Content-type: text/html; charset=utf-8\r\nFrom: $from\r\nReply-to: $from";
 
if ( mail($to, $subject, $message, $header) ) {
echo "Email sent to $to!";
} else {
echo "Error occured while sending email to $to!";
} //end if
?>

 


Luu chuong trinh vao file example4.php va ch?y th?: email g?i thanh cong, t?i noi an toan va khi d?c thi hi?n th?...ti?ng ?-r?p!!! Co gi sai ? day chang?


Th?c ra khong co gi ph?i lo l?ng c?. Khi g?i email ti?ng Vi?t Unicode b?n c?n chu y m?t s? di?m sau:



  • D?t charset cho email. Charset nay ph?i nen trung v?i charset c?a file ma ngu?n PHP va website c?a b?n. Trong vi d? ? tren, ta d?t charset cho email la utf-8 thi b?n cung ph?i luu file example4.php len dia v?i charset la utf-8. B?n xem m?c "LUU FILE V?I TI?NG VI?T UNICODE" ? bai vi?t Gi?i thi?u - Cac bu?c chu?n b? c?n thi?t d? tim hi?u them v? luu file ma ngu?n PHP len dia v?i charset utf-8.

  • Cho du b?n da d?t charset dung r?i, v?n co th? email khong hi?n th? dung ti?ng Vi?t khi d?c! Vi quy?n hi?n th? email nhu th? nao la do chuong trinh email client quy?t d?nh. N?u b?n d?c email b?ng Outlook ch?ng h?n, thi Outlook s? t? d?ng nh?n bi?t charset c?a email va hi?n th? dung ti?ng Vi?t. N?u b?n d?c email tren web (nhu Yahoo, Hotmail...) thi co th? b?n ph?i t? tay ch?n l?i Encoding la utf-8 tren trinh duy?t vi cac web email nhu Yahoo, Hotmail s? dung charset m?c d?nh c?a minh va b? qua charset du?c thi?t l?p trong email.

Va tuong t? nhu khi g?i HTML email, b?n d?ng nen ghi ti?ng Vi?t vao ph?n tieu d? (subject) c?a email! Vi nhu da noi ? ph?n tru?c, tieu d? (subject) c?a email cung la 1 header d?t bi?c ch? khong n?m trong ph?n n?i dung chinh c?a email.


 


M?T S? L?I HAY G?P V?I HAM MAIL()


Email g?i khong t?i noi
Ham mail() tr? v? gia tr? TRUE, d?a ch? dung, n?i dung dung, noi chung m?i th? d?u dung nhung email ch? hoai khong th?y t?i! Ki?m tra Bulk email cung khong th?y luon! L?i ? dau he? Nguyen nhan co th? x?y ra ? 3 ch?:



  • mail server - server c?a b?n khong cho phep account c?a b?n g?i email, ho?c gi?i h?n ch? cho phep g?i s? lu?ng gi?i h?n email trong 1 kho?ng th?i gian nao do. B?n c?n h?i ngu?i qu?n ly server c?a b?n v? quy?n g?i email v?i PHP tren server.
    N?u v?n d? khong ph?i n?m ? ch? server khong cho phep b?n g?i email, ho?c chinh b?n la ngu?i qu?n ly server thi b?n ki?m tra ti?p 1 trong 2 nguyen nhan sau:

  • *NIX server (server ch?y cac h? di?u hanh UNIX ho?c Linux) - PHP m?c d?nh s? s? d?ng l?nh sendmail co s?n tren h? th?ng d? g?i email (v?i tham s? -t -i). M?c d?nh l?nh sendmail s? n?m trong /usr/sbin/sendmail ho?c trong 1 thu m?c nao do trong PATH c?a h? th?ng. N?u tren server c?a b?n l?nh sendmail n?m ? ch? khac v?i thu m?c m?c d?nh (vi d? tren server c?a b?n sendmail n?m ? /usr/local/secretbin/sendmail) thi b?n co th? lam nhu sau:
    - Tim va m? file php.ini ra.
    - Tim chu?i "sendmail", b?n s? th?y 2 dong tuong t? nhu sau:
    ; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
    ;sendmail_path =

    - B?n s?a l?i 2 dong do nhu sau:
    ; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
    sendmail_path = /usr/local/secretbin/sendmail -t -i

    Nh? b? ky t? ch?m ph?y (;) ? d?u dong sendmail_path =!
    Luu y: C?u hinh c?a PHP t?i day da dung va d? d? g?i email tren server *NIX. Tuy nhien v?n co th? server khong g?i email du?c! L?i luc nay la do b?n than c?u hinh c?a server ho?c qua trinh cai d?t va c?u hinh chuong trinh sendmail b? l?i. Cach ki?m tra nguyen nhan va s?a l?i n?m ngoai ph?m vi c?a PHP, b?n co th? d?t cau h?i len di?n dan CLB ngu?i s? d?ng Linux d? du?c giup d?!

  • Windows server - Tren Windows, PHP c?n ph?i dung 1 SMTP server d? g?i email. B?n tim va m? file php.ini ra, tim chu?i sendmail, g?n do b?n s? th?y vai dong nhu sau:
    [mail function]
    ; For Win32 only.
    SMTP = localhost
    smtp_port = 25;

    Cac dong tren cho ta bi?t PHP s? dung SMTP server localhost tren port 25 d? g?i email. R?t co th? b?n chua cai d?t va c?u hinh dung 1 SMTP tren localhost d? PHP co th? s? d?ng va g?i email. B?n hay tham kh?o bai vi?t nay d? cai d?t 1 SMTP server tren localhost va dung no d? g?i email v?i PHP.

 


N?i dung email b? d?t do?n
N?u b?n nh?n du?c email nhung n?i dung b? d?t do?n khong d?y d? thi co th? do 2 nguyen nhan sau:



  • Ma HTML trong email b? sai - N?u b?n g?i email HTML thi b?n nen chu y ki?m tra cu phap HTML c?a n?i dung email. Gi? s? b?n d?nh g?i email v?i n?i dung:
    <b>in d?m</b>
    Nhung ch?ng may b?n ghi sai thanh <b in d?m</b>
    thi khi d?c, email s? hi?n th? b? sai (co th? la m?t ch?, d?t do?n, v.v...).

  • Co 1 dong ch?a 1 d?u ch?m (.) ? d?u - Khi b?n g?i email tren Windows dung SMTP server, n?u n?i dung email co 1 dong nao do ch?a 1 d?u ch?m (ky t? .) ? d?u dong thi ky t? do s? b? b? qua, dong do s? b? b? qua, ho?c toan b? n?i dung email t? dong do tr? di s? b? b? qua. Ly do la vi dong ch?a 1 d?u ch?m ? d?u chinh la d?u hi?u bao cho SMTP server bi?t da k?t thuc n?i dung c?a email. D? kh?c ph?c, tru?c khi g?i email, ta thay th? d?u ch?m ? d?u dong b?ng 2 d?u ch?m:
    $message = str_replace("\n.", "\n..", $message);

 


CAC HAM PHP DU?C DUNG TRONG VI D?



TAI LI?U THAM KH?O



( Trich t?i diendantinhoc.net. Tham kh?o bai g?c  G?i email text, HTML va ti?ng Vi?t Unicode )

men who want to have a babyplanning for baby How do you get pregnant can you get pregnant right after having a babyhaving a baby at 16

Không có nhận xét nào:

Đăng nhận xét