无法从PHP发送电子邮件

时间:2014-05-07 07:18:57

标签: php email

我使用PHP版本5.4.7,赢得7 x64,localhost

这是我的代码:

mail("my_gmail@gmail.com",$subject,$message,"From: $user\n");

我已更改档案php.ini
1.删​​除extension=php_openssl.dll中的半音 2.转入

[mail function]   
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = my-gmail-id@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

并将sendmail.ini中的所有现有代码替换为

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log  
debug_logfile=debug.log
auth_username=my-gmail-id@gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id@gmail.com

但我仍然无法收到电子邮件,任何人都知道我的错误?plz

3 个答案:

答案 0 :(得分:0)

就像Karl所说,Google使用的SMTP端口是465,而不是587。

答案 1 :(得分:0)

我认为您缺少加密类型(ssl / tls)。因为gmail正在使用(ssl)加密。

答案 2 :(得分:0)

如果您可以使用第三方库,以下是如何实施的示例:

(两者都适用于TLS和SSL)

1. PHPMailer

$mailSubj = "Here is the subject";
$mailBody = "This is the HTML message body <b>in bold!</b>";
$mailText = "This is the body in plain text for non-HTML mail clients";
$mailAddr = "myclient@gmail.com";
$mailName = "My Client";

require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';

    //ssl using 465, it is work
    $mail->Port = 465;
    $mail->SMTPSecure = 'ssl';

    //tls using 587, it also work too
    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';

    $mail->SMTPAuth = true;
    $mail->Username = 'your_email@gmail.com';
    $mail->Password = 'your_password';

    $mail->From = 'your_email@gmail.com';
    $mail->FromName = 'Admin88hk';
    $mail->addAddress($mailAddr, $mailName);

    $mail->CharSet="UTF-8";
    $mail->WordWrap = 50;
    $mail->isHTML(true);

    $mail->Subject = "PHPMailer - " . $mailSubj;
    $mail->Body    = $mailBody;
    $mail->AltBody = $mailText;
$mail->send();

2. Zend Framework(版本1.12)

$mailSubj = "Here is the subject";
$mailBody = "This is the HTML message body <b>in bold!</b>";
$mailText = "This is the body in plain text for non-HTML mail clients";
$mailAddr = "myclient@gmail.com";
$mailName = "My Client";

require_once("Zend/Mail.php");
require_once("Zend/Mail/Transport/Smtp.php");

$mailServer = 'smtp.gmail.com';

$mailServerCfgSsl = array(
        'ssl' => 'ssl',
        'port' => 465,
        'auth' => 'login',
        'username' => 'your_email@gmail.com',
        'password' => 'your_password'
    );

$mailServerCfg = array(
        'ssl' => 'tls',
        'port' => 587,
        'auth' => 'login',
        'username' => 'your_email@gmail.com',
        'password' => 'your_password'
    );

//ssl using 465, it is work
$tr = new Zend_Mail_Transport_Smtp($mailServer, $mailServerCfgSsl);

//tls using 587, it also work too
$tr = new Zend_Mail_Transport_Smtp($mailServer, $mailServerCfg);

Zend_Mail::setDefaultTransport($tr);


$mail = new Zend_Mail('UTF-8');
    $mail->setSubject( "Zend - " . $mailSubj );
    $mail->setBodyHtml( $mailBody );
    $mail->setBodyText( $mailText );
    $mail->addTo( $mailAddr , $mailName );
$mail->send();