I'm experiencing problems with PHPMailer, the version of the class is 5.1, which is generating the following error: Could not instantiate mail function error that corresponds to an instance problem.
Checking the possible causes of this, I have seen that the directive: disable_functions is including the mail() function.
So when I use it, I get the following error:
Warning: mail() has been disabled for security reasons
Given all this, I wonder if the error generated, could not instantiate mail function, is directly related to the lack of this function in php.ini.
Obs: The MX server is configured for G Suite, so the SMTP settings are from google gmail.
I removed this and resolved it: $mail->Mailer = "SMTP";
Related
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.
Issue: I'm attempting to send mail from a contact form created using the Contact Form 7 (CF7) plugin for Wordpress. I've configured the basic form, sending to my personal email upon success. However, each submission results in a failure.
Environment: Docker wordpress container (runs only Apache, no sendmail program installed). Also, in wp-config.php, I've added this block to configure PHPMailer to use IsSMTP, and this has tested successful in all other places in WP, as well as with manual PHP scripts:
add_action( 'phpmailer_init', 'phpmailer' );
function phpmailer( $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = 'mysmtpopenrelayserver';
$phpmailer->SMTPAuth = false;
}
Expectation: Upon inspection of Contact Form 7 plugin file /includes/mail.php, it should be using phpmailer(), but the Apache error log shows an email of "sh: sendmail: command not found" so this indicates to me that CF7 is really attempting to use mail() function instead.
I need some explanation to better understand what's going on here. I don't want to install sendmail into this container.
By default PHPMailer (and thus CF7, I assume) acts as a big wrapper around PHP's built-in mail() function. The mail function requires that you have a working local mail server, most commonly postfix.
To work around this, configure CF7 to ask PHPMailer to use its own SMTP client, which will then send directly via whatever relay you configure (e.g. gmail), will not call mail and thus does not require a local mail server.
wp_mail() calls PHPMailer::IsMail() internally (see the PHPMailer docs).
This ensures that PHPMailer uses PHP's built-in mail() function.
I use new version of PHPMailer. On the server, I get an error:
Warning: escapeshellcmd() has been disabled for security reasons in /public_html/library/email/class.phpmailer.php on line 1442
Is there a function that could replace escapeshellcmd() ?
No, escapeshellcmd() has some inherent problems which make it worth disabling for many, but you can work around it a different way: use SMTP to localhost instead.
By default PHPMailer uses the PHP mail() function for sending, which calls a local sendmail binary via a shell (requiring the use of escapeshellcmd()), which in turn opens a synchronous SMTP connection to your mail server on localhost. You can skip much of this by sending directly to localhost yourself, bypassing the shell overhead. Do this:
$mail->isSMTP();
$mail->Host = 'localhost';
Other settings should work with defaults. Two advantages of using SMTP to localhost are that you can get much better feedback on the submission process (with $mail->SMTPDebug = 2;), and it's also faster than using mail().
You can use an older version. I used Version: 5.2.6 and it works.
I'm using the SwiftMailer class to send mail using the php mail() function or SMTP depending on my app's configuration (development or production). My code looks like this :
// Default mailer: php mail() function
$this->transport = \Swift_MailTransport::newInstance();
// If a SMTP host is defined
if (isset($_SITE['site_smtp_host'])) {
$this->transport = \Swift_SmtpTransport::newInstance($_SITE["site_smtp_host"], 587)
->setUsername($_SITE["site_smtp_user"])
->setPassword($_SITE["site_smtp_pass"]);
}
Since SwiftMailer 5.4.5 I'm getting this deprecation notice :
Exception: UNKNOWN ERROR (16384): The Swift_Transport_MailTransport
class is deprecated since version 5.4.5 and will be removed in 6.0.
Use the Sendmail or SMTP transport instead.
Should I use Swift_SendmailTransport as I was using Swift_MailTransport ? Will it work it the same environnements ? Does it also use the php mail() functions ? If not, is it not possible to use the php mail() function with SwiftMailer anymore ?
First of all, about deprecation from swift mailer site:
It is advised that users do not use this transport if at all possible
since a number of plugin features cannot be used in conjunction with
this transport due to the internal interface in PHP itself.
The level of error reporting with this transport is incredibly weak,
again due to limitations of PHP's internal mail() function. You'll get
an all-or-nothing result from sending.
If you need 100% compatible solution, you need to check php.ini settings and OS platform http://php.net/manual/en/mail.configuration.php
For unix platform will be enough to call ->setCommand with ini_get("sendmail_path") value. For windows platform support need to check smtp option.
This is how it’s done. Note that the Sendmail path is not the same on all servers.
// Get the Sendmail path
$sendmailPath = ini_get('sendmail_path');
$sendmailPath = ($sendmailPath === false || $sendmailPath === '') ? '/usr/sbin/sendmail -bs' : sendmailPath;
// Create the transport method
$transport = new \Swift_SendmailTransport($sendmailPath);
$mailer = \Swift_Mailer::newInstance($transport); // 5.6 or...
$mailer = new \Swift_Mailer($transport); // ...6.2
// Now compose and send your email
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.