PHPMailer and function escapeshellcmd() - php

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.

Related

PHP Mail () function disabled in SMTP impact?

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";

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

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

mail() timeout issue

When I execute my email script via browser a timeout fatal error is returned (unless I drastically increase the execution time, then it will run ok, not the solution I'm looking for). The email is sent tho, but it takes forever (5 min. average) to arrive (at my inbox)!
(Considering that via command line it works perfectly I think that SMTP at php.ini is certainly well configured.)
So this is the code executed by browser request:
<?php
mail('amatos#example.com', 'test subject', 'test body', 'From: Andre Matos <amatos#example.com>');
?>
and when I run this same (is it really the same? I'm starting to doubt myself) code via command line:
php -r "mail('amatos#example.com', 'test subject', 'test body', 'From: Andre Matos <amatos#example.com>');"
it works perfectly! The script runs, it stop and the email arrives instantly (2/3 seconds).
So, what can cause this difference and how to fix it? Any ideas?
Thanks in advance.
[edit] some extra info:
- the machine is windows
- the server is localhost
- php.ini is the same for both the browser and the cli instance
[edit2]
Thank you all for trying to guess which was the problem. I placed the question hopping that someone had the problem before and knew of something specific. Given nothing specific showed up and none of the suggestions really worked, I've decided to accept the one that allowed me to reach more conclusions about the problem... +1 For all your helpful knowledge/thoughts (/guesses) :-)
I've hypothesised some couses, but I used to linux and on windows I can olny guess:
php_cli and mod_php are 2 different binaries, mod_php can be slightly damaged
php_cli and mod_php use 2 different users, the network profile of apache user can be the problem (dns, firewall, proxy...)
your php script is on "problematic" location or contains some problematic character, but your cli script is by param, try to execute same script: php -f z:\path\to\php\mail.php
Given this note from http://php.net/manual/en/function.mail.php, it seems very likely that the issue is with the MTA and not PHP directly:
The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).
Perhaps it has something to do with the way the MTA is responding to the particular user, or user-specific firewall rules for outgoing mail connections on your machine. Can you run the command-line as the web server user rather than yourself? If so, does that re-create the problem from the command-line?
How about having the web server execute the command-line PHP rather than the parsed PHP file? (For example, perhaps you can run a batch script via CGI.) Does that solve the problem?
(Sorry that these are more guesses than definite answers.)
Just to be clear: The php-instance used by the script is the same as the one used by the command line code?
Many web hosts use smtp-relay, which will gather a bunch of emails and send them all at once, so it won't be strange behaviour if your mail is late. However, the long execution time is not normal.
Before you send your mail in the script make sure to print out the ini_get() of the variables SMTP, smtp_port, and sendmail_from and be certain that these are working values. PHP running on Windows does not have the benefit of sending mail out via Sendmail, and whatever the PHP devs cobbled together for it is sketchy at best.
I always like to test mail via telnet so I can see if the server is giving an error that is not being passed back properly by the client:
c:\> telnet smtp.domain.com 25
220 smtp.domain.com ESMTP Postfix
helo mailtest
250 smtp.domain.com
mail from: user#local.com
250 2.1.0 Ok
rcpt to: user#remote.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
from: user#local.com
to: user#remote.com
subject: test mail
this is a test message
.
250 2.0.0 Ok: queued as 42AD8364FE0E
quit
221 2.0.0 Bye
Try to set username and password to "From" mail id. so it can authenticate and sends the mail quickly.
Have you tried PHP mailer?
In my observation its sends mails within seconds. Below example will give you a quick look how to use php mailer class.
include "class.phpmailer.php";
$msg="Hello! This is a test..."
$mail=new PHPMailer();
$email="someone#friend.com"; //person who receives your mail
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->Username = "admin#example.com"; //your mail id
$mail->Password = "sdfsd441"; //password for your mail id
$mail->SetFrom('admin#example.com', 'admin'); //from address
$mail->AddAddress($email);
$mail->Subject ="Test Mail";
$mail->Body = $msg;
$mail->IsHTML(true);
$mail->MsgHTML($msg);
$mail->Send();
EDIT :
In PHP manual they stated like this,
The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).
so that may cause delay? I think this link might help you.
My first guess would be that the browser version of your mailing has already a context or connections ready for the sending.
On the contrary, the direct (php -r) execution has to load the mailing context.
To confirm this idea, you can make a loop for sending 10 mails and check wether the mails after the first are much quicker.

PHP Mail Error in Microsoft WebMatrix

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.

How to send an email using Zend_Mail, sendmail, and localhost?

I'm developing a zend framework application that includes a simple email function. The development version is running on my computer, which is running Ubuntu. The production version is going to run on a production server.
When trying to send a test email to myself, I get an exception with the message: "Unable to send mail". I don't know if this is an environment issue, or a code issue. I'm not using a transport so I think it is defaulting to Zend_Mail_Transport_Sendmail. Here's my code:
public function sendtestAction()
{
$mail = new Zend_Mail();
$mail->setFrom('test#aol.com', 'Test Email');
$mail->addTo('my#email.com', 'My Name');
$mail->setSubject('This is just a test.');
$mail->setBodyText('This is only a test.');
$mail->send();
}
Update: I tried a different approach by setting the SMTP transport to use localhost:
transport = new Zend_Mail_Transport_Smtp('localhost');
Zend_Mail::setDefaultTransport($transport);
I got a different error this time: "Connection refused" Not sure what that means. Maybe I haven't set something up yet?
Update: I guess I didn't have an SMTP server installed/setup. This tutorial made it really easy for me to get an SMTP server up an running. Now both of the code samples above work.
It sounds like you need to configure an MTA, or find one that you can send to. Ubuntu desktop should set one up by default, probably either exim or postfix, but if you haven't configured it, it will unlikely to be running.
You don't want to set the default transport if you wish to use sendmail (it is the default) and SMTP is different.
That it doesn't send the emails suggests that sendmail or the MTA on your server is not installed/not setup correctly.

Categories