使用PHP向gmail帐户发送电子邮件


Sending Email to gmail account using PHP

我正在尝试从我的网站向gmail帐户发送邮件。似乎在代码级别上一切都很好。你们能调查一下这个问题吗:即使我运行这个脚本,gmail帐户也不会收到邮件。

 <?php
        if(isset($_POST['submit'])){
        ini_set('SMTP','localhost'); 
        $msg='Name : '.$_POST['name']."'n"
                .'Email : '.$_POST['email']."'n"
                .'Message : '.$_POST['message'];
                mail("aa@gmail.com","Message from Contact Us",$msg);
                     }
        else{
               echo 'cannot send email';
            }
        ?>

我想你会安装phpmailer(http://pear.php.net/)然后通过smtp发送,这里是示例代码:

require_once "Mail.php";
$from = '<from@gmail.com>';
$to = '<to@yahoo.com>';
$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>');