How to mail a log file to yourself using PHP from a Windows machine ?

不羁岁月 提交于 2019-12-24 04:08:13

问题


Situation:

  • I run a PHP script from a windows 7 machine using CLI.
  • My script will do its thing and then generate 1 log file at the end.
  • I already got all that part working.

In addition to that, I want to email myself that log file every time the script runs.

Somewhere in the bottom of my script I tried this :

mail('email@gmail.com', 'Report', strip_tags($response). PHP_EOL );

The script run all the way to the bottom, I got my log file to generate, I also got a report on my CLI as well, but I never receive any email.

Result :

I am not sure is because I :

  • am on a Windows.
  • need to allow specific php extension
  • Need to configure more setting in my php.ini.

Can someone help clarify this issue ?


回答1:


You need a Mail Server which is configured in the PHP.ini to send your mails.

Here is a short Tutorial:

http://geekswithblogs.net/tkokke/archive/2009/05/31/sending-email-from-php-on-windows-using-iis.aspx

Note that the IIS6 console still needed for the Mail Server, also if you're hosting on >=IIS7.

Also you need to make sure, your Mail Server is acceptet by the Mail Server you want to send this mail to. This is definitve not a trival task. Gmail and GMX for example never accept it, if you didn't have Reverse DNS correctly configured.

If you don't know how, I highly recommend to have a talk to your System Administrator. Mail Server are very hard to setup correctly. It's a task I work around.

But here are the good news, if you do not want to configure your own mail server. It is very simple with an Hosted Email Adress and SMTP, if your using the Open Source Project PHPMailer:

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

//More in the Documentation    

It has a powerfull SMTP Class, which helps extreme for login into any SMTP Account (Gmail for example).



来源:https://stackoverflow.com/questions/28903207/how-to-mail-a-log-file-to-yourself-using-php-from-a-windows-machine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!