使用Linux在PHP中发送邮件

时间:2016-06-22 13:48:37

标签: php smtp gmail

我有一个程序可以完美地使用Windows在PHP中发送邮件,但现在我想在Linux中使用cronjob发送它以便每天执行它。在Linux中工作需要做什么?我是否需要像在Windows中那样配置php.ini文件,sendmail等?

这是我用来发送电子邮件的程序。

<?php
ini_set('max_execution_time', 600); 
require 'class.phpmailer.php';

$host = ".....";
$user = "....";
$pass = ".....";
$db = "......";




$con1 = pg_connect("host=$host dbname=$db user=$user password=$pass")
    or die ("Could not connect to server\n");

$query1 = "select commands.author, users.badge_no
from commands
inner join users
on commands.author=users.username";

$rs1 = pg_query($con1, $query1) or die("Cannot execute query: $query\n");


$query = "SELECT distinct commands_details.delivery_date,commands_details.command_no,commands.author,commands_details.name
FROM commands_details
inner join commands
on commands_details.command_no=cast(commands.id as varchar)";

$rs = pg_query($con1, $query) or die("Cannot execute query: $query\n");

$nume=array();
$mail1=array();

while ($row = pg_fetch_row($rs1)){
    $nume[]=$row[0];  
    $mail1[]=$row[1];
}

$arrlength = count($nume);


$var_data='2016-06-19';
$mail_trimitere=array();
while ($row = pg_fetch_row($rs)) {
   if(strpos($row[0], $var_data) !== false){
        for($j=0;$j<$arrlength;$j++){
            if($row[2]===$nume[$j]){
                $mail_trimitere[0]=$mail1[$j];
                echo $mail_trimitere[0];
                break;
            }
        }

        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->Mailer = 'smtp';
        $mail->SMTPAuth = true;
        $mail->Host = 'smtp.gmail.com';
        $mail->Port = 587;
        $mail->SMTPSecure = 'tls';


        $mail->Username = "......";
        $mail->Password = "........";

        $mail->IsHTML(true); // if you are going to send HTML formatted emails
        $mail->SingleTo = true;

        $mail->From = "......";
        $mail->FromName = ".......";


        $mail->addAddress(".........","robert");

        $mail->Subject = "Testing PHPMailer with localhost";
        $mail->Body = 'urmeaza sa primiti maine comanda cu numarul '.' '.$row[1].' '.'produs'.' '. $row[3].' '. 'in data de '.' '.$row[0];

        if(!$mail->Send())
            echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
        else
            echo "Message has been sent";


  }
}



pg_close($con1);


?>

0 个答案:

没有答案