Solved

PHPMailer with GMail and Windows Scheduler

Posted on 2011-02-22
5
1,144 Views
Last Modified: 2012-05-11
Hi,

I have an internal Windows 2003 server which runs Apache and IIS side-by-side. I'm logged in as the administrator.
Up to last week, I had a Windows Scheduled Task which invoked a PHP file.
This PHP file used PHP's inbuilt "mail" command to send an email, using an internal mailserver.

I then changed the mailserver to Gmail and because it needed an SMTP login, I changed the "mail" command to PHPMailer which I'm very familiar with.

If I now open Internet Explorer and put in the URL of my PHP file, PHPMailer sends the email as expected. With SMTPDebug set to 1, no warnings are displayed.

If I now run the unchanged Windows Scheduler job (either manually or schedule it), the email is NOT sent and instead, a  command prompt window flashes up saying "unable to connect to server: Unable to find the socket transport "ssl"  - did you forget to enable it when you configured PHP? Could not connect to SMTP host."

If I run phpinfo() it shows the PHP.ini file that I modified last week. If I open this PHP.ini file, "openssl" is uncommented. Apache was restarted since that change was made last week.

Can anyone help me? Does PHPMailer not work with Windows Scheduler? Is the PHPMailer include not being recognised, even if I put the full path in the include?

Thanks in advance
0
Comment
Question by:kbit
5 Comments
 
LVL 17

Accepted Solution

by:
Dushan De Silva earned 400 total points
ID: 34957440
First create a php file based on the attached code snippet. save it as www.mywebsite.com/mail.php

1. save code snippet as "mail.php"
2. edit variables in << >>
      If using a free email host and info is unknown: http://www.emailaddressmanager.com/tips/mail-settings.html
3. upload to www.mywebsite.com
4. Download http://sourceforge.net/projects/phpmailer and place class.phpmailer.php in your php includes directory
5. navigate with a web browser to www.mywebsite.com/mail.php to make sure it sends

Next in Windows go to "Start | Control Panel | Scheduled Tasks".
1. Click the Add Scheduled Task button
2. Click Next
3. Select Internet explorer from the list
4. Select Perform this task Daily (well change it later)
5. Click Next
6. Enter your username and password of the computer
7. Select open advanced properties and click Finish

In advanced properties
1. Add "www.mywebsite.com/mail.php" after "C:\PROGRA~1\INTERN~1\iexplore.exe" in the "Run" Section
2. Click on the schedule tab and click advanced
3. Check "Repeat Task" and say every 30 minutes for 24 hours.
4. Click OK
5. Click OK
6. Enter Windows user name and password again

The website should pop up and send an email every 30 minutes now.

<?php
 
require("class.phpmailer.php");
 
$mail = new PHPMailer();
 
$mail->IsSMTP(); // send via SMTP
$mail->Host     = "<<smtpserver>>:<<stmpport>>"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "<<uname>>"; // SMTP username
$mail->Password = "<<pword>>"; // SMTP password
$mail->From = "<<email address from>>";
$mail->FromName = "<<email name from>>";
$mail->AddAddress("<<email address to>>"); 
$mail->AddReplyTo("<<email address from>>","<<email name from>>");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject  =  "Subject";
$mail->Body     =  "Body text";
$mail->AltBody  =  $mail->Body;
        
if ( $mail->Send() )
{
echo "Email sent successfully";
}
else
{
echo "Email failed";
}
 
?>

Open in new window

0
 
LVL 20

Expert Comment

by:virmaior
ID: 34957452
this sort of issue usually arises from a question of what user the scheduler is set to run as.
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 100 total points
ID: 34958079
PHP thru Apache uses the 'php.ini' in the Apache folder.  PHP on the command line uses the 'php.ini' in the PHP folder or maybe in C:\Windows.  Usually.  Different files.
0
 

Author Comment

by:kbit
ID: 34958505
Thanks for the suggestions all. Dushan911, your suggestion looks very promising, just getting someone to test it in the next few hours but the job I quickly set up earlier looks great!
0
 

Author Comment

by:kbit
ID: 34959984
This worked nicely, just added a batch file to taskkill the explorer sessions. Thanks again for all your help
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question