Page 1 of 1

Convert a php resource to a string

Posted: Sat Oct 06, 2018 10:38 am
by sjuaq
Hi,

I was creating this little program to help me out sending emails but there is a issue i can't solve.

Convert a resource to a string
$file returns Resource id #3 instead of the value of the string

Here is a code example:

Code: Select all

<?php
$file = fopen("emails.txt","r");

while(! feof($file))
  {
  mail($file, 'Thanks for joining!', Hi\nWe are glad to see you onboard\n\nPrivateloader);
  echo fgets($file). " - just received a email.<br>";
  }

fclose($file);
?>
Thanks

Re: Convert a php resource to a string

Posted: Sun Oct 07, 2018 9:35 pm
by Christopher
The $file variable is a resource returned by fopen(). The first parameter for mail() should be a string containing the email address of the recipient.

Are the email addresses in the file? If so, then read the line into a variable:

Code: Select all

  $email = fgets($file);
  mail($email, 'Thanks for joining!', Hi\nWe are glad to see you onboard\n\nPrivateloader);

Re: Convert a php resource to a string

Posted: Thu Oct 11, 2018 1:56 pm
by sjuaq
Thanks it really helped.

I have one other question how do i increase the time limitation of 30 seconds per execution in PHP?
I'm having problems running the whole script under 30 seconds.

Re: Convert a php resource to a string

Posted: Sat Oct 13, 2018 7:14 pm
by Christopher
sjuaq wrote:I have one other question how do i increase the time limitation of 30 seconds per execution in PHP?
I'm having problems running the whole script under 30 seconds.
You cannot really speed up how fast mail is sent. You probably need to call the script multiple times. Each call would only sends a small number of emails. But then, you will need to keep track of the number of the last email. It would be much easier with a database. Each record could have the email address and whether it had been send. Then just select the next N unsent email each call, and mark them in the database when they are sent.