我如何使用PHP从服务器发送邮件?

时间:2014-01-03 11:44:51

标签: php email phpmailer

我正在尝试使用phpmailer发送邮件。现在它与我的localhost工作正常,但是一旦我将它上传到我的网站上,它从未奏效?任何人都可以告诉我如何使它工作,或者我可以找到解决方案?我试过谷歌搜索它,但它没有多大帮助。

使用localhost编码 -

<?php

//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');

require_once('PHPMailer_5.2.4/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body=$_POST["purpose"]." mailed to you by - ".$_POST["name"]." ( ".$_POST["number"].",".$_POST["email"].")";

$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->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "gmail_username";  // GMAIL username
$mail->Password   = "gmail_password";            // GMAIL password

$mail->SetFrom($_POST["email"], 'First Last');

$mail->AddReplyTo("harshitladdha93@gmail.com","First Last");

$mail->Subject    = $_POST["subject"];

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "hladdha.bhl2009@gmail.com";
$mail->AddAddress($address, "John Doe");


if(!$mail->Send()) {
  header( 'Location: http://www.facebook.html' ) ;
} else {
  header( 'Location:http://www.google.com' ) ;
}

?>

对于我的服务器,我做了以下更改 -

<?php
$mail->Host       = "mail.exoticalstudio.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "contact@exoticalstudio.com";  // GMAIL username
$mail->Password   = "password_for_webmail";            // GMAIL password

if(!$mail->Send()) {
  header( 'Location: http://www.facebook.html' ) ;
} else {
  header( 'Location:http://www.google.com' ) ;
}

?>

我尝试使用错误信息而不使用表单并直接运行脚本,我收到授权失败的错误。我该如何解决这个问题?

    SMTP -> FROM SERVER:220 mx.google.com ESMTP oo13sm84286802oeb.0 - gsmtp 
SMTP -> FROM SERVER: 250-mx.google.com at your service, [50.22.12.14] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN 250-ENHANCEDSTATUSCODES 250 CHUNKING 
SMTP -> ERROR: Password not accepted from server: 534-5.7.14 Please log in via your web browser and then try again. 534-5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787 534 5.7.14 54 oo13sm84286802oeb.0 - gsmtp 
SMTP -> FROM SERVER:250 2.1.5 Flushed oo13sm84286802oeb.0 - gsmtp 
SMTP -> FROM SERVER:530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 oo13sm84286802oeb.0 - gsmtp 
SMTP -> ERROR: MAIL not accepted from server: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 oo13sm84286802oeb.0 - gsmtp 
The following From address failed: harshitladdha93@gmail.com : MAIL not accepted from server,530,5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 oo13sm84286802oeb.0 - gsmtp
SMTP server error: 5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 oo13sm84286802oeb.0 - gsmtp

The following From address failed: harshitladdha93@gmail.com : MAIL not accepted from server,530,5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 oo13sm84286802oeb.0 - gsmtp
SMTP server error: 5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 oo13sm84286802oeb.0 - gsmtp

SMTP server error: 5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 oo13sm84286802oeb.0 - gsmtp

The following From address failed: harshitladdha93@gmail.com : MAIL not accepted from server,530,5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 oo13sm84286802oeb.0 - gsmtp
SMTP server error: 5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 oo13sm84286802oeb.0 - gsmtp

SMTP server error: 5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 oo13sm84286802oeb.0 - gsmtp

它说发现了可疑的登录活动,如何让它成为值得信赖的活动请帮助。

2 个答案:

答案 0 :(得分:0)

我们通过gmail发送电子邮件与phpmailer:

$mail->Host       = "ssl://smtp.gmail.com";

我认为你至少需要ssl://部分。但可能整个字符串应该是这个。 (或者如果只有评论在你的第二个片段中误导,那么邮件设置可能是错误的......)

如果$mail->ErrorInfo返回$mail->send(),则false包含错误。

通常,在exit()电话后添加header('Location:...');是一种很好的做法。 (你可以确保以这种方式终止游行。)

答案 1 :(得分:0)

您必须检查服务器支持哪个版本的php,并且服务器上是否启用了smtp和ssl端口。如果服务器支持像5.2这样的旧版本的东西,那么你必须使用旧版本的phpmailer。为此,您可以创建一个像phpinfo.php这样的php文件,并在其中输入以下代码 -

<?php phpinfo(); ?>