All I need from my server is to be able to send emails to my users when they forget their password. I try the code below after installing the Google App Engine SDK for PHP and it gives me the error
Fatal error: require_once(): Failed opening required
'google/appengine/api/mail/Message.php' (include_path='.:') in
/Library/WebServer/Documents/AppEngine/testMail.php on line 2
This is my code:
require_once 'google/appengine/api/mail/Message.php';
use google\appengine\api\mail\Message;
try {
$message = new Message();
$message->setSender('test#gmail.com');
$message->addTo('test#example.com');
$message->setSubject('Example email');
$message->setTextBody('Hello, world!');
$message->send();
echo 'Mail Sent';
} catch (InvalidArgumentException $e) {
echo 'There was an error';
}
I'm thinking that I did not install the engine properly but I'm lost at this point. Any ideas?
Well, two things. First of all, you don't need the require_once statement. At least I've never had to use that (maybe you did need it in older versions of GAE but as far as I know, it isn't needed. Your php script is currently trying to open a directory that doesn't exist in your project.) All you need is the use google\appengine\api\mail\Message; statement.
Secondly, when you setSender, you need to make sure that your email 'test#gmail.com' is a registered email in your google app engine app -> otherwise no actual email will get sent.
Hope that helps.
Related
Background
Hi,
I am new to cron jobs and I am trying to set up a test one just to see how they work. I created it in cPanel like this:
wget -O - -q https://website.com/staging/wp-content/themes/AT103/test-cron.php
My file is pretty simple so far:
<?php
$email_to = "info#domain.com";
$title = "Test title";
$body = "Test body";
mail($email_to, $title, $body);
?>
All works fine here, I recieve the email every interval that my cron job runs.
What I want
On my site people can put up ads/listings to sell their stuff. In the cron job I want to go through all listings and email the buyers and sellers for the ones that has expired. As a first step in this I need to access the variables on my website, e.g. a listing object which is put in a variable like $post. Or through a function which returns the stuff I want if easier.
What I tried
I tried to access a random file and its functions by using lots of different code examples found online. The file is in the same folder as my test-cron.php. Here are a few things I tried to put at the top of test-cron.php (one at a time, not all at once):
require '/functions.php';
require 'https://website.com/staging/wp-content/themes/AT103/functions.php';
require '/home3/username/public_html/staging/wp-content/themes/AT103/functions.php';
This all resulted in the same thing, I did not get an email anymore. So I assume there is some sort of error with the lines? I also tried with require_once() with the same result.
Questions
How do I access other scripts ("live" variables or functions) in my folder hierarchy through a cron job?
If it is not possible for some reason, can I instead access my database information somehow?
If the file to be required/included, is in the same folder as the running script you would use one of the following:
require 'functions.php'; no leading slash tells it to look in the include path, then in the current directory.
require './functions.php'; (better) the ./ explicitly says look in the current directory.
https://www.php.net/manual/en/ini.core.php#ini.include-path
EDIT:
I just realized I did not address the fact that you are using cron and that's because ...
You are sill running PHP, cronjob or not makes no difference it still works the same!
However, it can be more difficult to debug on a production server. If you want to see exactly what's happening when the script fails then you can wrap it in a try block, catch and send the error to your email, or output the error and view it in the browser.
I know on Bluehost shared-hosting, if any of my cronjobs produce any output it will be automatically sent to me via email. I use the format below, and always get an email telling me when & why it happened. While developing you can simply navigate to your test-cron.php in the browser.
<?php
try {
require './functions.php';
/*
all of your logic
*/
} catch (Error $e) {
echo "Caught Error: \n" . $e;
}
This a php application running on Google App Engine. I'm using getImageServingUrl to get a url and store it for later use. I've actually implemented this in another part of the application, but in this case, when run, the php code stops right at the point where I make the call to get the url. The are no errors in the page or in the App Engine log. Is there another place to get errors? I'm not having luck tracking down the issue. Here's what I have:
// At the top of the page I call this
use google\appengine\api\cloud_storage\CloudStorageTools;
// Some processing takes place to get the storage location of the image then
if (is_file($full_path_photo)) {
echo 'Getting URL...<br>';
$object_public_url = CloudStorageTools::getImageServingUrl($full_path_photo, ['secure_url' => true]);
echo 'Successful<br>';
};
// I've also tried it like this:
$object_public_url = CloudStorageTools::getImageServingUrl($full_path_photo);
// And adding this before the call:
CloudStorageTools::deleteImageServingUrl($full_path_photo);
When I run this, I get the "Getting URL...", then nothing. The page just stops executing.
Try/Catch was able to get an error message for this one. In my case it was kind of a generic error. For the overall issue I think has something to do with my buckets. I tried the whole thing with a different bucket and it seems to work. Here is the example -
// At the top of the page I call this
use google\appengine\api\cloud_storage\CloudStorageTools;
// Some processing takes place to get the storage location of the image in GCP storage then
if (is_file($full_path_photo)) {
try {
$object_public_url = CloudStorageTools::getImageServingUrl($full_path_photo, ['secure_url' => true]);
throw new Exception();
} catch (Exception $e) {
echo 'Error: '.$e->getMessage().'<br>';
};
Thanks for the help Tom.
i work with token (COM Object) , in java Script all things is okay . my problem is creating a object from it in php.
this is the code :
try {
// flag 1
$verifier = new COM("pkiactivex.Verifier") or die("Couldn't create the COM Component");
// flag 2
}
catch(com_exception $e)
{
//flag 3
}
first i should say this code work in another local system in wampp and i have xampp and i don't think it's because of xampp or wampp but i think maybe a module is missing. about code in my system code run until flag 1 and even its don't go to exception and flag 3 it's go no where , even i see a blank page with no error !!
i know it's not a php error and i put the code just for more detail!
This is driving me up a wall!
I'm trying to use phpmailer to send e-mail. Here is my code:
print "about to do the require!";
require_once("$s[phppath]/phpmailer.php");
print "require has been successful! creating a PHPMailer object!";
$mail = new PHPMailer();
print "yay!";
However, the program never gets to "yay", it dies at line 4 ($mail = new PHPMailer();). Most maddening is that no error message is displayed, even though php is running with display_errors = On. To test this, I threw in some obvious errors, like skipping semi-colons at the end of lines, and I immediately get an error message about this.
What the heck is going on here?
Edited
I modified the code like this:
require_once("$s[phppath]/phpmailer.php");
try {
$mail = new PHPMailer(true);
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Still no dice.
I am using PHPMailer also. Now in 2018 Version 6.05.
I was also in trouble with creation of $mail = new PHPMailer; I always saw the message that the class could not be created. But I was sure the file with the class was included.
What I found out, was that there is a namespace used within the class PHPMailer.
So the new object does not exist as "PHPMailer" but as class within the namespace PHPMailer\PHPMailer ! (not to be confused with folders of your installation).
What finally worked for me was to write the following line:
$mail = new PHPMailer\PHPMailer\PHPMailer;
// You need to add the namespace part after new and before class PHPMailer
Ok, there were two parts to this question:
1) Why no error messages? I feel very, very stupid about this one, because though I had display_errors turned on, my own script actually disabled error_reporting. Thank you Pathik Ghandi for pointing out this very simple fix. I can be quite special at times.
2) Why was PHPMailer not working? Because apparently PHPMailer now requires TWO files instead of one: class.phpmailer.php is not enough. You now also need PHPMailerAutoload.php. Hope this helps someone down the line!
Try echoing out $s[phppath] to make sure you are getting the right path. Redownload and reupload the phpMailer lib. Try using require instead of require_once. Try using include instead of require. Go into the phpmailer.php file and add in an echo or something to make sure you are actually requiring that file.
To be sure; Are you running some kind of framework? ie Wordpress? If so you may need to set a flag to show PHP error messages. WP: define("WP_DEBUG", true);
Try to call restore_error_handler() function above require.
require_once does not seem to work. I am trying to use Zend libraries to send mail but something does not work. I am using my web hosting server provider so I have to put the libraries in a subdirectory (without installing any framework).
// $path= dirname(__FILE__)."/ZendLibrary:".get_include_path() ;
$path= "./ZendLibrary".PATH_SEPARATOR.get_include_path() ;
set_include_path($path);
//echo $path ;
echo "Spot 0" ;
require_once 'Zend/Mail.php';
echo "Spot 1" ;
I got the "Spot 0" message but I don't get the "Spot 1" message. I have selected only two Zend libraries:
ZendLibrary/Zend/Mail/* (directory)
ZendLibrary/Zend/Mime/* (directory)
ZendLibrary/Zend/Mail.php (script)
ZendLibrary/Zend/Mime.php (script)
ZendLibrary is a folder in the same directory where my script is. What could happening?
UPDATE#1:
My problem is that the code stops working right when I run require_once. It does not echo "Spot 1" message. I have tried absolute paths for set_include_path, I have tried to load a .php library script without internal require_once statements, but nothing makes it work. As my test is run from a web hosting site in the Internet I don't have access to logs!.. or do I?
$path= realpath(dirname(__FILE__).'/ZendLibrary').PATH_SEPARATOR.get_include_path() ;
// $path= "./ZendLibrary".PATH_SEPARATOR.get_include_path() ;
set_include_path($path);
Zend Mail has some dependencies.
Hard dependencies
Zend_Exception
Zend_Loader
Zend_Mime
Zend_Validate
Soft
Zend_Locale
Zend_Date
Zend_Filter
Zend_Registry
The best is to add all Zend library to the include path in PHP and all components will load the dependencies automatically.
You havent met all the dependencies... At the minimum you also need Zend_Exception (all component specific exceptions in the framework extend form this) but im pretty sure there are others that Mail and Mime depend on. Just to make it easy on myself i would also grab Zend/Loader and use it for autoloading.
Update:
I took a look and it seems as though you will also need Zend/Validate and Zend/Loader which is required by one of the classes in Validate component - though Mail only uses Zend/Validate/Hostname (which does depend on the abstract classes and interfaces for Validate as well as Zend_Validate_Ip) so you might be able to get away with not grabbing Zend/Loader, but you might as well jsut make use of it for general purpose autoloading.
As a suggestion you could use [Swift Mailer]1 instead... youll be bale to avoid dependency hell and i like it a lot better anyhow. Only down side is its for Sending mail, while Zend_Mail will also let you read a mail store on a local or remote server.
I used Zend_Mail with SMTP standalone before, here are the files I needed. I also reduced it down to what you need if you only want to use sendmail also.
If you want to use Sendmail, that is the easiest. Your dependencies are:
Zend/Exception.php
Zend/Mail.php
Zend/Mime.php
Zend/Mail/Exception.php
Zend/Mail/Transport/Abstract.php
Zend/Mail/Transport/Exception.php
Zend/Mail/Transport/Sendmail.php
Zend/Mime/Exception.php
Zend/Mime/Message.php
Zend/Mime/Part.php
And with those files, here is an example use:
<?php
// optionally
// set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/Zend');
require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Sendmail.php';
$transport = new Zend_Mail_Transport_Sendmail();
$mail = new Zend_Mail();
$mail->addTo('user#domain')
->setSubject('Mail Test')
->setBodyText("Hello,\nThis is a Zend Mail message...\n")
->setFrom('sender#domain');
try {
$mail->send($transport);
echo "Message sent!<br />\n";
} catch (Exception $ex) {
echo "Failed to send mail! " . $ex->getMessage() . "<br />\n";
}
If you need SMTP, then you have a few more dependencies as prodigitalson showed. In addition to the above you need at least:
Zend/Loader.php
Zend/Registry.php
Zend/Validate.php
Zend/Mail/Protocol/Abstract.php
Zend/Mail/Protocol/Smtp.php
Zend/Mail/Transport/Smtp.php
Zend/Validate/Abstract.php
Zend/Validate/Hostname.php
Zend/Validate/Interface.php
Zend/Validate/Ip.php
Zend/Validate/Hostname/*
Zend/Mail/Protocol/Smtp/Auth/*
Then you can do something like this:
<?php
require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Smtp.php';
$config = array(//'ssl' => 'tls',
'port' => '25', //465',
'auth' => 'login',
'username' => 'user',
'password' => 'password');
$transport = new Zend_Mail_Transport_Smtp('smtp.example.com', $config);
$mail = new Zend_Mail();
$mail->addTo('user#domain')
->setSubject('Mail Test')
->setBodyText("Hello,\nThis is a Zend Mail message...\n")
->setFrom('sender#domain');
try {
$mail->send($transport);
echo "Message sent!<br />\n";
} catch (Exception $ex) {
echo "Failed to send mail! " . $ex->getMessage() . "<br />\n";
}
Answer:
My ISP (web hosting too) has a setting to disable require_once. I was able to setup a Unbuntu virtual machine on VirtualBox and set up a web server. I copied the content of my test web site in my Unbuntu and find out that require_once does work as expected !!! That may explain why the server-side include did not work for me either. The following was a usefull check, that indicated me the file was in the expected place.
$file = 'ZendLibrary/Zend/Mail.php';
if ((!is_file($file)) or (!is_readable($file)))
die("File does not exists or is not readable.");
Now I have to find out why it is disabled, how to enable it, or just switch web hosting company. Update: I had to pay $20 dolars a year more for the provider to enable PHP (in this case to be able to include).
This site can tell you more about it.