Previously I have the mail() function in PHP to work with my site. Now we got a domain for the site ( nginx server ) and now the mail() just does not want to behave, always returns false. I have the PHP ini set up for mail() with the correct domain as
So...I turned to PHPMailer and it's still not working....
I get this error....
Could not instantiate mail function.
so my question is what is going wrong? I do not have a SMTP host... i want to use the local server as the mailing server as it was working before and I believe I have the right set up for that.
Was installed with composer.
This is my code:
require_once($_SERVER['DOCUMENT_ROOT']."/vendor/phpmailer/phpmailer/src/PHPMailer.php");
require_once($_SERVER['DOCUMENT_ROOT']."/vendor/phpmailer/phpmailer/src/SMTP.php");
require_once($_SERVER['DOCUMENT_ROOT']."/vendor/phpmailer/phpmailer/src/Exception.php");
$emailer = new PHPMailer\PHPMailer\PHPMailer();
$emailer->IsMail();
$emailer->SetFrom("noreply#my.domain");
$emailer->FromName = "My Domain";
$emailer->AddAddress($admin_data['email']);
$emailer->isHTML(false);
$emailer->Subject = "test";
$emailer->Body = " test "
if(!$emailer->send()){
give_error("Send Email Error: " . $emailer->ErrorInfo);
return false;
}
When looking to get under the hood of why your PHPMailer-driven email is failing to send, be sure to turn on PHPMailer's "SMTPDebug" option.
To do this, set the SMTPDebug property to "3" after instantiating your PHPMailer object. In your code, it would look like:
//Echo errors, server responses, client message, data, commands & connection status
$emailer->SMTPDebug = 3;
This will echo out all errors, server responses, and client messages that the email might be spilling out as it's trying to complete its sending. With this, you should be able to get a better handle on what might be causing your email to mis-fire or not send at all.
Even if you're try-catching errors currently, it won't capture the full breadth and depth of information without this option turned on.
Please remember, however, to turn this option off before pushing to production. Setting the SMTPDebug property back to 0 will ensure you're not leaking errors, or important server information to the end-user.
//Will disable all debugging.
$emailer->SMTPDebug = 0
You can find more information about the SMTPDebug property within the PHPMailer documentation, which is likely named "PHPMailer-master/docs/" within your codebase.
Follow the links to the troubleshooting guide. In order for the mail() function to work, you need a local mail server installed - I recommend postfix. It’s nothing to do with your code.
That said, while using a local mail server is a good choice, it’s both faster and safer to use SMTP to localhost than to use the mail function. See the PHPMailer docs for why that is.
Related
I have a daily newsletter I send out to a list of people each day. I use PHP and the PHPMailer object to send the emails.
This has been working fine up until today when I didn't get an email (test) and I checked the logs to find that intermittently I was getting SMTP errors back from
mail->Send();
Method of the class on SOME of the people - not all of them.
All of the emails were exactly the same size (length in characters) apart from their name and email address.
The error details from
$mail->ErrorInfo;
Included these two errors
SMTP Error: Data not accepted.<p>SMTP server error: 2.1.5 Ok
SMTP Error: Data not accepted.<p>SMTP server error: </p>
Why there are broken P tags in the error message I have no idea - also why the first error has OK in it??
However other people in the loop had emails go out fine.
Also when I put the script into test mode so ONLY these failures were emailed out (so one call of the script = one email to one person), I found no problem and the people got their emaail.
Now I asked tech support what the error meant and got back "Google it" and I can't find the error 2.1.5 OK anywhere. I don't really get why it says OK after an error either.
As a test for tomorrow I put a test in for a failed Send and then a Sleep for 5 seconds with a retry.
I don't know if this will help but I thought that maybe my SMTP server which is on the same server as my script (Rackspace VPS Linux) was having temporary issues connecting so a sleep might be handy in this situation.
This is the first time I have had this issue, I haven't rebooted APACHE or my server (or mailserver) and before this script I had another one send out 403 emails without any problem.
It would be nice to know what the error is and why it happened though.
Rob
That error is defined exactly where you would expect it to be - in the RFC that defines SMTP error codes. 2.1.5 means "Destination address valid". That it starts with a 2 means it's not an error as such, but a response code - warnings and errors start with 4 and 5. Why you got it, I don't know, but it certainly seems it was not expected in that context. Mail servers often have problems - they run out of disk space, get swamped by spam filtering, and this is why mail servers have queues. Sending directly using PHPMailer's SMTP class is not appropriate for high-volume sending - using SMTP is fine, but point it at a proper (nearby) MTA that can handle deliveries properly.
As for the garbled ErrorInfo content, I suspect you are using an old version of PHPMailer - the debug output has been cleaned up fairly recently. Another thing that changed lately is the handling of errors during single sends to multiple recipients (e.g. BCCs) - it's now much more likely to get to the end of a long recipient list without giving up than it was before.
We have shared hosting servers which use PHP fastcgi (on IIS) for several clients (shared hosting). Regularly clients use old exploitable code which causes holes in their applications that eventually gets used by hackers to install malicious code. Most of the time this code is being used to send spam from our servers.
We have no control over our clients code, so patching the holes is quite impossible.
We would however like to block the clients sending spam once they send more then X email messages in Y amount of time.
The setup is fastcgi based, so there is little relation between php and the webserver.
PHP sends its mail through SMTP on localhost. The mailserver allows relay of all localhost connections (obviously).
One thing that goes through my mind is setting an environment variable containing an identifier in the fastcgi environment and using php's prepend file option to add a header to all mail send by php's mailer. After that we could use that mail header to identify the spamming culprit.
The option above still would not take care of spam scripts using regular telnet (telnet localhost, HELO, MAIL FROM etc... ) when sending email.
My question to you: is the idea that i've mentioned the best and perhaps only option to deal with our issue? Or are there better solutions for this situation? And if so, please explain how you would deal with the issue.
You can filter that on you MTA (message transfer agent). For example, allow no more than 50 emails in 1 hour for each user in Exim ( http://www.exim.org ) config file (/etc/exim/exim.conf):
begin acl
acl_check_not_smtp:
warn ratelimit = 0 / 1h / strict / $sender_address_local_part
log_message = Sender rate $sender_rate / $sender_rate_perio
acl_not_smtp = acl_not_smtp
begin acl
acl_not_smtp:
deny message = Sender rate overlimit - $sender_rate / $sender_rate_period
ratelimit = 50 / 1h / strict
accept
And no matter how they try to send, via php mail() or other method.
Most shared hosts block the use of PHP's mail() function, as that can be easily exploited. Instead they advice using sendmail or similar scripts which require SMTP authentication before sending. Assuming you're not already doing this, once implemented, you should be able to keep track of number of emails sent from a particular domain/email account and put restrictions on it.
Okay, stick with me on this one. I have not implemented it but it looks good.
The concept here is that you could
run a php file before EVERY page on your customers site
in that php file rename the mail function to mail_internal().
in that php create a new function called mail to do your check / verification that your customer is allowed to send mail, and if they are call the mail_internal() function with the same parameters.
You will have to install the runkit PECL extension
http://us.php.net/manual/en/runkit.installation.php
Changes
in php.ini
auto_prepend_file /var/www/allclients_forcedfile.php
in /var/www/allclients_forcedfile.php
runkit_function_rename ( "mail" , "mail_internal" );
function mail ( $to , $subject , $message, $additional_headers = "", $additional_parameters ="" )
{
$args = func_get_args();
error_log("mail_internal : $_SERVER[HTTP_HOST] : ".implode(" : ",$args));
//lookup whether you want to send more mail for this client maybe by keeping a counter in some file in the $SERVER[DOCUMENT_ROOT]
if($sendmoremail)
return mail_internal ( $args[0], $args[1] , $args[2], $args[3] , $args[4] );
return false;
}
As expected it seems Stack Overflow is not the right place for this question. The provided answers do not expose some clear method to identify FastCGI sessions' connections to the MTA (SMTP) server.
I will go with my initial concept of adding an identifier to php's environment. This identifier can be read in PHP's prepend file by using the getenv() function. This identifier can then be added to mail headers for outgoing mail.
Furthermore I have enabled the mail.add_x_header ini setting which will help identify which script caused the spam run.
I am leaving the question open for the bounty duration, hoping other options will magically appear :)
I'm trying to send an email using php. I've the following only it doesn't seem to output anything to my browser nor send my email, can anybody see what i'm doing wrong?
$to = "liam#mysite.co.uk";
$subject = "Alien Sighting";
$message = foreach ($_POST as $key=>$value) {
$$key = $value;
}
$from = "Sighting#site.co.uk";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
Can you give us $_POST content please ?
Does your PHP configuration display all errors messages? Make sure to use error_reporting (E_ALL) in your dev environnement to find any typo / error. That should help to get an output to your browser.
You have to be sure to setup your SMTP server to send your email if you are running the script on your computer otherwise you can get a test server so you don't have to setup the SMTP it will send automatically
Are u using Rmail.php? Do u uploaded your code to a webserver or is it on your local PC?
I am always using Rmail.php to send mails.
$mail = new Rmail();
$mail->setFrom(YOUR ADDRESS);
$mail->setSubject(YOUR SUBJECT);
$mail->setHTML($textSchueler);
$mail->send(array($address));
You'll also want to check and make sure your server is configured to send email at all. While most shared hosting accounts will be configured for you if you are setting up your own server or developing locally PHP may not have the any way to send the messages. Try checking to see if the mail() function returns true indicating the message was successfully sent and then look in the mail logs on the server to verify transmission.
In this kind of situation where PHP does not output anything to your browser (even it should), most likely the script fails before your echo statement.
Make sure you have not disabled error reporting. See PHP runtime configuration error_reporting and display_errors. You can change error reporting behaviour also runtime with error_reporting() function in the code. Enabling error reporting helps you to figure out what the problem might be.
Even you had disabled the error reporting on the browser, check out your webserver logs. They should have details what's the problem (syntax error in the script, failed mail delivery by mail function call... there are many possibilities).
I am a newbie in Web Development an I am currently learning PHP and MySQL. I have read HeadFirst PHP and MySQL, and tried the examples. But The PHP Mail() function doesn't work on my Local Machine. I have tried the script on a web server but nothing happens, the mail isn't sent. Please Help me. I have configured the PHP.INI file to send emails but still the problem persists.
<?php
$to = "me#me.com";
$sub = "hello";
$msg = "Hello, how are you?";
//Mail Function
mail($to,$sub,$msg);
?>
I am using WebMatrix with PHP 5.2 installed. Please help me, I am trying out this one since last 2 hours! I am stuck!
mail() uses 'localhost' to send - it generally assumes it's on Linux.
You will need to aquire a basic SMTP server and run it on windows, OR you may be able to use the SMTP server of your're ISP.
Whichever option, you will need to edit your php.ini, you will find:
[mail function]
; For Win32 only.
;SMTP =
you must set SMTP to the ip/port of a mailserver - again wither run one locally or use your ISP.
EDIT
You could try this approach - I have personally never tried to use GMail for sending: http://www.linuxquestions.org/questions/programming-9/php-pear-mail-packege-support-security-through-ssl-586976/
But The PHP Mail() function doesn't work
Yes it does. The problem is either with how you configured PHP or with the MTA you configured it to use. You'd need to provide details of both for us to understand why mail is not getting sent.
The above answer also is applicable to those who use a simulated local IIS e.g. the WebMatrix on IIS Express users who reside within a corporate network with a SMTP machine available.
To be able to send emails out from within it one needs to edit the PHP.INI file (found typically in \Program Files (x86)\IIS Express\PHP\v5.3) and replace 'localhost' with the SMTP server IP or DNS name.
we're starting to build a web app. My colleague is developing on linux, and I am running via a WAMP stack running windows xp. We're using Zend.
When we submit a form and send an email using Zend email, the email would send and then I would get a blank screen, wheras on the linux machine the app would continue normally.
So I wrote my own little script, mail.php which uses phpmailer - and the exact same thing happens, the email sends, and then blank screen. So we have:
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
So there is no error reported, the email sends, but "Message has been sent" never prints to the screen (or anything else, normal HTML too).
I am not very technical, so apologies if there are obvious debug steps to take. Is there something peculiar to windows php config that I have missed?
It's an off-site SMTP server with authentication.
Sounds like you are getting an error, but just not seeing it. Make sure you have this somewhere in your code
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
And inspect your apache logs for 500 errors as well.
PHP has it's own error log, when in doubt check there. You should be able to locate it by running
<?php
phpinfo();
?>
It should be located in the PHP Core section - if it's blank, edit your php.ini file and turn log_errors on and specify where you want the file to be.
Errors I couldn't get to display I've found using this.
UPDATE
Did some digging and it seems that Zend_Mail is essentially a wrapper for PHP's mail() function according to the documentation: http://framework.zend.com/manual/en/zend.mail.html
With that in mind there's some information on PHP's mail() function in the PHP manual that you're going to want to look at regarding SendMail http://www.php.net/manual/en/ref.mail.php the first comment on the page (as of this writing) has all the details on configuring your WAMP server to behave like a *nix server - at least as far as mail() operations go ;-)
I use phpmailer with success on a windows box (my dev machine). Can I see the setup code? I do something like the below. One thing is you need to make sure openssl module is installed in php if you are using ssl. Take a look at the below. Make sure your SMTPDebug flag is set to have some output that you can work with.
<?php
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "blah.com";
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "mail.blah.com";
$mail->Port = 465;
$mail->Username = "noreply#blah.com";
$mail->Password = "smtppass";
$mail->SetFrom('noreply#blah.com', 'Blah Name');
$mail->AddReplyTo("noreply#blah.com", "Blah Name");
$mail->Sender = "noreply#blah.com"
?>
apologies for taking so long to answer this. The problem was caused by a firewall in the office blocking outbound SMTP traffic. I am still not sure as to why it returned nothing - but outside of this office when it was tested the php errors for invalid smtp etc. returned fine. Just a case of getting the appropriate ports allowed on the network.
Thanks everyone for their help.