PHPMailer - HTML content getting blocked - php

I'm having trouble trying to send out a HTML email.
I can:
Connect to the server fine with SMTP and SSL
I can send a simple HTML email fine
But anything that resembles what I would call "normal" HTML content gets blocked EVERY TIME!
Anyone suggest anything to try? I don't see anything wrong with what I've done, but obvs there's something there...
So this code gets blocked for me:
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = "xxxx";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Username = "xxx";
$mail->Password = "xxx";
$mail->setFrom('xxx', 'Me');
$mail->addReplyTo('xxx', 'Me');
$mail->addAddress('xxx#me.com');
$mail->Subject = 'Here come the bastards';
$mail->CharSet = 'UTF-8';
$mail->msgHTML('
<html>
<body>
<table style="width: 100%;background-color: #60bb98;">
<tr>
<td>
<a href="#">
<img src="http://mystorymycontent.com/wp-content/themes/MSMC/img/logo-new.gif" />
</a>
</td>
</tr>
<tr>
<td>HELLO EVERYONEZZZ
</td>
</tr>
</table>
</body>
</html>
');
$mail->AltBody = 'Heres all the copy from the HTML verison of the email. Theres a few lines about things and that';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

I'm using PHPMailer to send HTML mails and I had to do this :
$mail->IsHTML();
From the doc :
Before sending this out, we have to modify the PHPMailer object to
indicate that the message should be interpreted as HTML.
Technically-speaking, the message body has to be set up as
multipart/alternative. This is done with: $mail->IsHTML(true);

i agree with #FLX but in the phpmailer example it's written this way
$email ->IsHTML();
not
$mail ->IsHTML();
or the variable with you create the instance.
$VARIABLE = new PHPMailer();
.
.
.
.
$VARIABLE ->IsHTML();
it's just a suggestion

Related

Include an image in email PHP

I'm experiencing an issue where only the text from body is appearing the image is coming through broken, does anyone see where I might be going wrong?
<?php
require("php/PHPMailer.php");
require("php/SMTP.php");
if (isset($_GET['email'])) {
$emailID = $_GET['email'];
if($emailID = 1){
$mail = new PHPMailer\PHPMailer\PHPMailer();
$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 = "";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "";
$mail->Password = "";
$mail->SetFrom("");
$mail->Subject = "Test";
$mail->Body = '<html><body><p>My Image</p><img src="images/EmailHeader.png" width="75%"></body></html>';
$mail->AddAddress("");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message has been sent";
}
}
}
?>
The image URL you are using is relative. You will need to use the full URL.
Instead of:
<img src="images/EmailHeader.png" width="75%">
It needs to be an absolute URL that the public can see, such as:
<img src="https://image-website.com/images/EmailHeader.png" width="75%">
You can not upload image with relative path in Email. You have to use full path because when the mail is send it require whole path to fetch the image content.,
Replace this line,
$mail->Body = '<html><body><p>My Image</p><img src="images/EmailHeader.png" width="75%"></body></html>';
With this line,
$mail->Body = '<html><body><p>My Image</p><img src="http://your-domainname.com/images/EmailHeader.png" width="75%"></body></html>';
Another option is that you can use use base64 images for it. But in your case you have to give full path.

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.

PHPMAILER, IsHTML already set to true

The isHTML is already set to True. But it doesnt work in the email i receive. I just get the sample html from a tutorial.
<?php
require("phpmailertest/class.phpmailer.php");
$x=$_SESSION['items'];
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
// $mail->SMTPDebug = 2;
$mail->From = "benedictpayot#gmail.com";
$mail->FromName = "BravoTech Solutions";
$mail->Host = "smtp.gmail.com"; // specif smtp server
$mail->SMTPSecure= "ssl"; // Used instead of TLS when only POP mail is selected
$mail->Port = 465; // Used instead of 587 when only POP mail is selected
$mail->SMTPAuth = true;
$mail->Username = "benedictpayot#gmail.com"; // SMTP username
$mail->Password = "Ichthys030313!"; // SMTP password
$mail->AddAddress($_SESSION['email_address']);
$mail->IsHTML(true);
$mail->Subject = "Mail Test";
$mail->Body = '<html><body>';
$mail->Body = '<table style="border-color: #eee;"';
$mail->Body = '<tr>
<td>JALOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
<td>OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
<tr>
<td>BENEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEDICT
<td>PAYOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOT
</table>';
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
session_destroy();
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
You can set Body (and AltBody) manually to any value you like. msgHTML() is a convenience function to set moth of them and optionally apply an html to text conversion to generate your plain text version. It also sets isHTML, rewrites image URLs and various other things - but you don't have to use it.
In your code you're saying:
$mail->Body = '<html><body>';
$mail->Body = '<table style="border-color: #eee;"';
$mail->Body = '<tr>...
Which should be:
$mail->Body = '<html><body>';
$mail->Body .= '<table style="border-color: #eee;"';
$mail->Body .= '<tr>...
otherwise you're just overwriting the contents of Body each time.
You should base your code on the gmail example bundled with PHPMailer - it looks like you are using an old one from somewhere else.
I'm not sure (correct me if I'm wrong) but I was always setting body like this:
$mail->MsgHTML($body);

attachment is not sent with phpmailer

I'm trying to send an attachment with the phpMailer script.
Everything is working correctly since my email is sent correctly, however I do have a problem with an attachment that is not sent.
HTML part:
<p>
<label>Attachment :</label>
<input name="doc" type="file">
</p>
PHP:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = '*****';
$mail->SMTPAuth = true;
$mail->Port = 465;
$mail->Username = '*****';
$mail->Password = '*****';
$mail->SMTPSecure = 'ssl';
$mail->From = $_POST["name"];
$mail->FromName = 'Your Name';
$mail->Subject = 'Message Subject';
$mail->addAddress('*****');
$mail->addAttachment($_FILES["doc"]);
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = "You got a new message from your website :
Name: $_POST[name]
Company: $_POST[company]
Phone: $_POST[phone]
Email: $_POST[email]
Message: $_POST[message]";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Thanks!</title>
</head>
<body>
<p> <img src="img/correct.png" alt="icon" style=" margin-right: 10px;">Thank you! We will get back to you soon.</p>
</body>
</html>
Try:
if (isset($_FILES['doc']) &&
$_FILES['doc']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['doc']['tmp_name'],
$_FILES['doc']['name']);
}
Basic example can also be found [here](https://code.google.com/a/apache-extras.org/p/phpmailer/wiki/AdvancedMail).
The function definition for `AddAttachment` is:
public function AddAttachment($path,
$name = '',
$encoding = 'base64',
$type = 'application/octet-stream')
Please have a look about how PHP file uploads work. This array key:
$_FILES["doc"]
... will never contain a file. At most, it'll contain an array with information about where to find the file.
Untested solution, but should work:
change
$mail->addAttachment($_FILES["doc"]);
to
$mail->addAttachment($_FILES["doc"]["tmp_name"], $_FILES["doc"]["name"], "base64", $_FILES["doc"]["tmp_type"]);
However, please consider learning php upload file handling as already commented by others. Don't use user input without validation. Never!

PHPMailer and Style Sheet formatting

I'm using PHPMailer to automatically send an order acknowledgement as an HTML formatted email. Everything is working as expected except that the formatting of the acknowledgement isn't correct. I included my style sheet with an 'AddAttachment' line which seems to have fixed the header of the acknowledgement form, but the rest of the form still isn't right. Has anyone run into this situation before and know what I need to do to fix it? My edited program code follows in case it'll help!
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPDebug = 0;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true;
$mail->Port = 25; // set the SMTP port
$mail->Host = "<<smtp server>>"; // SMTP server
$mail->Username = "<<username>>";
$mail->Password = "<<password>>";
$mail->From = "<<email address>>";
$mail->FromName = "<<name>>";
$mail->AddAddress("<<email address>>");
$mail->Subject = "Acknowledgement Form";
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->IsHTML(true);
$mail->Body = file_get_contents('<<acknowledgement form page>>');
$mail->AddAttachment('printer.css'); // attach style sheet
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
You should'nt work with external CSS files in emails... Not many of the email-clients can handle that and not all of your customers use email-clients like Outlook or Thunderbird. Instead, you should disgn you html-code "90s style" :-P
e.g. <p style="width: 200px; text-align: center;">

Categories