2008-08-30 8 views
11

PHP webappでは、特定のエラーが発生したときに電子メールで通知したいと考えています。 Gmailアカウントを使用して送信したいと考えています。これはどうすればできますか?Gmailを使用したPHPメール

答えて

8

GmailのSMTPサーバーは、非常に特殊な設定が必要です。 Gmail helpから

:あなたはおそらくPear::MailまたはPHPMailerでこれらの設定を設定することができ

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 

。詳細は、ドキュメントを参照してください。

4

あなたは、GmailのSMTPサーバーを使用して電子メールを送信するとき、それはあなたのGmailアドレスから来たように、それは何を重視することから$のためにあるにもかかわらず、見ていきますことをGmailのSMTPサーバ

注意してPEARのメール機能を使用することができます。

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>"); 
} 
?>