2016-04-29 141 views
-1

我想從我的網站發送郵件到Gmail帳戶。看來代碼級別的一切都很好。你們可以請你看看這個問題:即使我運行這個腳本,郵件沒有收到Gmail帳戶。使用PHP發送電子郵件到Gmail帳戶

<?php 
     if(isset($_POST['submit'])){ 
     ini_set('SMTP','localhost'); 
     $msg='Name : '.$_POST['name']."\n" 
       .'Email : '.$_POST['email']."\n" 
       .'Message : '.$_POST['message']; 

       mail("[email protected]","Message from Contact Us",$msg); 
        } 

     else{ 
       echo 'cannot send email'; 
      } 

     ?> 
+0

你在本地安裝SMTP? –

+1

[如何使用PHP發送電子郵件?](http://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php) – Robert

回答

0

我想你將安裝的PHPMailer(http://pear.php.net/),然後通過SMTP發送這裏是示例代碼:

require_once "Mail.php"; 

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

$headers = array(
    'From' => $from, 
    'To' => $to, 
    'Subject' => $subject 
); 

$smtp = Mail::factory('smtp', array(
     'host' => 'ssl://smtp.gmail.com', 
     'port' => '465', 
     'auth' => true, 
     'username' => 'your gmail ', 
     'password' => 'your password' 
    )); 

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

if (PEAR::isError($mail)) { 
    echo('<p>' . $mail->getMessage() . '</p>'); 
} else { 
    echo('<p>Message successfully sent!</p>'); 
相關問題