17

PHPMailer を使用して gmail メールを送信しようとしています。この投稿をフォローしました

これを行うために、以下に示す関数を設定しました。

function sendEmail($email, $name) {
    $mail = new PHPMailer();
    $mail->IsSMTP(); // send via SMTP
    //IsSMTP(); // send via SMTP I commented it cos it gives an error
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->Username = 'email@gmail.com'; // Changed my email
    $mail->Password = "password";// Changed my password
    $mail->From = 'email@gmail.com';
    $mail->FromName = 'FROM NAME';


    $mail->AddAddress($email);

    $mail->IsHTML(true); // send as HTML
    $mail->Subject = "Subject";
    $mail->Body = "Body";

    if (!$mail->Send()) {
        return false;
    } else {
        return true;
    }
}

残念ながら、false を返し続けます。コードの何が問題なのか教えてください。

編集:私が得ているエラーを以下に示します:

SMTP -> ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

SMTP Error: Could not connect to SMTP host.

更新されたコード:

 $Mail = new PHPMailer();
    $Mail->IsSMTP(); // Use SMTP
    $Mail->Host = "smtp.gmail.com"; // Sets SMTP server
    $Mail->SMTPDebug = 2; // 2 to enable SMTP debug information 
    $Mail->SMTPAuth = TRUE; // enable SMTP authentication
    $Mail->SMTPSecure = "tls"; //Secure conection
    $Mail->Port = 587; // set the SMTP port
    $Mail->Username = EMAIL; // SMTP account username
    $Mail->Password = PASS; // SMTP account password
    $Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
    $Mail->CharSet = 'UTF-8';
    $Mail->Encoding = '8bit';
    $Mail->Subject = 'SUB';
    $Mail->ContentType = 'text/html; charset=utf-8\r\n';
    $Mail->From = EMAIL;
    $Mail->FromName = 'FROM NAME';
    $Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line

    $Mail->AddAddress($email); // To:
    $Mail->isHTML(TRUE);
    $Mail->Body = "Hi";
    $Mail->AltBody = "Hi";
    $Mail->Send();
    $Mail->SmtpClose();
4

7 に答える 7

21

これが実際の例です:

<?php
function SendMail( $ToEmail, $MessageHTML, $MessageTEXT ) {
  require_once ( 'class.phpmailer.php' ); // Add the path as appropriate
  $Mail = new PHPMailer();
  $Mail->IsSMTP(); // Use SMTP
  $Mail->Host        = "smtp.gmail.com"; // Sets SMTP server
  $Mail->SMTPDebug   = 2; // 2 to enable SMTP debug information
  $Mail->SMTPAuth    = TRUE; // enable SMTP authentication
  $Mail->SMTPSecure  = "tls"; //Secure conection
  $Mail->Port        = 587; // set the SMTP port
  $Mail->Username    = 'MyGmail@gmail.com'; // SMTP account username
  $Mail->Password    = 'MyGmailPassword'; // SMTP account password
  $Mail->Priority    = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
  $Mail->CharSet     = 'UTF-8';
  $Mail->Encoding    = '8bit';
  $Mail->Subject     = 'Test Email Using Gmail';
  $Mail->ContentType = 'text/html; charset=utf-8\r\n';
  $Mail->From        = 'MyGmail@gmail.com';
  $Mail->FromName    = 'GMail Test';
  $Mail->WordWrap    = 900; // RFC 2822 Compliant for Max 998 characters per line

  $Mail->AddAddress( $ToEmail ); // To:
  $Mail->isHTML( TRUE );
  $Mail->Body    = $MessageHTML;
  $Mail->AltBody = $MessageTEXT;
  $Mail->Send();
  $Mail->SmtpClose();

  if ( $Mail->IsError() ) { // ADDED - This error checking was missing
    return FALSE;
  }
  else {
    return TRUE;
  }
}

$ToEmail = 'Name@example.com';
$ToName  = 'Name';

$Send = SendMail( $ToEmail, $MessageHTML, $MessageTEXT );
if ( $Send ) {
  echo "<h2> Sent OK</h2>";
}
else {
  echo "<h2> ERROR</h2>";
}
die;
?>

このスクリプトを試してみたところ、いくつかのメッセージを問題なく送信できました。

更新しました:

これは、成功時の Gmail からの典型的な応答です。

SMTP -> FROM SERVER:220 mx.google.com ESMTP 20sm6345523qek.6
SMTP -> FROM SERVER: 250-mx.google.com at your service, [181.155.13.39]
                     250-SIZE 35882577
                     250-8BITMIME
                     250-STARTTLS
                     250 ENHANCEDSTATUSCODES
SMTP -> FROM SERVER:220 2.0.0 Ready to start TLS
SMTP -> FROM SERVER: 250-mx.google.com at your service, [181.155.13.39]
                     250-SIZE 35882577
                     250-8BITMIME
                     250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
                     250 ENHANCEDSTATUSCODES
SMTP -> FROM SERVER:250 2.1.0 OK 20sm6345523qek.6
SMTP -> FROM SERVER:250 2.1.5 OK 20sm6345523qek.6
SMTP -> FROM SERVER:354 Go ahead 20sm6345523qek.6
SMTP -> FROM SERVER:250 2.0.0 OK 1353474062 20sm6345523qek.6
SMTP -> FROM SERVER:221 2.0.0 closing connection 20sm6345523qek.6
于 2012-11-20T08:40:57.527 に答える
2

アカウントに必要なSMTPSecureオプションを設定していないため、コードが機能していませんsslgmail

include_once "/lib/phpmailer/PHPMailer.class.php";
include_once "/lib/phpmailer/SMTP.class.php";
include_once "/lib/phpmailer/POP3.class.php";

$mail = new PHPMailer(true);
$mail->IsSMTP();

try {
    $mail->Host = "smtp.gmail.com"; 
    $mail->SMTPDebug = 2; 
    $mail->SMTPSecure = 'ssl'; //<----------------- You missed this 
    $mail->SMTPAuth = true; 
    $mail->Host = "smtp.gmail.com"; 
    $mail->Port = 465; // 
    $mail->Username = "xxxxxx@gmail.com";
    $mail->Password = "xxxxxx";
    $mail->AddAddress('to@example.com', 'John Doe');
    $mail->SetFrom('xxxxxx@gmail.com', 'First Last');
    $mail->Subject = 'This is a TEST message';
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
    $body = "To view the message, please use an HTML compatible email viewer!"; // automatically
    $mail->MsgHTML($body);
    $mail->Send();
    echo "Message Sent OK</p>\n";
} catch ( phpmailerException $e ) {
    echo $e->errorMessage(); 
} catch ( Exception $e ) {
    echo $e->getMessage(); 
}

出力

SMTP -> FROM SERVER:220 mx.google.com ESMTP q22sm2927759bkv.16 
SMTP -> FROM SERVER: 250-mx.google.com at your service, [62.173.54.190] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 250 ENHANCEDSTATUSCODES 
SMTP -> FROM SERVER:250 2.1.0 OK q22sm2927759bkv.16 
SMTP -> FROM SERVER:250 2.1.5 OK q22sm2927759bkv.16 
SMTP -> FROM SERVER:354 Go ahead q22sm2927759bkv.16 
SMTP -> FROM SERVER:250 2.0.0 OK 1353341553 q22sm2927759bkv.16 
Message Sent OK
于 2012-11-19T16:16:25.977 に答える
1

このような種類の問題では、問題に関連する可能性のあるサーバーの問題が多数あるため、本番環境にデプロイする前に開発環境でどのように動作するかを確認することが重要です。

そのため、デバッグを true に設定する前に、表示されるメッセージを確認してください。

$mail->SMTPDebug = 1;

とはいえ、そのような状況での一般的なサーバーの問題は次のとおりです。

  1. PHP での SSL サポートの欠如。有効にする必要があります。

  2. ある種のファイアウォールが、アウトバウンド ソケットへの接続をブロックしている可能性があります。PHPを使用して確認できます。

-

$p = fsockopen( '127.0.0.1', <port number>, $errno, $errstr, 5 );
if ( !$p )
    // port is closed or blocked
else
    // port is open and available
    fclose( $p );`
于 2012-11-14T21:06:36.363 に答える
0

localhost から xampp サーバーからメールを送信しました

このコードは私にとって完全に機能します

1: https://github.com/PHPMailer/PHPMailerから phpmailer をダウンロードし ます。

2: xampp に移動し、php.ini を検索します。

3 php.ini検索で

;extension=php_openssl.dll    
 remove(;)       
 extension=php_openssl.dll 

次に、PCの作業を保存して再起動します

<%php <br/>
require_once("C:\\xampp\\phpMailer\\PHPMailer-master\\class.phpmailer.php"); <br/>

$mail = new PHPMailer(); <br/>
$mail->IsSMTP();  // telling the class to use SMTP    <br/>
$mail->SMTPAuth = true;         // Enable SMTP authentication  <br/>
$mail->SMTPSecure = 'ssl' ;   <br/>
$mail->Host    = "smtp.gmail.com" ;// SMTP server <br/>
$mail->Port = 465; // or 587   <br/>

$mail->Username = 'senderemailid@gmail.com';    // SMTP username <br/>
$mail->Password = 'senderpassword';     // SMTP password   <br/>

$mail -> IsHTML(true);  <br/>
$mail->From    = 'senderemailid@gmail.com'; <br/>
$mail->FromName = 'sendername';   <br/>
$mail->addAddress('receiveremailid@domain.com','receivername');  <br/>

$mail->WordWrap = 50;  <br/>

$mail->Subject  = "This mail send from  PhP code xampp"; <br/>
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer."; <br/>

if(!$mail->Send()) {   <br/>
echo 'Message was not sent.';   <br/>
echo 'Mailer error: ' . $mail->ErrorInfo;  <br/>

} else    <br/>
 {   <br/>
echo 'Message has been sent.';  <br/>
}  <br/>
?>  <br/>
于 2014-03-30T10:34:03.247 に答える
0

試す...

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

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP

try {
  $mail->Host       = "smtp.gmail.com"; // SMTP server
  $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->Host       = "smtp.gmail.com"; // sets the SMTP server
  $mail->Port       = 465;                    // set the SMTP port for the GMAIL server
  $mail->Username   = "yourname@yourdomain"; // SMTP account username
  $mail->Password   = "yourpassword";        // SMTP account password
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->Subject = 'This is a TEST Message';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML($obdy);
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}
?>

構成に置き換えるだけで、require_once('class.phpmailer.php'); を設定します。正しい場所を指し、「contents.html」を HTML テンプレートに置き換えます。

また

HTML テンプレートを使用しない場合は、このコードを使用してください...

<?php

$body ='Your HTML message should go here';

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

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP

try {
  $mail->Host       = "smtp.gmail.com"; // SMTP server
  $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->Host       = "smtp.gmail.com"; // sets the SMTP server
  $mail->Port       = 465;                    // set the SMTP port for the GMAIL server
  $mail->Username   = "yourname@yourdomain"; // SMTP account username
  $mail->Password   = "yourpassword";        // SMTP account password
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->Subject = 'This is a TEST message';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML($body);
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}
?>
于 2012-11-12T13:33:25.520 に答える