PHPMailer $mail->send() showing results on page - php

I have PHPMailer working through Gmail. I've sent emails to myself to test that it's working, and it is. The user submits a signup form (very basic setup) on index.php, which then triggers sending the email.
My problem is that after submitting the form, it "echoes" to the page the process that it's going through: to give a sense of it, here's the start (3000 characters so I'm only including a bit):
2017-03-01 21:25:36 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ( ) 2017-03-01 21:25:36 Connection: opened 2017-03-01 21:25:36 SERVER -> CLIENT: 220 smtp.gmail.com ...
This shows up directly on the page. I'm sure I could get around it by redirecting to another page on success, but it seems much easier to simply not print all of the information to the page in the first place. I've tested my code and it's definitely the $mail->send(); command that's triggering it.
Here's the code I'm using to call PHPMailer (it's inside <?php and ?> tags.
require_once "phpmailer/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "myemailaddress#gmail.com";
$mail->Password = "mypassword";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->From = "myemailaddress#gmail.com";
$mail->FromName = "my name";
[![enter image description here][1]][1]
$mail->addAddress("towhoever#gmail.com", "their name");
$mail->isHTML(true);
$mail->Subject = "Testing email for phpmailer";
$mail->Body = "<i>Mail body in HTML</i>. It worked!";
$mail->AltBody = "This is the plain text version of the email content. Also, it worked!";
if($mail->send())
{
echo "Message has been sent successfully";
}
else
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
Misc. system information if it helps: I'm using XAMPP on Windows 10, running in localhost. I'm connected to my Gmail account.
Here's what the problem looks like on the page (I didn't screenshot much as I'm unsure how much is sensitive information):

PhpMailer have option to debug.
$mail->SMTPDebug // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
Comment $mail->SMTPDebug = 3; in the code.

$mail->SMTPDebug = 0; //after all functions work proper set to 0;

Related

Sending e-mail with attachment to gmail never gets received, using phpmailer

Something weird is going on when i'm trying to send an e-mail with attachment to gmail, using phpmailer.
Sometimes the email received after a delay of 5 minutes and sometimes it is never received. From phpmailer debugger i get that the email is actually sended, so i assume it has something to do with the attachment getting blocked by gmail.
The attachment is a zip file containing only .jpg and .pdf files.
If i remove the addAttachment('path/to/file') everything works fine.
The weird thing is that sometimes, even with the attachment, the email received immediately and other times the email never gets received.
Here is the code
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'my_host';
$mail->Port = 'my_port';
$mail->SMTPAuth = true;
$mail->Username = 'my_username#domain.com';
$mail->Password = 'my_password';
$mail->setFrom('my_username#domain.com', 'my_username#domain.com');
$mail->addAddress('test#gmail.com', 'test#gmail.com');
$mail->Subject = $subject;
$mail->msgHTML($msg);
$mail->AltBody = $subject;
$mail->addAttachment($zipfilename);
if($mail->send()){
echo "ok";
}else{
echo "error";
}
I don't have any clue why this is happening so any help will be appreciated.
EDIT:
I found out that with two changes everything works fine.
The changes are the setting
$mail->SMTPSecure = 'tls'
and i had a path like this ./path/to/file and changed it to 'path/to/file'.

PHPMailer not working in web page

I'm trying to send an email with PHPMailer, but it's not working for me so far.
On my FTP I've put 2 files, the class.phpmailer.php and sendEMail.php (this file is created by me), with this content:
<?php
require_once('/var/www/vhosts/MYWEBPAGE/httpdocs/class.phpmailer.php');
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = "mail.dom.com";
$mail->Username = "smpt#mail.com";
$mail->Password = "passwd";
$mail->Port = 25;
$mail->setFrom("my#mail.com", "me", 0);
$mail->addAddress("to#mail.com");
$mail->Subject = "test";
$body = "Hello World!";
$mail->Body = $body;
if(!$mail->send()) {
echo ("Invoice could not be send. Mailer Error: " . $mail->ErrorInfo);
} else {
echo "Invoice sent!";
}
?>
I'm missing something? When I execute this file, it shows me nothing, i mean before the if(!$mail->send()) {... It shows me every echo, but after that line, it shows me nothing.
It's because you've not read the readme that tells you what files you need, nor based your code on the examples provided. You've not loaded the SMTP class, nor the autoloader that will load it for you, so as soon as you try to send, it will fail to find the SMTP class. This will be logged in your web server logs like any other PHP fatal error.
Instead of this:
require_once('/var/www/vhosts/MYWEBPAGE/httpdocs/class.phpmailer.php');
do this:
require '/var/www/vhosts/MYWEBPAGE/httpdocs/PHPMailerAutoload.php';

phpmailer sending but not receiving

Just finished setting up PHPMailer to send the PDF that is created from my html form (using FPDF, the pdf file is created without a problem). It says sent successfully but im not receiving anything?
I have checked other peoples code and it looks just like mine. Is there anything im doing wrong with the PHPmailer code at the bottom?
my host,username and password are all correct 100% as far as I know we dont use TLS or SSL. Maybe that has something to do with this?
My code:
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 4;
$mail->Host ="*****";
$Mail->SMTPAuth = true; // enable SMTP authentication;
$mail->Username = "*****";
$mail->Password = "****";
$mail->Port = 587;
$mail->SMTPSecure = "tls";
$mail->From = "******";
$mail->FromName = "Jurgen Hof";
$mail->addAddress("testingaccount23#gmail.com", "Tester");
$mail->isHTML(true);
$mail->Subject = 'Test Leave Application';
$mail->Body = 'Test.';
$mail->AddAttachment("/var/www/html/leaveform/AlpineLeaveApplication.pdf");
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Email Sent Successfully!';
?>
You're setting lots of SMTP properties, but not actually asking it to send via SMTP! Add this:
$mail->isSMTP();
Your browser will then explode with debug output, so I suggest you turn down SMTPDebug = 2.

Why does phpmailer take 15+ seconds to load?

I recently setup phpmailer as my SMTP sending script to use for a website I've been working on and it's been nothing but a headache. My setup is IIS, and I'm currently using office365 to send out emails(although I have tried gmail and the results were no faster).I would appreciate any and all advice on how to speed this up, or if there's a way to post to a php file from javascript or php and then just let it work I could do that too. I'm trying to stay away from cron jobs if possible.
my script:
function sendMail($code, $email) {
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "smtp.office365.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "support#[domain redacted].com";
//Password to use for SMTP authentication
$mail->Password = "[password redacted]";
//enable TLS
$mail->SMTPSecure = 'tls';
//Set who the message is to be sent from
$mail->setFrom('support#[domain redacted].com', '[redacted]');
//Set an alternative reply-to address
$mail->addReplyTo('support#[domain redacted].com', '[redacted]');
//Set who the message is to be sent to
$mail->addAddress($email);
//Set the subject line
$mail->Subject = 'This is a test email from EdTester Support';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->Body = "Hello there, This is your activation e-mail. Please go to: http://[domain redacted]/activate.php?code=". $code . "&email=" . $email;
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}

PHPMailer - Certain SMTP messages don't send

I have a script that sends emails to users, which are sometimes divided into multiple messages depending on size. Here's the code:
for($i=0; $i<count($the_message); $i++){
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->Host = "my.smtp.server";
$mail->Port = $email_port;
$mail->Username = "my#email.com";
$mail->Password = "mypassword";
$mail->SMTPAuth = true;
$mail->From = $from;
$mail->FromName = "My Name";
$mail->IsHTML($is_html);
if($is_html === TRUE){
$mail->AltBody = "To view the message, please use an HTML compatible email client, or change your export to a plain text format.";
}
foreach($to as $address){
$mail->AddAddress($address);
}
$mail->Body = $the_message[$i];
$mail->Subject = $the_sub;
$mail->Send();
}
This used to work great. Now, it seems that certain messages will never send. It seems to be a data issue (or perhaps the way PHPMailer sends lines to the server?), but I can't pinpoint why certain message segments are always rejected by the server.
The error message I get from PHPMailer is this:
SMTP Error: Data not accepted.
And from PHP itself:
Notice: fputs(): send of 31 bytes failed with errno=104 Connection
reset by peer in class.smtp.php on line 580
Here is a segment of HTML data that always fails to send:
http://pastebin.com/LGYkTTFA
Note that size isn't an issue, I can successfully send much larger HTML messages than this. However, when I send multiple segments, this one is never accepted by the server.
Here, I rewrote it for you in a somewhat saner way. You don't need to create everything from scratch every time around the loop, just change the body and send again.
<?php
require 'PHPMailerAutoload.php';
$the_message = array('Hello', 'there', 'Justin');
$to = array('tom#example.com', 'dick#example.net', 'harry#example.org');
$the_sub = 'Crazy subject';
$email_port = 25;
$from = 'justin#example.com';
$is_html = true;
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->Host = "localhost";
$mail->Port = $email_port;
$mail->Username = "my#email.com";
$mail->Password = "mypassword";
$mail->SMTPAuth = true;
$mail->From = $from;
$mail->FromName = "My Name";
$mail->Subject = $the_sub;
$mail->IsHTML($is_html);
$mail->WordWrap = 200;
if($is_html){
$mail->AltBody = "To view the message, please use an HTML compatible email client, '.
'or change your export to a plain text format.";
}
foreach($to as $address){
$mail->AddAddress($address);
}
for($i=0; $i<count($the_message); $i++){
$mail->Body = $the_message[$i];
$mail->setWordWrap();
$mail->Send();
}
Update: added word wrapping to deal with content with very long lines.
In class.smtp.php, line 51, I changed the constant MAX_LINE_LENGTH from 998 to 500. This allowed all of my email segments to pass through.
I'm still not sure if the issue lies with my SMTP server or PHPMailer, I'll have to test it elsewhere.
EDIT: Tested using Gmail's SMTP server, and keeping 998 worked fine. This leads me to assume that my SMTP relay server is rejecting data sometimes depending on the length.
EDIT2: Still experienced failures with the above fix. I changed my port number and added SSL, and it is magically working. Wonky server issues...

Categories