dongyi1982 2015-10-08 13:29 采纳率: 0%
浏览 27

Google Mail的SMTP脚本

I have got the below script from this link. My Website is running with Digital Ocean hosting and I am unable to send a mail. Can you please help me out to work. Also, How do I get to know the actual mail failure ?

#!/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>'."
";
  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}
";
  $body = file_get_contents($argv[3]); 
} else {
  $length = strlen($argv[3]);
  echo "[+] Delivering text with length of {$length} to {$to}
";
  $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 :)
";

?>
  • 写回答

1条回答 默认 最新

  • douyi9447 2015-10-08 13:38
    关注

    change your host smtp.gmail.com to ssl://smtp.gmail.com

    try this

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

    it might help you

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探