2008-08-30 42 views
11

Trong ứng dụng web PHP của tôi, tôi muốn được thông báo qua email bất cứ khi nào một số lỗi nhất định xảy ra. Tôi muốn sử dụng tài khoản Gmail của mình để gửi chúng. Làm thế nào điều này có thể được thực hiện?Thư PHP bằng cách sử dụng Gmail

Trả lời

8

SMTP-server của Gmail đòi hỏi một cấu hình rất cụ thể.

Từ Gmail help:

Outgoing Mail (SMTP) Server (requires TLS) 
- smtp.gmail.com 
- Use Authentication: Yes 
- Use STARTTLS: Yes (some clients call this SSL) 
- Port: 465 or 587 
Account Name: your full email address (including @gmail.com) 
Email Address: your email address ([email protected]) 
Password:  your Gmail password 

Bạn có thể thiết lập các thiết lập này lên trong Pear::Mail hoặc PHPMailer. Xem tài liệu của họ để biết thêm chi tiết.

4

Bạn có thể sử dụng chức năng email PEAR với của Gmail SMTP Server

Lưu ý rằng khi gửi e-mail sử dụng máy chủ SMTP của Gmail, nó sẽ trông giống như đến từ địa chỉ Gmail của bạn, bất chấp những gì bạn đánh giá cao là với $ từ.

(mã sau đây được lấy từ About.com Programming Tips)

<?php 
require_once "Mail.php"; 

$from = "Sandra Sender <[email protected]>"; 
$to = "Ramona Recipient <[email protected]>"; 
$subject = "Hi!"; 
$body = "Hi,\n\nHow are you?"; 

// stick your GMAIL SMTP info here! ------------------------------ 
$host = "mail.example.com"; 
$username = "smtp_username"; 
$password = "smtp_password"; 
// -------------------------------------------------------------- 

$headers = array ('From' => $from, 
    'To' => $to, 
    'Subject' => $subject); 
$smtp = Mail::factory('smtp', 
    array ('host' => $host, 
    'auth' => true, 
    'username' => $username, 
    'password' => $password)); 

$mail = $smtp->send($to, $headers, $body); 

if (PEAR::isError($mail)) { 
    echo("<p>" . $mail->getMessage() . "</p>"); 
} else { 
    echo("<p>Message successfully sent!</p>"); 
} 
?> 
Các vấn đề liên quan