无法从PHP发送电子邮件

黎刹·拉娜(Rizal Lana)

我使用PHP版本5.4.7,赢得7 x64,本地主机,

这是我的代码:

mail("[email protected]",$subject,$message,"From: $user\n");

我已经更改了文件php.ini
1.在extension=php_openssl.dll
2.中删除Semicon,更改为

[mail function]   
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = [email protected]
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
[email protected]
auth_password=my-gmail-password
[email protected]

但是我仍然无法收到电子邮件,有人知道我的错误吗?

蔡卓en

如果可以使用第三方库,则以下示例如何实现:

(两者均适用于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 = "[email protected]";
$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 = '[email protected]';
    $mail->Password = 'your_password';

    $mail->From = '[email protected]';
    $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 = "[email protected]";
$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' => '[email protected]',
        'password' => 'your_password'
    );

$mailServerCfg = array(
        'ssl' => 'tls',
        'port' => 587,
        'auth' => 'login',
        'username' => '[email protected]',
        '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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章