Convert a php resource to a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sjuaq
Forum Commoner
Posts: 33
Joined: Thu Aug 10, 2017 4:48 pm

Convert a php resource to a string

Post 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
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Convert a php resource to a string

Post 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);
(#10850)
sjuaq
Forum Commoner
Posts: 33
Joined: Thu Aug 10, 2017 4:48 pm

Re: Convert a php resource to a string

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Convert a php resource to a string

Post 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.
(#10850)
Post Reply