Google Mail的SMTP脚本

时间:2015-10-08 13:29:26

标签: php wordpress email smtp gmail

我从这个link获得了以下脚本。我的网站正在运行Digital Ocean托管,我无法发送邮件。 你可以帮我出去工作吗?另外,我如何了解 实际的邮件失败?

#!/usr/bin/php
<?php

if (PHP_SAPI !== 'cli') exit;
if (count($argv) < 4) {
  echo 'Usage: ' . basename($_SERVER['PHP_SELF']) . '  <recipient1#recipient2#...> "<subject>" <"msg" or file>'."\n";
  exit;
}

require_once "Mail.php";

$from         = "xxx@gmail.com";
$aRecipients  = (strpos($argv[1], '#')) ? explode('#', $argv[1]) : array($argv[1]);
$to           = '';
foreach ($aRecipients as $recipient) $to .= "{$recipient},";
$to           = rtrim($to, ',');
$subject      = $argv[2];
$body         = '';
if (file_exists($argv[3])) {
  echo "[+] Delivering file {$argv[3]} to {$to}\n";
  $body = file_get_contents($argv[3]); 
} else {
  $length = strlen($argv[3]);
  echo "[+] Delivering text with length of {$length} to {$to}\n";
  $body = "{$argv[3]}";
}

$host     = gethostbyname('smtp.gmail.com'); 
$port     = 465; 
$username = "xxxx@gmail.com";
$password = "xxx";

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

$smtp =& Mail::factory('smtp',
    array(
    'host'      => $host,
    'port'      => $port,
    'debug'     => true, // set to true if u want to see what happens in the background
    'auth'      => true,
    'username'  => $username,
    'password'  => $password
    )
);

$smtp->send($to, $headers, $body);
echo "[+] Mail sent :)\n";

?>

1 个答案:

答案 0 :(得分:0)

将您的主人smtp.gmail.com更改为ssl://smtp.gmail.com

试试这个

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'XXX@gmail.com',
        'password' => 'xxx'
    ));

它可能对你有帮助