PHP mail successful with empty to parameter - php

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.

Related

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.)

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

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.

No Response from Send() function when using PHPMailer

I am using PHPMailer to send email using a contact form on my web page. I am using the code
$msg = $mail->Send();
echo $msg;
but nothing is printed out.. Obviously I have all my email/domain settings above these lines. Funny thing is that the email is sending correctly, but I get no response (true OR false) from the send method, thus I cannot run an if statement to redirect to another page according to whether the email has been sent or not..
Replace your $mail->Send() call with:
if(!$mail->Send()) {
echo $mail->ErrorInfo;
}
The most likely reason you're getting no output when you echo $msg is because the result is false. When you echo false it will display nothing. Use var_dump instead of echo.

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.

How to flush output in ZF

I have a controller that, among other things, sends emails. I need to echo a message to the user before the email sending starts (otherwise it looks like the screen is stuck).
So, how do I echo a message which is set in the start of a controller, before I reach the end of the controller, or, should I think in another direction all together?
Try maybe:
<?php
//...
public function someAction()
{
echo "Something";
ob_flush();flush();
}
This forum post discusses your issue. They suggest:
<?php
$frontController = Zend_Controller_Front::getInstance();
$frontController->setParam('disableOutputBuffering', true);
And then performing the ob_flush();flush(); technique.
For the mail part of the question.
Depending on the timing - how soon after the request the message needs to be sent - another possiblity would be to create a message queue (maybe a db table), write a record to that table, and then run a cron process that consumes the queue, sending any unsent messages, marking them as sent, etc.
You could try using a shutdown function to send the email. If you also flush the output buffer this will make sure the user gets to see the rendered page first. In your code call:
register_shutdown_function('send_email', $params);
And then have a function that looks like:
function send_email($params) {
ob_flush();flush();
// Send your email here
}

Categories