I am trying to send emails from PHPMailer. Everything is working but the problem is it is sending emails along with HTML tags even after writing $mail->IsHTML(true); . Below is my code for sending emails.
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = EMAIL_COMPOSE_SECURE;
$mail->Host = EMAIL_COMPOSE_SMTP_HOST;
$mail->Port = EMAIL_COMPOSE_PORT;
$mail->Username = EMAIL_COMPOSE_OUTGOING_USERNAME;
$mail->Password = EMAIL_COMPOSE_OUTGOING_PASSWORD;
$mail->SetFrom(EMAIL_COMPOSE_INCOMING_USERNAME);
$mail->Subject =$subject;
$mail->Body = $message;
$mail->IsHTML(true);
$mail->AddAddress($email_to);
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
echo "Message has been sent";
}
And one more thing I will mention, in my application text editor for writing the emails is ckeditor. Will that cause any problem? Please help.
Why would you not expect it to use HTML if you call IsHTML(true)? That's how you tell PHPMailer to treat your message body as HTML! If you don't want HTML as the content type, call IsHTML(false), or just don't call it at all since plain text is the default.
If you want both HTML and plain text, call msgHTML($html) instead and it will also handle the HTML->text conversion for you.
As Chris said, call IsHTML before setting Body.
And as Dagon said, if you put HTML in $message, it will send HTML...
Unless I'm misunderstanding your question, it should be sending the HTML tags. Check that the received email has the Content-Type: is text/html.
It has to send the HTML tags still for the client at the other side to be able to display it properly. If the message contains HTML tags and you don't want the tags, then you want to call IsHtml(false) and you need to actually strip the HTML from the message.
Not all email clients can read HTML tags. So if you're seeing the HTML it's either because your client can't render HTML, or that the Content-Type: is text/plain.
Related
I'm trying to figure out why my emails are considered spam by hotmail, so I'm using an online tool called mail-tester.com.
According to it, I have an 8.4 out of 10, but one of the problems is that SpamAssasin detected that "Message only has text/html MIME parts", it adds that "You should also include a text version of your message (text/plain)" and it points a link to the spamAssasin rule "MIME_HTML_ONLY"
Myq uestion is: I'm sending the emails using phpMailer. My email is not blacklisted and this just happens with hotmail.
I'm setting the headers as text/plain.
I'm putting the email content into the strip_tags() function.
Why it says that I'm sending HTML?
This is what I'm using (the whole script is quite long, but this is the pertinent part that sends the email. I do receive the email without issues in other email clients, like gmail.
require $_SERVER['DOCUMENT_ROOT'].'/phpMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.mysite.online';
$mail->SMTPAuth = true;
$mail->Username = 'info#mysite.online';
$mail->Port = 25;
$mail->CharSet = 'UTF-8';
$headers .= 'Content-type: text/plain';
$mensajeLimpio = strip_tags($resultado['mailMensaje']);
$mail->addAddress($pedidoEmail);
$mail->Subject = $resultado['mailAsunto'];
$mail->Body = 'Estimado/a '.$nombreUsuario.',<br>'.$mensajeLimpio;
Add $mail->IsHTML(false); to your options
I'm building a PHP based newsletter application. I know it is a good practice to send plain text version of the email beside the html as well. My question is how can I do this in practice? Just simply put one under the other? like:
<p style="font-weight:bold;">This is a newsletter!</p>
<p style="color:red;">
You can read awesome things here!
<br/>
check out us on the web!
</p>
This is a newsletter\r\n
You can read awesome things here!\r\n
check out us on the web: www.the-awesome-site.com
Won't these two interfere with each other? I mean if the mailer client can understand HTML, then the plain text with nearly the same content would be confusing at the end of the mail. Or if the client can't parse html then the user will see bothering raw HTML source before the human friendly plain text. Is there any way hide the useless one depending on the situation? I'm going to use PHPMailer if it is important.
PHPMailer is a great class to use because it detects when the email client doesn't support HTML.
Check out this code snippet. It should help a little.
require("PHPMailer-master/PHPMailerAutoload.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "mail.somemailserver.com";
$mail->SMTPAuth = false;
$mail->From = $someemail;
$mail->FromName = "Who ever";
$mail->AddAddress($email);
$mail->AddCC($anotheremail);
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Some subject";
$mail->Body = "<html>Content goes here</html>";
//if the client doesn't support html email use
$mail->AltBody = "Content";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
By using the $mail->AltBody, you can send plain text emails. Hope this helps!
My company is currently trying to streamline our process for submitting forms. I have read that what we are trying to do can be done with PHP or PHPMailer but I seem to have hit a roadblock.
What we are trying to do is open a fillable PDF in browser, complete it, and then click a button at the bottom to email it to a designated recipient.
I currently have a PHP script that allows me to email the blank document using PHPMailer and our server email service.
I have tried using the "AddStringAttachment" feature of PHPMailer:
<?php
require("../PHPMailer_5.2.3/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "xxx.xxx.org"; // SMTP server
$mail->From = "xxx#xxx.org";
$mail->AddAddress("xxx#xxx.org");
$mail->Subject = "Attachment Test";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
$mail->AddStringAttachment($string,'filename.pdf');
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
...but I was unable to find a way to define the string in a way that would send the completed form. Currently, if I put any data into the "$string" field the email fails.
Is this even possible? Or am I just spinning my wheels?
The problem might be with your mail server. Many servers don't like sending corrupted PDFs, which would be any PDF that doesn't look like a normal PDF - say, a string of English text.
Are you able to send any other kind of attachment, before I get into how to generate a PDF in PHP?
I am new to php and have been learning on the fly as I build pages. My current task is to be able to open a PDF in browser, complete the form, and submit the form upon clicking a button at the bottom of the form.
We would like the completed form to be sent without having to save a copy to the server. Having a two form cycle would not be horrible though.
I currently am able to send an email, but the script only sends the blank document.
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "xxx.xxx.xxx"; // SMTP server
$mail->From = "xxx#xxx.xxx";
$mail->AddAddress("xxx#xxx.xxx");
$mail->Subject = "Attachment Test";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
$mail->AddAttachment('tshirtorderform.pdf');
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
I realize that I am telling it to retrieve the blank document, but I am coming up empty on how to attach the completed form.
Thanks.
There's a semi-undocumented AddStringAttachment() method in PHPmailer, which lets you attach directly from a string, instead of requiring an actual on-disk file:
http://phpmailer.worxware.com/index.php?pg=tutorial
search for "String Attachments". Beats me why they don't list in the Methods documentation.
I recommended AddStringAttachment() method also, but first You need to use TCPDF to create PDF's body.
I gave some hint in this topic: AddStringAttachment with PDF in PHPMailer not work
I am using the class PHPMailer to send Mails via SMTP:
<?php
require 'php_mailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.dfgdfgdfg.de'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'dfgdfg'; // SMTP username
$mail->Password = 'dfgsdfgdsfg'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'community#fdgdfg.de';
$mail->FromName = 'dfgdfgdg';
$mail->AddAddress('interview#dfgdfg.de', 'Udo'); // Add a recipient
$mail->AddBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'HTML-Mail mit Logo';
$mail->Body = 'Nachfolgend das <b>Logo</b>';
$mail->AltBody = 'Aktiviere HTML, damit das Logo angezeigt wird';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
?>
My Questions:
Whats the best way to send to a lot of Mails (same Mailtext, only the appellation is different (Hello $NAME)?
Is the PHP script waiting until every mail is delivered? Because sometimes I want to send a mail to some hundreds people, when a user is doing an action on the website. so this user cant wait of course, until all those mail were sent succesful!
Thanks!
Alex
You are setting PHPMailer to interact with SMTP, so I guess that it will wait for it to complete. This is not optimal, because as you say you will block the PHP script until SMTP responds.
It would be better to send through your localhost: set PHPMailer to use sendmail, which will usually be a wrapper to a local exim4 or postfix, which will then handle the mailing for you. This is much better, also because the local server will handle any possible temporary error, and retry later. PHP won't.
You may also want to explore other options, like Mandrill or Sendgrid to do the job, especially if you do lot of mailing or bulk mailing.
Whats the best way to send to a lot of Mails (same Mailtext, only the
appellation is different (Hello $NAME)?
You can do something like, Set the name too.
// rest of code first
$mail->AddAddress("you#example.com")
$ids = mysql_query($select, $connection) or die(mysql_error());
while ($row = mysql_fetch_row($ids)) {
$mail->AddBCC($row[0]);
}
$mail->Send();//Sends the email
You can have special string 'name_here' in the body and place $name with str_replace function
Is the PHP script waiting until every mail is delivered? Because sometimes I want to send a mail to some hundreds people, when a user is doing an action on the website. so this user cant wait of course, until all those mail were sent succesful!
Yes according to my knowledge you will have to wait.
How to do a str_replace ? Assume that your email body is as follows
$body = " Dear %first_name%,
other stuff goes here....... ";
$body = str_replace("%first_name%", $first_name, $body);
above will replace %first_name% with the name($first_name) you provide.