Check if the mail is queued in server with mail() function - php

I am new to php.
I have used a mail() function in order to send mails.
I know for a fact that the mail function is used just for queuing up the mail to the server and further has no hand in it.
I wanted to ask whether there is any method of knowing as to what was the error if the mail has not been queued to be sent on the server.(Sending of the mail being an entirely different problem).
Considering my code to be:
if(mail($to,$subject,$content,$headers))
{
echo"Sent";
}
else
{
print_r(error_get_last());
echo"not sent";
}
I have seen this question but the answer doesn't solve my problem.
Thanks in advance and apologies if the question appears to be baseless to you.

Related

PHP mail successful with empty to parameter

I am learning PHP and from what I understand the mail function has a to parameter that needs to comply with a certain string format - php doc. I have read that if I parse an empty string then the mail function will return false (not 1). However, when I try this the mail function never fails. Is there something I have missed?
Code:
<?php
if (mail('', 'mySubject', 'myMessage')) {
echo "Success!";
} else {
echo 'Failure!';
}
?>
The output is "Success!"
Only by removing the argument entirely gets the else statement to execute. Does the to parameter need to be of a certain string format like the documentation states and if not, then how can I get this function to fail? Thanks
The mail() function does not validate the input. It more or less just takes the data and hands it over to the system mailer daemon.
If that handoff was successful, the method returns true.
It it likely that your local mailer daemon will log an error that it couldn't process the email
php mail() function is just a middle-man between your mail daemon and your code.
You need to manually validate email addresses, typically using something like
if( filter_var( $email_address ,FILTER_VALIDATE_EMAIL ) )
{
mail(parameters);
}
else {
// handle error
}
Like #skaveRat said, you'll most probably find something in your email daemon log that this mail was not sent.

Continue execution in php after a function call without depending on function execution

This sounds a bit odd but I have a situation where I have to send email using php mail function. This mail function is in a sub function. Now I want to execute my php script complete without having to worry about success or failure of the mail function. Like I don't care whether the mail function was executed successfully or not because of any error or something but I want my php script to be executed completely. How can I achieve this ?
Thanks in advance.
A long as your use of the mail function is correct, your php script should continue to run after executing the mail function whether it succeeds or not in sending the mail.
What is the error you are getting? You could always wrap it in...
try {
} catch(Exception $e) {
}
I know this was years ago, but I was looking for something similar, and I think this is more what you're after - create a separate file with that function, and call it separately - essentially as a separate action:
exec("php mailuser.php > /dev/null 2>&1 &");
From Prevent PHP From Waiting for mail() function - see the accepted answer there for more details.
(If the mail function delay is not the concern, then a normal try/catch should likely be sufficient.)

how many email have been sent using mail function php

Is there any way possible to find out the total number of sent emails from php mail function. My mail function is inside a while loop and I want to know the number of sent emails.
Thanks
If you just want to know the number of mails accepted for delivery in the while loop, add a counter variable:
$mailsSent = 0;
while($condition) {
if (mail('foo#example.com', 'My Subject', 'My Message')) {
$mailsSent++;
}
}
echo $mailsSent;
For the total amount of mails accepted for delivery, you can configure a log file in php.ini
mail.log string
The path to a log file that will log all mail() calls. Log entries include the full path of the script, line number, To address and headers.
Reference: http://php.net/manual/en/mail.configuration.php#ini.mail.log
If you want to know the number of mails which actually got sent, check the sendmail log.
Re-Edited the Answer! Please check now. I was confused at first!
You can use this way to check how many mails have been sent by using this script:
<?php
$count = 0;
while ($condition) {
if(mail($to, $subject, $message))
$count++;
}
echo "Totally, $count messages have been sent!";
?>

ValidateAddress on PHPMailer behaves odd

I'm using PHPMailer and $mail->Send() is returning an error, my problem is when I use this email-string "noreply#pleasenoreply.com" within $mail->SetFrom(), but in the other hand it works fine with almost any other email i.e "hello#hello.com".
After debugging the code I found out that the problem is in the file class.phpmailer.php over the function ValidateAddress(). It seems that the email "noreply#pleasenoreply.com" is not valid by FILTER_VALIDATE_EMAIL nor the preg_match
PHPMailer - class.phpmailer.php - line 550:
public static function ValidateAddress($address) {
if (function_exists('filter_var')) { //Introduced in PHP 5.2
if(filter_var($address, FILTER_VALIDATE_EMAIL) === FALSE) {
return false;
} else {
return true;
}
} else {
return preg_match('/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+#(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/', $address);
}
}
Why is that possible?? does anyone have any idea what is going on??? why this email "noreply#pleasenoreply.com" is not allow?
my problem is when I use this email-string "noreply#pleasenoreply.com" within $mail->SetFrom()
I don't know why that specific address is being rejected and others aren't, but generally, you need to specify not only a valid E-Mail address as the from address, but one that is handled on the mail server you're sending the message from.
Otherwise, either the sending server is going to deny sending, or the receiving server is very likely to throw away the message as spam.
The usual policy is to specify noreply#yourdomain.com (yourdomain.com being your website domain). On some servers, you need to actually set up that address to be allowed to send mail from it.

PEAR Mail_Queue stops on recipient bad syntax

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!

Categories