1

私は自分のウェブサイトで使用するために、jquery contactable plugin (Google で見つけた!) の mail.php ファイルを利用しようとしています。提供されたスクリプトは非常に単純ですが、ホストの SMTP 要件と統合する際に問題が発生しています。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('mymail@mymail.com', $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-認証/ )

<?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 = "mywebsitemail@mywebsiteaddress.com";
 $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('mymail@mymail.com', $subject, $contactMessage, $headers))  ? "success": "failure";
$output = json_encode(array("response" => $response));  
    header('content-type: application/json; charset=utf-8');
    echo($output);

 ?>

私は実際にちょっとした問題に遭遇しました。私のホストは PHPMailer をサポートしていません :-(。SMTP を使用した PearMail のみです。彼らは、上記のコードを微調整し、既存のコードを組み込むことを提案しました。まさに、これをオンラインに投稿する前に私がやろうとしていたことです。広場に戻る1、アイデアは?

コメント、提案、何でも大歓迎です! :-)

4

2 に答える 2

9

メールを送信するには、PHPMailerを試してみてください。テスト済みで、誰もが使用しており、問題なく動作します。また、多くの機能と構成オプションがあります。

PHPMailerで SMTP を使用してメールを送信する場合、必要なコードはこれだけです

// 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 = "yourusername@gmail.com";
$mail->Password = "yourpassword";            
//$mail->Port = 465; // optional if you don't want to use the default 

$mail->From = "my@email.com";
$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);
于 2012-07-13T20:49:40.687 に答える
0

これは、無効になっている PHP の mail() 関数を回避するためのテスト スクリプトです。ピアメールを使用しています。print ステートメントはブラウザでテストするためのものです。削除することをお勧めします。

<?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 support@myserver.com 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 [xyz@myserver.com]
 * 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: welcome@theappflap.com 
//[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"] = "support@myserver.com";  #- 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; //'support@myserver.com';

$headers['From']    = 'welcome@myserver.com'; 
$headers['To']      = $email; //'support@myserver.com';   // 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";
 }
?>
于 2015-10-11T16:17:49.583 に答える