phpmailer wont send email after submitting form - php

Im messing around with phpmailer and i've got everything working.
But what im trying to do now is after submitting a form send a mail.
Not anything too difficult just a basic email that acknowledges the form has been submitted (no form data).
problem : email is not sending after submitting form (email code is working 100% tested)
Hope someone can help me out :)
mail.php code :
<?php
//ini_set(‘display_errors’, 1);
include '/var/www/includes/mailer.php';
//require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.nicetrygoyim.nl'; //Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'secret#secret.com'; // SMTP username
$mail->Password = 'nicetry'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('secret#secret.com', 'Mailer');
$mail->addAddress('secret#secret.com', 'secret'); // Add a recipient
$mail->addAddress('secret#secret.com', 'secret'); // Name is optional
//$mail->addReplyTo('secret#secret.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->smtpConnect([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
my Form :
<form action="mail.php" method="post">
Leerlingnummer:<br>
<input type="text" name="leerlingnummer"required placeholder="Voer hier het leerlingnummer in" /><br>
E-mailadres:<br>
<input type="submit" name="submit" class="groottext" value="Reparatie indienen"/>
edit : typo
edit : forgot to mention problem

If this code is running, you should be seeing a ton of debug output, even if it is working correctly. You don't actually say what the problem is, but you're doing a few things wrong that I can see. It would really help if you based your code on the examples provided and read the docs instead of just guessing.
$mail->SMTPSecure = 'TLS';
should be:
$mail->SMTPSecure = 'tls';
Don't call smtpConnect() yourself, you'll mess up the tracking of SMTP transaction state. If you want to set SSL params, set them the expected way and then just call send(), which will deal with the connection:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
The next question is why are you doing that? If you can't provide an explicit, specific reason for doing that, you're doing something wrong.

Related

Mailer Error Mail() without being connected

<?php require 'PHPMailer_5.2.4/class.phpmailer.php';
$mail = new PHPMailer;
$mail->SMTPDebug=0;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'gmail-smtp-in.l.google.com';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'demotp18#gmail.com'; // SMTP username
$mail->Password = '****'; // * is just for not to show password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->Port = 587;
$mail->setFrom('neelsoni.sn#gmail.com');
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
) );
$mail->addAddress('demotp18#gmail.com'); // Name is optional
$mail->WordWrap = 50;
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
output : Message could not be sent.Mailer Error: The following From address failed: neelsoni.sn#gmail.com : Called Mail() without being connected
my question is about PHPMAILER
What is the problem in the code ??
why i get this error ? i turn on less secure in gmail.
And in HOST i tried smtp.gmail.com but there is same error i seen, thats why i found this host type.
same code in my one friend's laptop it's working but another friend is facing this same error.
please help me !!!!!!!!!!

peer certificate did not match "smtp.office354.com" using phpmailer

Good Day,
I was having an error on my phpmailer smtp configuration to smtp.office365.com
here my script
require __DIR__ .'/vendor/phpmailer/phpmailer/src/Exception.php';
require __DIR__ .'/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require __DIR__ .'/vendor/phpmailer/phpmailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = gethostbyname('smtp.office365.com'); // Specify main and backup SMTP servers
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'office365 username'; // SMTP username
$mail->Password = 'office365 password'; // SMTP password
$mail->SMTPOptions = array (
'ssl' => array(
// 'verify_peer' => false,
// 'verify_peer_name' => false,
// 'allow_self_signed' => true
));
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->From = $mail->Username;
$mail->addAddress('recipient#gmail.com'); // Name is optional
//Attachments
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
I got this error
stream_socket_enable_crypto(): Peer certificate CN=`servername.com' did not match expected CN=`smtp.office365.com'
if I uncomment
$mail->SMTPOptions = array (
'ssl' => array(
// 'verify_peer' => false,
'peer_name' => 'smtp.office365.com',
// 'verify_peer_name' => false,
// 'allow_self_signed' => true
));
I still get authentication failure..
Im not really good with server configuration as someone is doing the server set up. but my site is on https.
Think about what this means. You’re asking to connect to a named host, but the name on the certificate does not match. That means that either the server is misconfigured (unlikely for office365), or you are being redirected to a different server that’s using a different name. This is extremely likely, as it’s very common in large hosting providers. All will be revealed if you set SMTPDebug = 2, as the troubleshooting guide the error message links to suggests.
That this has happened is a good thing - it’s one of the main reasons for using TLS - it not only encrypts your traffic in transit, but provided assurance that the server you connected to is the one you expected, i.e. it is doing its job properly.

SMTP Error: Could not connect to SMTP host. i tried all result found on stackoverflow and githubs

im new in php im trying to use phpmailer to send email using gmail
here is the code i wrote
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'sendermail#gmail.com';
$mail->Password = '<some password>';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('anadresse#gmail.com', 'GestionStock');
$mail->addAddress('anadresse#gmail.com', 'hamza');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} ?>
i got as results SMTP Error: Could not connect to SMTP host.
SMTP Error: Could not connect to SMTP host.
Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host.
can someone tell me what im doing wrong !!
The same problem I had using Gmail and I solved it by downloading the cacert.pem certificate from the site https://curl.haxx.se/docs/caextract.html You should also write the php.ini file as follows: extension = php_openssl.dll openssl.cafile = C: \ xampp \ php \ extras \ ssl \ cacert.pem
It must be activated in the GMail account in the label: Access and security of the option:
Allow less secure apps access option: ON
This solution is thanks to the matteobin user contribution of stackoverflow
i figured out that the problem not from code , its from the account gmail , idk exactly what is but i have different accounts , some works and other not working , i tried creating new gmail account and enabling lesssecureapps but not worked
it works only in a specific gmail with a disabled lesssecureapps
The following code has worked for me.
require('./phpmailer/PHPMailerAutoload.php');
require('./phpmailer/class.phpmailer.php');
$mail=new PHPMailer();
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->CharSet = "utf-8";
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->SMTPAuth=true;
$mail->isSMTP();
$mail->Username="myemail#gmail.com";
$mail->Password="mypassword";
$mail->setFrom('myemail#gmail.com','Some text here');
$mail->addAddress($email);
$mail->addReplyTo('myemail#gmail.com');
$mail->isHTML(true);
$mail->Subject="Confirmation email";
$mail->Body="<h2 style='text-align='center';'>Confirmation email</h2></br>
<p>Your message has been received. One of our team members will contact you shortly.</br></br>Thank you for contacting us.</p>";
if(!$mail->send())
{
echo "Message could not be sent!";
echo $mail->ErrorInfo;
}
else
{
echo "Message sent successfully!";
}
I was having the same problem. I added the following code:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
It is because, in the newer version, PHP has implemented stricter SSL behaviour which has caused this problem. Hope this solves your problem as well.

PHP Mailer to outlook: Data not accepted Recipient address reserved by RFC 2606

When i try to send an email to my outlook account I receive an error: SMTP Error: data not accepted. Message could not be sent.Mailer Error: SMTP Error: data not accepted.SMTP server error: DATA END command failed Detail: 501 5.1.5 Recipient address reserved by RFC 2606 SMTP code: 550 Additional SMTP info: 5.3.4. I tried with my Gmail account and it works but not with outlook live. Anyone can help me?
Here's the code:
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP();
$mail->Host = 'smtp-mail.outlook.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'andreacivitas#hotmail.it'; // SMTP username
$mail->Password = '***********'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption, `ssl` also accepted
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('andreacivitas#hotmail.it');
$mail->addAddress('andreacivitas#hotmail.it', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
RFC2606 defines some domains that are designated as being for example use only, and are guaranteed never to exist. in particular these include example.com, example.org and example.net. This means that you can use reasonable-looking addressees in example code without fear of inadvertently sending email or other traffic to random people, which might happen if you use a made-up name like mydomain.com because it could actually exist.
The error you're seeing has recognised that you're using a reserved domain like this and so is refusing to accept your submission. Use real addresses, or remove the lines that use the reserved addresses, and it will work.
You've set the SMTPSecure option incorrectly - it is case-sensitive, so it should be:
$mail->SMTPSecure = 'tls';
You're disabling certificate verification via SMTPOptions - Outlook/Hotmail etc usually serve verifiable certificates, so you should only disable verification to solve a specific problem as it is not a safe way to avoid verification issues, i.e. don't do it unless you really need to for a known, specific reason.

phpmailer and php5.6 not working with office365

I have a site hosted with rackspace and upgraded my site technology from php5.4 to php5.6. Now I can no longer send emails from the site. The worst part is I'm not even getting any errors and the logs show nothing. I am using phpmailer 5.2.14.
Here is my mail script
require 'phpmailer/PHPMailerAutoload.php';
if (isset($_POST['contactForm'])) {
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$body = '
<html>
<body>
<div style="float:left; width:100%; margin:0 0 25px 0; padding:20px; background:#222; text-align:center;">
<div style="display:inline-block; vertical-align:top;">
<img src="img/logoEmail.png" alt="waesf">
</div>
</div>
<main style="float:left; width:100%; padding:20px;">
<p style="font-family:Arial; font-size:18px;">'.$message.'</p>
</main>
</body>
</html>';
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.office365.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'person#email.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->SetFrom('person#email.com', 'Ballpoint Machinist');
$mail->AddAddress('person#email.com', "BPM"); // Add a recipient
$mail->addReplyTo('person#email.com', 'Information');
// $mail->addCC('cc#example.com');
// $mail->addBCC('bcc#example.com');
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = $body;
if (!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
When the script runs the site then hangs for a while then gives a says server timed out and nothing else. No php error codes nothing and when I check the logs there are no errors related to my mail code. However this only happens with office 365 when I change the smtp settings to gmail then I at least get php errors on the page.
I have already read a bunch of threads on the subject but I fail to understand. I have also gone through https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting and added the SMTPOptions to exclude ssl but this didn't help.
Im perplexed that I dont get any errors. I have SMTPDebug = 3 and error_reporting(E_ALL).
edit with Willy Pt suggestion. Still doesn't work when script runs server times out.
if (isset($_POST['contactForm'])) {
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$body = '
<html>
<body>
<div style="float:left; width:100%; margin:0 0 25px 0; padding:20px; background:#222; text-align:center;">
<div style="display:inline-block; vertical-align:top;">
<img src="img/logoEmail.png" alt="Ballpoint Machinist">
</div>
</div>
<main style="float:left; width:100%; padding:20px;">
<p style="font-family:Arial; font-size:18px;">'.$message.'</p>
</main>
</body>
</html>';
$mail = new PHPMailer(true);
// $mail->SMTPDebug = 4; // Enable verbose debug output
try {
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.office365.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'person#person.com'; // SMTP username
$mail->Password = 'test'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->SetFrom('person#person.com', 'Ballpoint Machinist');
$mail->AddAddress('person#person.com', "BPM"); // Add a recipient
$mail->addReplyTo('person#person.com', 'Information');
// $mail->addCC('cc#example.com');
// $mail->addBCC('bcc#example.com');
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = $body;
$mail->send();
echo "Message Sent OK\n";
/*if (!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}*/
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
}
}
Can anyone help?
Don't know if this counts as an answer but I was able to get this working with gmail. exact same code just use gmail credentials and smtp host instead of office365. I did have to set gmail to allow for less secure apps.
The problem is with PHP5.6 and self signed certificate verification.
With PHP5.6, certificate verification is enabled by default and certificate can not be self signed.
The correct fix for this is to replace the invalid, misconfigured or self-signed certificate with a good one.
Or, configure it to use a non-self signed certificate:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
You can also change these settings globally in your php.ini, but that's a really bad idea; PHP 5.6 made this change for very good reasons.
Sometimes this behavior is not quite so apparent; sometimes encryption failures may appear as the client issuing a QUIT immediately after trying to do a STARTTLS. If you see that happen, you should check the state of your certificates or verification settings.

Categories