使用GMail SMTP服務器從PHP頁面發送電子郵件




smtp class php (10)

SwiftMailer可以使用外部服務器發送電子郵件。

這裡是一個顯示如何使用Gmail服務器的例子:

require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";

//Connect to localhost on port 25
$swift =& new Swift(new Swift_Connection_SMTP("localhost"));


//Connect to an IP address on a non-standard port
$swift =& new Swift(new Swift_Connection_SMTP("217.147.94.117", 419));


//Connect to Gmail (PHP5)
$swift = new Swift(new Swift_Connection_SMTP(
    "smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS));

我試圖通過一個PHP頁面通過GMail的SMTP服務器發送一封電子郵件,但是我得到這個錯誤:

身份驗證失敗[SMTP:SMTP服務器不支持身份驗證(代碼:250,響應:mx.google.com為您服務,[98.117.99.235] SIZE 35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES流水線]]

誰能幫忙? 這是我的代碼:

<?php
require_once "Mail.php";

$from = "Sandra Sender <[email protected]>";
$to = "Ramona Recipient <[email protected]>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "smtp.gmail.com";
$port = "587";
$username = "[email protected]";
$password = "testtest";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
?>

<?php
date_default_timezone_set('America/Toronto');

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             = "gdssdh";
//$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host       = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug  = 1;                     // 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   = "[email protected]";  // GMAIL username
$mail->Password   = "password";            // GMAIL password

$mail->SetFrom('[email protected]', 'PRSPS');

//$mail->AddReplyTo("[email protected]', 'First Last");

$mail->Subject    = "PRSPS password";

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

$mail->MsgHTML($body);

$address = "[email protected]";
$mail->AddAddress($address, "user2");

//$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!";
}

?>

'auth' => false,

另外,看看端口25是否工作。


問題中列出的代碼需要兩個更改

$host = "ssl://smtp.gmail.com";
$port = "465";

SSL連接需要使用端口465。


我不推薦Pear Mail。 它自2010年以來沒有更新過。還請閱讀源文件; 源代碼幾乎過時,用PHP 4風格編寫,並且發布了很多錯誤/錯誤(Google it)。 我正在使用Swift Mailer。

Swift Mailer集成到任何使用PHP 5編寫的Web應用程序中,提供靈活和優雅的面向對象的方法來發送具有多種功能的電子郵件。

使用SMTP,sendmail,postfix或您自己的自定義傳輸實施發送電子郵件。

支持需要用戶名和密碼和/或加密的服務器。

在不剝奪請求數據內容的情況下防止標頭注入攻擊。

發送符合MIME的HTML /多部分電子郵件。

使用事件驅動的插件來定制庫。

處理較大的附件和內存/嵌入式圖像,使用較少的內存。

它是一個免費且開放的源代碼,您可以下載Swift Mailer並上傳到您的服務器。 (功能列表從所有者網站複製)。

Gmail SSL / SMTP和Swift Mailer的工作示例在這裡...

// Swift Mailer Library
require_once '../path/to/lib/swift_required.php';

// Mail Transport
$transport = Swift_SmtpTransport::newInstance('ssl://smtp.gmail.com', 465)
    ->setUsername('[email protected]') // Your Gmail Username
    ->setPassword('my_secure_gmail_password'); // Your Gmail Password

// Mailer
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject Here')
    ->setFrom(array('[email protected]' => 'Sender Name')) // can be $_POST['email'] etc...
    ->setTo(array('[email protected]' => 'Receiver Name')) // your email / multiple supported.
    ->setBody('Here is the <strong>message</strong> itself. It can be text or <h1>HTML</h1>.', 'text/html');

// Send the message
if ($mailer->send($message)) {
    echo 'Mail sent successfully.';
} else {
    echo 'I am sure, your configuration are not correct. :(';
}

我希望這有幫助。 快樂編碼... :)


// Pear Mail Library
require_once "Mail.php";

$from = '<[email protected]>';
$to = '<[email protected]>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => '[email protected]',
        'password' => 'passwordxxx'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}


要在Ubuntu中安裝PEAR的Mail.php,請運行以下命令集:

    sudo apt-get install php-pear
    sudo pear install mail
    sudo pear install Net_SMTP
    sudo pear install Auth_SASL
    sudo pear install mail_mime

使用Swift郵件程序 ,通過Gmail憑據發送郵件非常簡單:

<?php
require_once 'swift/lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
  ->setUsername('GMAIL_USERNAME')
  ->setPassword('GMAIL_PASSWORD');

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance('Test Subject')
  ->setFrom(array('[email protected]' => 'ABC'))
  ->setTo(array('[email protected]'))
  ->setBody('This is a test mail.');

$result = $mailer->send($message);
?>

Gmail需要使用端口465,也是來自phpmailer的代碼:)





gmail