PHPMailer Send() function is stopping a script after executing - php

I am using PHPMailer to send a user the results of a form using the following steps:
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "user#example.com";
$mail->Password = "passwd";
$mail->SetFrom("user#example.com");
$mail->Subject = "Test";
$mail->Body = mailMaker($data);
$mail->IsHTML(true);
$mail->AddAddress("otheruser#example.com");
$mail->Send();
This is a part of a larger .php document which is called by ajax and returns the form data back to the page.
When the $mail->Send() function is called the email is sent and everything works fine, but the script ends and I don't get the data back during ajax. I know it is this line because if I comment it out everything works (except the mail isn't sent obviously).
I tried moving the mail methods to another script but I kept getting errors, and was unable to even get a var_dump() or anything returned from the ajax. Is there a logical reason Send() is stopping the script in this case?

Related

SpamAssassin flags our PHP Emailer for being a PHP_SCRIPT (-2.499)

I've got a custom PHP website that has a form which triggers an email to be sent and in this email it contains an authentication link to confirm/verify that they are the person submitting to the form.
The current PHPMailer code looks like:
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
//$mail->isSMTP();
$mail->Host = 'localhost';
//$mail->SMTPAuth = false;
//$mail->Username = '';
//$mail->Password = '';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->XMailer = 'WebsiteName Mailer';
$mail->addCustomHeader('X-Mailer-Type', 'WebsiteName/Auto/Verify');
$mail->setFrom('auth#websitename.com', 'WebsiteName');
$mail->addAddress('test-xxxxxxx#srv1.mail-tester.com');
//Content
$mail->isHTML(true);
$mail->Subject = 'Form Authentication from WebsiteName';
$mail->AltBody = 'Example email body here in plain text';
$mail->Body = "<!DOCTYPE html><html lang='en'><body>Example html email here <br></body></html>";
$mail->send();
I've also tried changing the port to 587 and changing the SMTPSecure to ENCRYPTION_STARTTLS
Every email that is sent to a server with SpamAssassin gets -2.499 score because of the PHP_SCRIPT flag which has the description of Sent by PHP script
I'm trying to find a fix my PHP (PhpMailer) to get around this as my emails are legitimate and wondering what I can do to better make them be detected fairly.
The interesting thing is emails sent with WP SMTP Mail plugin via WordPress do not get flagged for this SpamAssassin rule even though WP SMTP Mail also uses PhpMailer
https://github.com/awesomemotive/WP-Mail-SMTP/blob/master/src/Providers/MailerAbstract.php#:~:text=public%20function%20process_phpmailer(%20%24phpmailer%20)%20%7B

phpmailer does not respect encryption "none"

I am trying to send an email using phpmailer. I have set the encryption to "none" to send using plaintext, however, it starts TLS. I am able to telnet manually to the server and successfully send the email in plaintext, meaning that the server supports plaintext and is not enforcing TLS. Here are the settings and traffic capture:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = MAILSRV;
$mail->Port = "25";
$mail->SMTPSecure = "none";
$mail->SMTPAuth = "1";
$mail->Username = MAILUSER;
$mail->Password = MAILPASS;
$mail->IsHTML(true);
$mail->setFrom(MAILFROM, "Test");
$mail->CharSet = "utf-8";
$mail->Encoding = "base64";
$mail->addAddress(MAILTO);
$mail->Subject = SUBJECT;
$mail->msgHTML(BODY);
Server sends RST after client hello. How can I configure phpmailer to send the email with no encryption?
Don't make up random stuff and expect it to work; pay attention to the correct types and values of PHPMailer properties, ideally by reading the API documentation.
$mail->Port = "25";
Port is an integer, not a string.
$mail->SMTPSecure = "none";
If you're trying to turn it off, set it to an empty string (as the source code says), not some random value, though it is off by default so you don't have to do this anyway.
$mail->SMTPAuth = "1";
This is a boolean value, and it already defaults to false.
$mail->Encoding = "base64";
This is generally a bad idea, and makes your messages more likely to be classified as spam.
But to get to the point of your question, PHPMailer uses opportunistic encryption, meaning that if your server advertises it, PHPMailer will automatically attempt to use it. This can fail in some circumstances, usually when the target mail server is misconfigured in some way, but the thing you are looking for is:
$mail->SMTPAutoTLS = false;
and the documentation on it is here.

PHPmailer issue - php coding above html but not receiving emails

I'm trying to use phpmailer to work alongside a contact form I have made. I was originally using simple php coding for the form, but this would not work on the web company's server, so they asked me to use phpmailer instead. I have changed the coding a little bit to try and integrate it into the contact page that has the contact form.
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.live.com'; // Specify main and backup SMTP servers
$mail->Username = "myemail#hotmail.com";
$mail->Password = "mypassword";
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->addAddress('stacey_victoria#hotmail.com', 'Sender'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Message from Website';
$mail->Body = $message; $name;
$mail->From = $email;
if($_POST){
$feedback = 'Thanks for your message';
}
?>
The contact page appears as normal, and once the submit button has been pressed, the echo-feedback message is shown. However, no email is sent through.
I edited the "Simple Example" found here: https://github.com/PHPMailer/PHPMailer and uploaded the php file to the server and this worked fine so I know the smtp information I have used is correct and working.
Is it even possible to put phpmailer coding above the html coding?
If so, what am I doing wrong?
You are not sending the email...
Use :
if($mail->Send())

phpMailer: Could not instantiate mail function

I created a form that uses phpMailer to email the results to the website owner. Of course, before I add the owner's email address I use mine to test that the form works. When I use my email the form sends perfectly, however, when I use the owner's address it throws the error "could not instantiate mail function" and won't send the form. The error only occurs with email addresses associated with the owner's domain. Any idea why this could be happening?
If I type this into the command line it works fine:
echo 'test' | mail -s 'test' me#example.com
edit: I wasn't initially using SMTP, but it's now configured as shown below. The error message is now "SMTP Error: The following recipients have failed xxx#somedomain.com" and the end result is still the same. It can e-mail to a number of gmail test addresses but has issue with the owner's email#hisdomain.com. Further, with SMTPDebug it's now producing the error "RCPT TO command failed: 550 No Such User Here" The owner's e-mail, however, works without issue when e-mailed through gmail, outlook, etc.
phpMailer code:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->Debugoutput = "error_log";
$mail->Host = "mail.mydomain.com";
$mail->SMTPAuth = true;
$mail->Username = "admin#mydomain.com";
$mail->Password = "XXXXXXXXXXXXXXX";
$mail->CharSet = 'UTF-8';
$mail->AddReplyTo($emailAddress);
$mail->SetFrom("admin#mydomain.com");
$mail->AddAddress($toAddress,$toName);
$mail->Subject = $emailSubject;
$mail->isHTML(true);
$mail->Body = $emailBody;
$mail->AltBody = strip_tags($emailBody);
// Attempt to send the e-mail
if (!$mail->send()) {
//error handling
}
There are couple of things you should try and check with this particular error message:
Make sure you can use regular php mail() function. Create a blank page and use the php mail() to send a test email. If that works, maybe its your SMTP that's having issues with the particular user domain. Setup gmail SMTP or a different SMTP to send emails:
$mail->IsSMTP();
$mail->Host = "smtp.domain.com";
// optional
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
Can you share your phpMailer source for us to view?
Set $mail->SMTPDebug = 2; so you can see what the server has to say, and read the troubleshooting guide.
You're using authentication without encryption, which is not a good combination and many servers won't allow that. Add this:
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
You've based your code on an old example, so you're probably using an old version of PHPMailer too; get the latest from github.

Yii, PHPMailer Cannot be loaded

im trying for days many ways to send email from a local server using gmail.. i once made it with a yii extension but after a while, its not working, i tried to add the whole PHPMailer and make the mailing with the proper way.
this is my code:
static function gmail($email)
{
$mail = new phpmailer();
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Username = "mail#gmail.com";
$mail->Password = "password"; //best to keep this in your config file
$mail->Subject = 'subject';
$mail->Body = 'message';
$mail->addAddress($email);
$mail->send();
}
i downloaded the PHPMailer library from Github and extracted all to components folder.
The way how i added to config file is like this:
'import'=>array(
'application.models.*',
'application.components.*',
'application.components.PHPMailer.*'
),
on the first try i get this error include(phpmailer.php): failed to open stream: No such file or directory
Update: i forgot to say, the way how i access this function is this way Mailer::gmail('$this->email'); where Mailer.php has the gmail function.
Double check your php.ini file to make sure phpmailer is located in the include_path because other than that based on the code you provided everything looks good.
(Sorry, would have put this in the comments but I am still unable to comment just yet.)
I'm not quite sure, I am using Yiis phpmailer extension for several month and it always worked very well.
However, try this with the Github thing:
require_once <PATH TO PHPMAILER> . 'class.phpmailer.php';
$mail = new PHPMailer();
[...] Rest of your code here
Replace with the absulute path to your class.phpmailer.php
i was able to send the mail by checking on gmail history security, it was showing that someone was trying to access my account with invalid authorization, i ended up by using YiiMailer, its a bit confusing about gmail.
the code was like this:
$mail = new YiiMailer();
//$mail->clearLayout();//if layout is already set in config
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "account#gmail.com";
$mail->Password = "password";
$mail->setFrom('no-reply#strandedgrounds.sr', 'Stranded Grounds');
$mail->setSubject('Recuperacion de contraseƱa');
$mail->AddAddress("mail#hotmail.com");
$mail->send();

Categories