Output ('',S); breaking code with Mpdf and PHPmailer - output

I am trying to set up sending a pdf attachment with mPDF and PHPMailer.
mPDF is woking fine when output is download to browser, the minute I change it to Output('',S) the code stops working with Error 500 and I have tried different versions of the mailer code with no luck. I cant find any error with the syntax and all online tutorials seem to agree with the code. I am stumped
My code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once __DIR__ . '/vendor/autoload.php';
// Grab variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$message = $_POST['message'];
$mpdf = new \Mpdf\Mpdf();
$data = "";
$data .= "<h1>Testing this</h1>";
//Add Data
$data .='<strong>First Name</strong> '. $fname.'<br>';
$data .='<strong>Last Name</strong> '. $lname.'<br>';
$data .='<strong>Email</strong> '. $email .'<br>';
if($message)
{
$data .='<br/><strong>Message</strong><br/> '. $message.'<br>';
}
//WritePdf
$mpdf->WriteHtml($data);
//output to bowser
$pdf = $mpdf->output('',S);
$enquirydata = [
'First Name' => $fname,
'Last Name' => $lname,
'Email' => $email,
'Message' => $message
];
sendEmail($pdf,$enquirydata);
function sendEmail($pdf, $enquirydata)
{
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'in-v3.mailjet.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('myemails#gmail.com', 'Test Form');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
// 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. Mailer Error: {$mail->ErrorInfo}";
}
}
?>

If you are in a localhost, comment the line with the validation $email->isSMTP();
It works for me in my local.

Related

SMTP ERROR: Failed to connect to server: Network is unreachable (101) SMTP Connect failed

Excuse me, I really need some help for this code :
<?php
require 'PHPMailerAutoload.php';
require 'class.phpmailer.php';
$from = "xxx#gmail.com";
$mail = new PHPMailer();
$mail->SMTPDebug=2;
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com"; // SMTP host
$mail->Port = 587; // set the SMTP port
$mail->Username = "xxx#gmail.com"; // SMTP username
$mail->Password = "xxx"; // SMTP password
$mail->SetFrom($from , 'Admin xxx');
$mail->Subject = "mail title";
$mail->MsgHTML("Halo halo");
$address = "xxx#gmail.com";
$mail->AddAddress($address, $to);
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
And I get this error: 2018-06-08 01:52:09 SMTP ERROR: Failed to connect to server: Network is unreachable (101) 2018-06-08 01:52:09 SMTP connect() failed.
I have tried all of solution about this problem, but nothing happen. please, I need your help. thank you..

phpmail gmail Could not connect to SMTP

Tried using the code below which has been working for years until last week. Some change on Gmail side perhaps. But its just not working even if various combinations are tried.
Allow unsafe apps is ON still unable to connect SMTP
<?php
sendMail("tosomeid#gmail.com", "fromsomeid#gmail.com", "test", "test msg<br>hello!");
function sendMail($to, $from, $subject, $message) {
try {
//$to='tosomeid#gmail.com';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From:' . $from . ' <fromsomeid#gmail.com>' . "\r\n";
ini_set("sendmail_from", "fromsomeid#gmail.com");
require_once("class.phpmailer.php");
require_once("class.smtp.php");
set_time_limit(240);
$mail = new PHPMailer(true);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAutoTLS = false;
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 4;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 587; // or 587 // set the SMTP port for the GMAIL server
$mail->Username = "fromsomeid#gmail.com"; // SMTP account username
$mail->Password = "password123"; // SMTP account password
$mail->From = "fromsomeid#gmail.com";
$mail->SMTPSecure = 'tls';
$mail->AddAddress("tosomeid#gmail.com");
$mail->SetFrom('fromsomeid#gmail.com', $from);
$mail->AddReplyTo("fromsomeid#gmail.com", $from);
$mail->AddBCC("tosomeid#gmail.com");
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->WordWrap = 50;
echo 'sendMail to=>' . $to;
if (!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo '<br>Message was sent successfully to selected recipients.';
}
} catch (Exception $ex) {
echo'EXCEPTION <br>';
echo '<br>Caught exception: ' . $ex->getMessage() . "\n".$ex->getTraceAsString();
}
}
?>
You add SMTPOption to config Phpmailer
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);

phpmailer does not connect to SMTP servers

i've been trying to use phpmailer and tried using live.com and gmail.com but it always can't connect to SMTP server. here's the full code (i've tried live.com using smtp.live.com but i get the same problem "Message could not be sent.Mailer Error: SMTP connect() failed.")
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'my emil address'; // SMTP username
$mail->Password = 'my password'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable encryption, 'ssl' also accepted
$mail->From = 'the same email address';
$mail->FromName = 'Mailer';
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('another email address'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
//$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I suspect it's because you've not set the port, and SMTPSecure should be lower case. Change this:
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
Beyond that, check that you're allowed to send outbound mail by your ISP/firewall and that your DNS is working.
Try This:
$mail->SMTPSecure = 'tls';
$mail->Host = 'tls://smtp.gmail.com';
$mail->Port = 587; //You have to define the Port
$mail->SMTPDebug = 3;
Remove This:
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'TLS';

Problems with sending HTML Email with PDF attachment using PHPMailer

Please help me, i'm trying to send an email with an HTML body and a PDF-file attached. I'm using PHPMailer. I tried a lot but all I get in my mail is this:
--b1_422917e00bd74f108a49b5d3d858e74d Content-Type: text/html; charset = "iso-8859-1" Content-Transfer-Encoding: 8bit Hello World --b1_422917e00bd74f108a49b5d3d858e74d Content-Type: application/pdf; name="factuur.pdf" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="factuur.pdf" JVBERi0xLjQKJcfsj6IKMTEgMCBvYmoKPDwvTGVuZ3RoIDEyIDAgUi9GaWx0ZXIgL0ZsYXRlRGVj b2RlPj4Kc3RyZWFtCnic7VtZs9zEFa5A2CYpQyALSUiiNyQqI3pfeGOrOECgMJc8BPJg38Xbta8x NqbyL/Jn85yvF6mPpNbMXMoPVCrlskvT6j59+izfWdT+pmG9UA0Lf4aH43ubt69x2dz8dsOb8Ofh zY2zrGfeNU7yXmvf3Ns4wXrDxDhiHes50431vGdGlt8K741pjjfDiMEMbZuBgNHhp2qGLYbfxxvP fa+9Gke8CTOG9flXoT8MZAaG1QOD8yMcb87e2sieCwXST3DUj/D3Tj7ytb/8bx751uaLzTcNV1r3 0kVtcy5Vr3ijmeqNwFzfc+51tgLVfHCx+XwT7SMIRbMgsUAZkzkPlFygBCq2V83D00jfOZAUJtE3 eHamwSoBJhvJlOidzfR1or97iXAWm/i8xBy0xHLZDytsfYXoPV2hNes9z0tcWgKxKq5NMA8RVJCn QVzO6UjKaJxb6XTyJCa4yzcb1XNjjI0D9Pn4XvPeUdjBQ3Y9k841R2cb7OydlyJx1kBtTirbWA19 YuHRvc1X7U+6LYxJYR5vn+m2CprG6vbZbivCI7PtT8MM47znrn2uc2CTG9s+3215mKAUBiMJZYxv X+gkJoSnFzsMYnPVvtSJXjGtbfv1/W4re8cEF+3POg6FCWfan3cKTwxrvn4YqFrGNMd7Ft5z314Z 17+cGBROta9gI4ktOZ7+efQRzi5gN5CshLTL2fX87Er3GvYYz34NNITygrfXw3G5sKq9EQ6jpZRK thfkmY6X2ffJ6N2Os17ClNsmjAqDc6j2S+zhlJKifRSY98wb1z4Mj5Jr355CoFYLjB0noo6Z9lai et cetera..
I'm using this code:
$message = "<b>Hello World</b>";
$fromname = "Tester";
$subject = "Test - 3";
$headers = "Content-Type:text/html\n";
$to = "test#test.nl";
$attachment = $_SERVER['DOCUMENT_ROOT'] . "/facturen/test.pdf";
$mail = new PHPMailer();
$body = preg_replace('/\[\]/',"",$message);
$mail->IsSMTP();
$mail->Host = "smtp.test.nl";
$mail->SMTPAuth = true;
$mail->Host = "smtp.test.nl";
$mail->Port = 25;
$mail->Username = $user;
$mail->Password = $pass;
$mail->IsHTML(true);
$mail->SetFrom($user, $user);
$mail->FromName = $fromname;
$mail->Subject = $subject;
$mail->Body = $body;
$mail->addCustomHeader($headers);
$address = $to;
$mail->AddAddress($address, $address);
$mail->AddAttachment($attachment, "factuur.pdf", $encoding = 'base64', $type = 'application/pdf');
if(!$mail->Send()) {
echo "Fout";
} else {
echo "Goed";
}
Anyone an idea how to resolve this?
try removing:
$body = preg_replace('/\[\]/',"",$message);
and
$headers = "Content-Type:text/html\n";

How to format my email php mailer

When I send the email with html tags.Gmail shows the tags also.Why is that? any solution?how to ad images bold text colored text according to my code?
here is my email content code
smtpmailer("$email", 'website#yahoo.com', '<html><body>website.lk Password recovery', 'Password recovery','Dear "'.$name."\"\n\nUse your new password to login and reset your password as you wish.\nTo reset password go to your \"My Account\" page and click \"Change my password\"\n\n"."Here is your new password:\n\n"."Password: "."$password"."\n\nBack to get bump: www.website.lk\n\nRegards,\n website.lk Admin\n</body></html>");
$reset_msg="Recovery completed. Check your e-mail !";
}else{
$reset_msg="Error in sending email.Try again !";
}
By including the below headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
mail($to, $subject, $message, $headers);
Source,
http://php.net/manual/en/function.mail.php
if you are using PHP Mailer,
$mail->MsgHTML($body);
$mail->AddAttachment("images/image1.gif");
$mail->AddAttachment("images/image2.gif");
if you are using this function:
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // 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;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
Then change the line of $mail->Body = $body; to code blow:
$mail->MsgHTML($body);
This change will allow you to send HTML emails via PHPMailer, You can also add $mail->CharSet = 'UTF-8'; to avoid future problems with special characters ...
Use $mail->IsHTML(ture);
If on phpmailer..