I'm trying to create a contact form. And I searched all over the internet how to send email, and they always say that it is better to use PHPMailer than mail() function.
The contact form consists of: Name, Email, Comments, and a Send Button.
I just followed this tutorial: https://alexwebdevelop.com/phpmailer-tutorial/ but it's not working as I always got the error called: 'Could not instantiate mail function.' when I already installed the Composer.
Proof that I've already installed the composer:
Composer installed with autoload.php
Here's my code inside the php tags:
use PHPMailer\PHPMailer\PHPMailer;
require 'C:\xampp\composer\vendor\autoload.php';
if(isset($_POST['send'])) {
$em = $_POST['email'];
$nm = $_POST['name'];
$msg = $_POST['comments'];
$mail = new PHPMailer();
$mail->setFrom($em, $nm);
$mail->addAddress('mygmail#gmail.com', 'Admin');
$mail->Subject = 'Concern';
$mail->isHTML(TRUE);
$mail->Body = '$em';
if(!$mail->send()) {
echo $mail->ErrorInfo;
}
}
Thank you for answering this question! It'll be a big help!
UPDATE:
Content/File of the composer.json:
{
"require": {
"phpmailer/phpmailer": "^6.0"
}
}
If you read the PHPMailer troubleshooting guide linked from nearly everywhere (including in your error message), you'll find it contains a detailed explanation for this error and how to solve it. Copying from there:
This means that your PHP installation is not configured to call the mail() function correctly (e.g. sendmail_path is not set correctly in your php.ini), or you have no local mail server installed and configured. To fix this you need to do one or more of these things:
Install a local mail server (e.g. postfix).
Ensure that your sendmail_path points at the sendmail binary (usually /usr/sbin/sendmail) in your php.ini. Note that on Ubuntu/Debian you may have multiple .ini files in /etc/php5/mods-available and possibly other locations.
Use isSendmail() and set the path to the sendmail binary in PHPMailer ($mail->Sendmail = '/usr/sbin/sendmail';).
Use isSMTP() and send directly using SMTP.
This is nothing to do with how you load composer's autoloader.
Some hosting services return this error when the email is considered a spam. Try using an email address with the same domain as your website.
Instead of: require 'C:\xampp\composer\vendor\autoload.php';
Try: require __DIR__ . '/vendor/autoload.php';
This is assuming that /vendor is a sub directory of your document root.
Related
I have a VPS where I've installed my site files, including the PHP email script that uses the php mail() function inside of the following directory (how my site directory/path is set up):
/var/www/mywebsite.com/html/
*DIRECTORY/PATH STRUCTURE SHOWN IN THE IMAGE BELOW:
https://ibb.co/2SDjb8z
And when I installed Postfix, I've configured it to send email through Amazon SES. Postfix has been installed inside of the following directory:
/etc/postfix/
*DIRECTORY/PATH STRUCTURE SHOWN IN THE IMAGE BELOW:
https://ibb.co/XF1JFvv
The problem that I'm having is that it will send email from the command line when testing that Postfix has been properly installed along with using the Amazon SES SMTP, BUT my php email script DOESN'T connect from my websites folder directory to Postfix.
How do I connect my php email script to Postfix? Do I need to change directories?
Here is the php mail() function script that I'm using below:
<?php
$to = "MyTestEmailAddress#gmail.com";
$subject = "Another Test!";
$txt = "Hello world!";
$headers = "From: MyEmailAddress#gmail.com" . "\r\n" .
"CC: AnotherTestEmailAddress.com";
mail($to,$subject,$txt,$headers);
?>
Note that the above php script, is the in the file called “email1.php” inside of my website folder. I’m just trying to connect it to Postfix which is located in the “/etc/postfix” directory.
When you installed Postfix it should have created a sendmail command line program.
Locate it and set the path to it in php.ini for the sendmail_path option.
sendmail_path string Where the sendmail program can be found, usually
/usr/sbin/sendmail or /usr/lib/sendmail. configure does an honest
attempt of locating this one for you and set a default, but if it
fails, you can set it here.
Systems not using sendmail should set this directive to the sendmail
wrapper/replacement their mail system offers, if any. For example, »
Qmail users can normally set it to /var/qmail/bin/sendmail or
/var/qmail/bin/qmail-inject.
First of all, hi and thanks for your time, yesterday I have installed PHP Mailer 6.0.5, by running composer locally then I uploaded the vendor folder that it generated on my server, but when I try to run phpmailer it say: escapeshellcmd() has been disabled for security reasons , here is my code:
require '/.../.../public_html/vendor/autoload.php';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->addAddress($_POST['Email']);
$mail->setFrom('.......');
$mail->Subject=".....";
$mail->Body=".....";
if ($mail->send()) {
.....
}
How can I solve this error, could it depend on my installation?
source https://github.com/PHPMailer/PHPMailer/issues/966
use:
$mail->isSMTP();
that way PHPMailer will send via SMTP to localhost, which does not involve calling escapeshellcmd.
Please forgive my ignorance.
I am trying to install PHPMailer 6.0.1 under PHP 5.6 on Linux.
My PHP installation is remote and I manage all my websites’ PHP via FTP (I typically download packages as .zips to Win 10, unpack and then FTP the result to my webspace).
Of the various ways to install PHPMailer, Composer is preferred, but this is where I come unstuck. None of the Composer instructions seems appropriate to this way of working – the installer wants me to the ‘Choose the command line PHP you want to use’, but I don’t have PHP locally ...
Annoyingly, I see PHPMailer’s composer.json file installed waiting to be used.
But no PHPMailerAutoload.php (is this created by Composer?)
So I try to do a manual install of PHPMailer. I download, unzip and FTP upload the resulting directories to my webspace in folder PHPMailer. I then insert the following at the head of my PHP code and outside of any functions:
require_once 'PHPMailer/src/PHPMailer.php';
require_once 'PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
With the ‘use’ statements I get a syntax error unexpected 'use' (T_USE) …
Without them I get as far as trying to instantiate:
$mail = new PHPMailer;
but this fails with a ‘class 'PHPMailer' not found
What please am I doing wrong and how can I do better?
This isn't specific to PHPMailer - it's what you need to do to use any of the myriad PHP packages that use namespaces. The PHP docs on how to use use are here.
The short version is, you need to put namespace and use directives before any other scripting, so if you simply reverse the order of your commands, it should work:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once 'PHPMailer/src/PHPMailer.php';
require_once 'PHPMailer/src/SMTP.php';
Incidentally, this is the order used in the example in the PHPMailer readme and all the other examples provided with PHPMailer. You may find the upgrade guide useful too.
The PHPMailerAutoload.php file no longer exists - composer's autoloader does a much better job. PHPMailer's own composer.json file is used to resolve dependencies and flag compatibility requirements for your app's own composer file, that is, it's used to tell your project's composer file how to use PHPMailer – but is not your project's composer file itself – every package you load will have its own.
Developing without a local PHP instance is hard work – developing on your live server is, shall we say, "discouraged"! If you can't install PHP directly, run it in a container or VM using Docker, VirtualBox or something like XAMPP that's completely self-contained.
In version 6.02, each of the phpmailer modules contain the namespace PHPMailer\PHPMailer declaration so the following works (no autoloader needed but this routine should be in /src folder):
include($_SERVER['DOCUMENT_ROOT'].'/path_setup.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/php/PHPMailer/src/PHPMailer.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/php/PHPMailer/src/SMTP.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/php/PHPMailer/src/Exception.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
Modify you require, and try set like the wiki of PHPMailer says:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
Link of wiki
Not passable without composer....
Warning: require(src/Exception.php): failed to open stream: No such file or directory in C:\xampp\htdocs\testtest\test.php on line 5
Fatal error: require(): Failed opening required 'src/Exception.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\testtest\test.php on line 5
First create a folder src and create Exception.php,PHPMailer.php,SMTP.php Liber's then we get results
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = "smtp.gmail.com"; // use $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6
$mail->Port = 587; // TLS only
$mail->SMTPSecure = 'tls'; // ssl is depracated
$mail->SMTPAuth = true;
$mail->Username = '#gmail.com';
$mail->Password = '';
$mail->setFrom('#gmail.com', '...');
$mail->addReplyTo('#gmail.com', ' Name');
$mail->addAddress('#gmail.com', '...');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("Hello test SMTP body"); //$mail->msgHTML(file_get_contents('contents.html'), __DIR__); //Read an HTML message body from an external file, convert referenced images to embedded,
$mail->AltBody = 'HTML messaging not supported';
// $mail->addAttachment('images/phpmailer_mini.png'); //Attach an image file
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message sent!";
}
?>*strong text*
Hey I am trying to send an email from a php script. When I try to do so, I get a pop up box that is titled "mailtodisk.exe - No Disk". In the body of the error message, it says, "There is no disk in the drive. Please insert a disk into drive \Device\Harddisk1\DR1".
I have tried to figure this out but to no avail. I am doing this from localhost.
Here is my script that is supposed to send the email:
<?php
$to = $_POST['email1'];
$subject = "Test mail";
$message = "I just sent you an email!";
$from = "ULSRL#louisiana.edu";
$headers = "From:" . $from;
if( mail($to, $subject, $message, $headers) )
{
echo ("Mail Sent.");
}
else
{
echo ("Mail could not be sent!");
}
?>
Any help is greatly appreciated! Thanks :)
I am guessing you are using XAMPP?
If so you will need to modify the php.ini located in your XAMPP installion, look for the following lines:
; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
;sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
; XAMPP: Comment out this if you want to work with mailToDisk, It writes all mails in the C:\xampp\mailoutput folder
sendmail_path = "C:\mailtodisk\mailtodisk.exe"
mail cannot be delivered in PHP even SMTP server is running and PHP mail() returns true
This should also help:
http://blog.joergboesche.de/xampp-sendmail-php-mailversand-fuer-windows-konfigurieren#xampp_180_sendmail
You are trying this solution in Localhost. So you can get this error. Try in live work. You will definitely succeed.
For XAMPP, I have the same message it shows about 4 times providing options
{cancel}, {try again}, {continue}
No matter what I choose it replied 4 times and instead of showing the next page it showed the blank page.
My XAMPP version saves emails to the C:\xampp\mailoutput and this part was working.
What I did was: I commented out the following line is PHP.ini
;sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"
Now it does not save the files in C:\xampp\mailoutput
BUT it does not give me this annoying error and the next page loads fine.
Root cause of the problem is that mailtodisk.exe tries to reach non existing drive on your Windows machine, which cause this annoying message. This can be considered as a bug in mailtodisk actually, so you can simply don't use it as other replies here suggest.
This problem is caused usually because of USB sockets attached to your computer, for example for reading SD cards, which are currently empty.
If you prefer walking on the wild side, you can tweak your registry and suppress this message. See video demonstration here https://www.youtube.com/watch?v=Aj7-pLaAq2c
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.