2012-07-13 97 views
1

我一直在尝试利用jQuery联系插件(google上的!)在我的网站上使用mail.php文件。虽然提供的脚本非常简单,但我遇到了将其与Host的SMTP要求集成的问题。这里是没有SMTP认证的原始脚本:Mail.php&Smtp身份验证问题

<?php 
    // Assign contact info 
    $name = stripcslashes($_POST['name']); 
    $emailAddr = stripcslashes($_POST['email']); 
    $issue = stripcslashes($_POST['issue']); 
    $comment = stripcslashes($_POST['message']); 
    $subject = stripcslashes($_POST['subject']);  

    // Set headers 
    $headers = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

    // Format message 
    $contactMessage = 
    "<div> 
    <p><strong>Name:</strong> $name <br /> 
    <strong>E-mail:</strong> $emailAddr <br /> 
    <strong>Issue:</strong> $issue </p> 

    <p><strong>Message:</strong> $comment </p> 

    <p><strong>Sending IP:</strong> $_SERVER[REMOTE_ADDR]<br /> 
    <strong>Sent via:</strong> $_SERVER[HTTP_HOST]</p> 
    </div>"; 

    // Send and check the message status 
    $response = (mail('[email protected]', $subject, $contactMessage, $headers)) ? "success" : "failure" ; 
    $output = json_encode(array("response" => $response)); 

    header('content-type: application/json; charset=utf-8'); 
    echo($output); 

?> 

我试过使用来自Google的建议,并使用它几个小时来玩。这是基于我迄今为止对PHP的无知的最新版本。 -__-(在此基础上:http://blog.geek4support.com/php-mail-script-with-smtp-authentication-how-to-send-mails-by-php-mail-script-using-smtp-authetication/

<?php 
require_once "Mail.php"; 

    // Assign contact info 
    $name = stripcslashes($_POST['name']); 
    $emailAddr = stripcslashes($_POST['email']); 
    $issue = stripcslashes($_POST['issue']); 
    $comment = stripcslashes($_POST['message']); 
    $subject = stripcslashes($_POST['subject']);  


$host = "mail.mywebsite.com"; 
$username = "[email protected]"; 
$password = "mymailpassword"; 

    // Set headers 
    $headers = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

    // Format message 
    $contactMessage = 
    "<div> 
    <p><strong>Name:</strong> $name <br /> 
    <strong>E-mail:</strong> $emailAddr <br /> 
    <strong>Issue:</strong> $issue </p> 

    <p><strong>Message:</strong> $comment </p> 

    <p><strong>Sending IP:</strong> $_SERVER[REMOTE_ADDR]<br /> 
    <strong>Sent via:</strong> $_SERVER[HTTP_HOST]</p> 
    </div>"; 

$smtp = Mail::factory('smtp', 
    array ('host' => $host, 
    'auth' => true, 
    'username' => $username, 
    'password' => $password)); 


$response = ($smtp->send('[email protected]', $subject, $contactMessage, $headers)) ? "success": "failure"; 
$output = json_encode(array("response" => $response)); 
    header('content-type: application/json; charset=utf-8'); 
    echo($output); 

?> 

其实我已经遇到了一点问题。我的主机不支持PHPMailer :-(。只有PearMail和SMTP,他们建议调整上面列出的代码,并将其与现有的代码合并在一起。 1,任何想法?

意见,建议,什么都将是非常感谢!:-)

+1

mail.php是什么,我们包括在行首。 – TechCare99 2013-07-24 18:22:41

回答

9

对于发送邮件,尝试PHPMailer,它的测试,每个人都使用它,它只是工作。它也有很多功能和配置选项。

最新的版本是this one,作为使用SMTP与PHPMailer的发送邮件,这是所有的代码,你需要

// Data received from POST request 
$name = stripcslashes($_POST['name']); 
$emailAddr = stripcslashes($_POST['email']); 
$issue = stripcslashes($_POST['issue']); 
$comment = stripcslashes($_POST['message']); 
$subject = stripcslashes($_POST['subject']); 

// Send mail 
$mail = new PHPMailer(); 
$mail->IsSMTP(); // telling the class to use SMTP 

// SMTP Configuration 
$mail->SMTPAuth = true;     // enable SMTP authentication 
$mail->Host = "myhost"; // SMTP server 
$mail->Username = "[email protected]"; 
$mail->Password = "yourpassword";    
//$mail->Port = 465; // optional if you don't want to use the default 

$mail->From = "[email protected]"; 
$mail->FromName = "My Name"; 
$mail->Subject = $subject; 
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 
$mail->MsgHTML($issue . "<br /><br />" . $comment); 

// Add as many as you want 
$mail->AddAddress($emailAddr, $name); 

// If you want to attach a file, relative path to it 
//$mail->AddAttachment("images/phpmailer.gif");    // attachment 

$response= NULL; 
if(!$mail->Send()) { 
    $response = "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    $response = "Message sent!"; 
} 

$output = json_encode(array("response" => $response)); 
header('content-type: application/json; charset=utf-8'); 
echo($output); 
+0

绝对的,辉煌的东西Gosu Kiwi!非常感谢!让我试着把它放在一起。 – junaidkaps 2012-07-14 17:33:12

+0

我已经遇到了一些问题。我的主机不支持PHPMailer :-(。只有PearMail和SMTP,他们建议调整上面列出的代码,并将其与现有的代码合并在一起。 1,任何想法? – junaidkaps 2012-07-15 01:31:07

+0

你应该检查PearMail的文档,它应该在他们的网站上。尝试找到一个SMTP示例,它不应该太难,你只需要替换登录信息,信息 – gosukiwi 2012-07-17 17:17:54

0

这是我的测试脚本为各地残疾人PHP mail()函数得到。它使用PearMail。打印语句用于在浏览器中进行测试,您可能需要删除它们。

<?php 
/* 
* Script to send an email as the PHP mail() function is disabled. 
* The hosting company requires SMTP authentication etc. 
* need to install the pear mail package in the cPanel 
* 
* Use the [email protected] as the sending/from email 
* (or maybe the welcome one?) 
* 
* Sunday, 11 october 2015 
* G O’Rilla 
* 
* 
*/ 
function pearMail($e_mail, $subject, $content) { 
/* 
// To ignore the Strict Standards (which are non fatal) change your error 
// reporting level in the php.ini file or better, inline with error_reporting() 
*/ 
error_reporting(E_ERROR | E_PARSE); 
# To use installed modules (cPanel - PHP Extensions and Applications Package Installer) 
# Add “/home/myaccount/php” to the include path. To do this, add the following code to your script: 
ini_set("include_path", '/home/myaccount/php:' . ini_get("include_path") ); 

require_once 'Mail.php'; 

#Server host only allows SMTP authentication 
/* 
* Use below setting for SMTP Authentication. 
* -- 
* SMTP Host : myserver.com 
* SMTP User : Use domain email Address [[email protected]] 
* SMTP Password : Use domain email password. 
* SMTP Port : 25 
*/ 
//print "Start Script <br>"; 
$params = array(); 
$params["host"] = “myserver.com";    # - The server to connect. Default is localhost - use your domain name. 
$params["port"] = 25;       # - The port to connect. Default is 25. 
// Error: return fron mailer: Failed to set sender: [email protected] 
//[SMTP: Invalid response code received from server (code: 550, response: 
// Access denied - Invalid HELO name (See RFC2821 4.1.1.1))] 
// sever requires authentication so TRUE 
$params["auth"] = TRUE;       # - Whether or not to use SMTP authentication. Default is FALSE. 
$params["username"] = "[email protected]"; #- The username to use for SMTP authentication. 
$params["password"] = “********”;    #- The password to use for SMTP authentication. 

print_r ($params); 
//Other parameters assuming default values will do 
#$params["localhost"] - The value to give when sending EHLO or HELO. Default is localhost 
#$params["timeout"] - The SMTP connection timeout. Default is NULL (no timeout). 
#$params["verp"] - Whether to use VERP or not. Default is FALSE. 
#$params["debug"] - Whether to enable SMTP debug mode or not. Default is FALSE. 
# Mail internally uses Net_SMTP::setDebug . 
#$params["persist"] - Indicates whether or not the SMTP connection should persist over multiple calls to the send() method. 
#$params["pipelining"] - Indicates whether or not the SMTP commands pipelining should be used. 

$rc = $mailer = & Mail::factory("smtp", $params); # creates a mailer instance 
if ($rc == NULL) { 
    print "<br>Failed to create mail instance <br>"; 
} 
else { 
    print "<br>mail instance created <br>"; 
} 

$recipients = $email; //'[email protected]'; 

$headers['From'] = '[email protected]'; 
$headers['To']  = $email; //'[email protected]'; // Input param 
$headers['Subject'] = $subject; //'TAF: Test message';  // Input param 

$body = $content; //'This is a test using PEAR mailer';  // Input param 

print ("recipients: " . $e_mail . " subject: " . $subject . " content: " . $content . "<br>"); 

$rc = $mailer->send($recipients, $headers, $body); 
print ("return from mailer; " . $rc); 

//print "<br>End Script"; 
} 
?>