i'm using PEAR Mail_Queue, everything works great, except when i'm trying to send emails to
"bad" recipients (bad syntax like "òla#test.com", "uuu#test,com" , "test#test.com.com")
When the queue finds a bad recipient it just stops, leaving all the others mail in the db queue table...
I just want to make it jump to the next mail, deleting (or not) the bad mail in the queue table...maybe all i need is just some errors handling...
the code i'm using (if you need more code just ask :) ) :
/* How many mails could we send each time the script is called */
$max_amount_mails = 10;
$query=mysql_query("SELECT count(*) FROM mail_queue ORDER by id asc");
$num_tosend= mysql_result($query, 0, 0);
$num_mail=ceil($num_tosend/$max_amount_mails);
/* we use the db_options and mail_options from the config again */
$mail_queue =& new Mail_Queue($db_options, $mail_options);
$mail_queue->setBufferSize(10);
$contaEmailSpeditesi=0;
/* really sending the messages */
for($i=1;$i<=$num_mail;$i++){
$mail_queue->sendMailsInQueue($max_amount_mails,MAILQUEUE_START,1);
sleep(2);
}
thanks !!
You should open a bug report in the pear bug tracker.
You really should filter out the invalid email addresses before adding entries to the mail_queue table - probably not the answer you want though!
Related
I have hostgator hosting. I want to use Cron Job. I am using this command to perform my task
/opt/php54/bin/php /home/abskillz/public_html/cron.php
Now the question is every time when cron is perform I got email that your action has processed or etc. Can anyone tell me that how can I stop getting this email?
I just remove the output message from my PHP page. So no more emails now :)
You can always set some persistent flag to indicate if sending email is required.
if (file_exist(__DIR__.'/cron_flag_'.self::SEND_EMAILS.'.php')) {
mail('a#b.com', 'subject', 'message');
}
if ($this->db->queryScalar('SELECT value FROM cron_flags WHERE type = "'.self::SEND_EMAILS.'"')) {
mail('a#b.com', 'subject', 'message');
}
So if flag is not set, CronJob just will just jump to the end of file and no processing will be done.
Please don't laugh at me but I believe that I just did something extremely stupid. I was working on setting up a newsletter for a site that I am working on, but I tried it out at first when there was a typo. While scanning throughout the database and sending emails, I screwed up on the part that makes it stop. I fixed the code, but the emails are still being send (to my mom :O) and they don't seem to be stoping.
This is the script when I executed it:
$message = $_POST['emailmessage'];
$subject = $_POST['subject'];
$query = mysql_query("SELECT `email` FROM `members` WHERE `active`='1'");
//This line underneath should not be there
$rows = mysql_fetch_assoc($query);
$headers = array(
"From: contact#thestopitcampaign.com",
"Content-Type: text/html"
);
//should be '$rows = mysql_fetch_assoc($query)' instead of '$rows'
while($rows)
{
mail($rows['email'],$subject,$message,implode("\r\n",$headers));
echo "<p>Sent to: " . $rows['email'] . "</p>";
}
I contacted FatCow to see if they could stop the script, but they said that they could not do that and they would have to delete my entire account and put me on a different server. I cannot do that. Is there anyway to generate an error or something that would make the rogue script stop? FYI I do not have SSH access.
--I looked in my php config file and the timeout for a script is 300 seconds. That seems like a lot of emails to send. Is there anyway to stop those emails?
What has been sent can't be stopped any more. But it won't run forever and probably has already stopped.
If the server is not grossly misconfigured by the provider, your script didn't run any longer than a certain time limit, e.g. 60 seconds. Even though messages continue to come through, it's probably no longer running, but the mail server is taking its time to handle all the messages that it created.
I would wait and see - the flood is likely to end soon.
What the provider says about moving the account to a different server doesn't seem to make any sense at all - if there is a rogue process that is sending E-Mail, they should be able to kill it easily. But anyway... I would wait.
My guess is that the emails are just queued up and should eventually stop as php script wont keep executing itself after the page has stopped loading.
I'd suggest you to use a newsletter provider like mailchimp as they are more relyable, are safe and the service is easy to integrate in your website.
Hope that helps!
hi i'm trying to pipe incoming emails to php script
i did step by step from my manual but when i send an email i get this error from my delivery system :
A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es)
failed:
pipe to |/home2/mimjobco/public_html/pipe.php
i chosse pipe.php address from cpanel so it cant be wrong
here is the picture !
here is my pipe.php code :
#!/usr/bin/php –q
<?php
require_once('class/support.php');
require_once('class/db.php');
$title = 'email_request';
$text = 'email_req_text';
$sup_id = 1 ;
$sup = new support;
$sup->title = $title;
$sup->part_id = $sup_id ;
$sup->text = $text;
$sup->email = 'email';
$sup->type = 1;
$sup->set_ticket();
mail('xxxx#gmail.com','new message recived','new message recived ');
i also set it's permission to 755
am i missing something ?
is there another thing that i should have done? someone had mention something about crone job ?!
The error message says "/home2/mimjobco/pipe.php", but the screenshot shows the file is in your "public_html" directory (presumably, "/home2/mimjobco/public_html/pipe.php").
Note that "pipe.php" should only be in "public_html" if you want it to be accessible on the website. Otherwise, it should go elsewhere in your home directory hierarchy (e.g. ~/bin/).
It looks like you are using bluehost, here is someone else who was able to fix the problem: http://www.bluehostforum.com/showthread.php?5786-Reading-email-with-php&p=26554#post26554
Setting up filter in cPanel, problem that delivery error coming back. Many solutions not available on a shared server.
Finally, this seems to work to avoid the error in delivery emails coming back - note the -q:
|/usr/bin/php -q /home/myacct/public_html/mydir/myfile.php
Solution came from http://forums.exocrew.com/index.php?showtopic=1838. (DEAD LINK)
The code for parsing emails being piped to the php file is at:
http://evolt.org/node/27914/
This gives you the message parsed as variables for subject, from, body etc.
Hope this helps!
I would like to be able to get an array of emails and make sure each email is sent. (e.g. Array > send each email > result) I kind of changed the question here because this is more important, plus I have added a 50 rep. point. Codewise how can I do this?
Apart from still using the mail() function, you probably want to setup a cron job for sending out the mails. For spooling mail send jobs use a separate database table. Or if it's about some sort of mailing list functionality, then a simple recipient list will do.
If you just want to send out a bunch of the same email at once, you
could call implode() on your array of emails to turn it into a string:
$to_string = implode(', ', $to_array);
Or, if you want to try something more complicated, you could use a
foreach loop to cycle through each email and to keep track of
successes and failures:
$success = array();
$failure = array();
foreach ($to_array as $to_email)
{
if (mail($to_email, $subject, $message, ...))
$success[] = $to_email;
else
$failure[] = $to_email;
}
I'm guessing your original question had to do with sending out all these
emails every day or something without necessitating you hitting a
button. If you have ssh access, see what happens if you type:
crontab -e
If you get some sort of error, you will have to speak with your system
administrator about cron. If you get a file, then you can use cron. This
is not a part of your current question, though, so I'll leave it.
The same way. You must have code that sends an email to an email address. Whether they are on the site or not, it is the same code. You just need to know their email address.
EDIT: If you are wondering how you would trigger the email to be sent, maybe you want to schedule it using a cron job, for example send an email every day at midnight.
This says it all, really: http://php.net/manual/en/function.mail.php
You just need an outgoing mail server installed (postfix, exim, sendmail)
Easy way to send an email:
$to = "usermail#test.com";
$from = "my_email#mydomain.com";
$subject = "Hello!";
$contents = "This is an test mail!";
mail($to, $subject, $contents, "From: $from");
If you don't have access to cron jobs then you will probably struggle to run without user interaction.
A common method for dealing with this is to run on every nth page load, or every so-often. This only works if you have a site that's visited about as often as you want to send email. You'll also want to use an ACID-compliant database. Pseudo-code follows.
if (1 == rand(1,100)) { // run once every 100 page loads
$emails = get_emails_to_send();
mark_emails_as_sent($emails);
$results = send_emails($emails);
mark_failures_as_needing_to_be_sent($results);
}
Alternately, you can run it on a timer:
if (time() - get_last_time_run() > $run_at_least_once_every_this_many_seconds) {
$emails = get_emails_to_send();
mark_emails_as_sent($emails);
$results = send_emails($emails);
mark_failures_as_needing_to_be_sent($results);
}
Or you can combine both with a &&. This depends on how often your page gets hit.
If you want to send email more often than you get page hits... too bad ;). Or have an external service call the page every so often.
I'm trying to create a PHP daemon that connects to an IMAP server and processes emails as they come in. I have it close to working, but the daemon keeps grabbing the original emails that it finds the first time the daemon is loaded. I believe the reason is because I'm opening the IMAP connection in the parent process. Example below:
if ($imapConnection=imap_open($authhost,$user,$pass) or die())
{
//start daemon
while()
{
//Grab email headers
$imapHeaders = imap_headers($imapConnection);
$count = sizeof($imapHeaders)
//loop the emails
for($i = 1; $i <= $count, $i++)
{
//process the email
//delete the email
}
System_Daemon::iterate(15);
}
}
imap_close($imapConnection);
I'd like to stay away from putting the IMAP connection within the loop. How can I keep the connection to the IMAP server outside of the loop and still get new emails?
In IMAP, mails stay on the server. So each time you come, if you have not explicitly removed them, old emails are still there. To prevent processing these emails, you could have a var that keeps the amount of mails you treated before, so you could go from $i = 0 (supposed the last arrived) to $i < $var where $var stands for the number of mails already treated.
EDIT :
Since you delete the mail by imap_delete, do an imap_expunge at each loop.
EDIT 2 :
Use imap_reopen, I tried you script on my server using imap_reopen($imapConnection, "{domain.tld}INBOX"); after each loop and it sees the new mail now. Does not do a new authentication, just move your stream.