我想用php从我的gmail帐户发送电子邮件,我从4小时开始在网上搜索答案,但出现了错误


I want to send email from my gmail account using php i am searching for answer on net since 4 hours but error appears

我使用的是wamp服务器,我的final.php中有以下代码。我也是添加错误,如下所示出现在代码后作为注释,请检查为.

我是php的新手。我正在寻找简单的代码,这样我就可以通过我的帐户发送消息,但我不明白为什么这个代码没有密码选项。

是否需要密码?请详细帮助我,我还需要同时向多个用户发送电子邮件。

<?php
    require("class.phpmailer.php");
    $mail = new PHPMailer();
    $mail->IsSMTP();  // telling the class to use SMTP
    $mail->Host     = "smtp.gmail.com"; // SMTP server

    $mail->From     = "63yogesh@gmail.com";
    $mail->AddAddress("11ce028@charusat.edu.in");

    $mail->Subject  = "First PHPMailer Message";
    $mail->Body     = "Hi! 'n'n This is my first e-mail sent through PHPMailer.";
    $mail->WordWrap = 50;

    if(!$mail->Send()) {
    echo 'Message was not sent.';
    echo 'Mailer error: ' . $mail->ErrorInfo;
    } else {
    echo 'Message has been sent.';
    }
    ?>

    //The following From address failed: 63yogesh@gmail.com Message was not sent.Mailer error: The following From address failed: 63yogesh@gmail.com

SMTP服务器错误:5.7.0必须首先发出STARTTLS命令。jw8sm18271654pbc.73-gsmtp

这里的示例说您必须设置几个数据,例如身份验证:

//Username to use for SMTP authentication
$mail->Username = "yourname@example.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";

和SMTP端口号:

//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;

编辑:这是从他们的官方网站使用SMTP的基本示例,试着用你的信息编辑它

require_once('../class.phpmailer.php'); //include("class.smtp.php"); //optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[']",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                            // 1 = errors and messages
                                            // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password
$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}