Problems with sending HTML Email with PDF attachment using PHPMailer - html

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";

Related

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

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.

Contact form with PHPMailser doesn't work

I would use PHPMailer with a contact form but I am doing something wrong because it doesn't work.
<?php
require ("class.phpmailer.php");
if (isset($_POST['submit'])) {
$name=$_POST['name'];
$subject=$_POST['subject'];
$email=$_POST['email'];
$message=$_POST['message'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.mail.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'myemail#mail.com';
$mail-> Password = 12345;
$mail->From = ($email);
$mail->FromName = ($name);
$mail->addAddress = 'myemail#mail.com';
$mail->isHTML(false);
$mail->Subject = "Enquiry from Website submitted by $name";
}
if (!$mail->Send()) {
echo "<script>alert('Submission failed.');</script>";``
}
else {
echo "<script>alert('Email has been sent successfully.');</script>";
}
?>
Would you be so kind to give me a little big help? I don't understand where I'm doing wrong.
Thank you.
If you inspect debug output you can find a hint. Most possible reasons are incorrect parameters or maybe smtp server rejects request.
Please post debug output.

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
)
);

html email does not send in codeigniter

This is my code to send HTML email but its not working properly. Just raw html code is shown in the email.
$emailID = $this->input->post('email');
$name = $this->input->post('name');
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['mailtype'] = 'html';
$config['crlf'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
$config['validate'] = FALSE;
$this->email->set_newline("\r\n");
$this->email->from($emailID,$name);
$this->email->to('vishvajitmahida009#gmail.com');
$this->email->subject('Email Testing');
$this->email->message('from clientside mail');
$this->email->send();
$message = $this->load->view('emailfile/file','',TRUE);
$this->load->library('email', $config);
$this->email->from($emailID,$name);
$this->email->to($emailID,$name);
$this->email->subject('Email Testing');
$this->email->message($message);
$this->email->send();
//echo $this->email->print_debugger();
Add $this->email->initialize($config);:
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['mailtype'] = 'html';
$config['crlf'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
$config['validate'] = FALSE;
$this->email->initialize($config);

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..