I need to send a email with a HTML msg on the Subject.
For the body, I use $mail->MsgHTML($someHTML); and it works fine.
But when I try to do the same with the Subject, it just interpret the whole thing as a text.
What I need is something like this:
$subject = "<img src='PATH_TO_IMG_FILE_IN_MY_SERVER'> This is the subject"
$mail->Subject = $subject;
Already tried to use the $mail->isHTML(true); but for what I read, it works only for the email's body.
Definitively, no. Email doesn't support HTML in the subject line, or any other header.
Related
I need to send text from users typed in a html form (textarea) along with emails. I use PHPMailer with HTML setup. This works perfect also because I need to embedd logos. The only problem is that email programs which receive the mail ignores line breaks from this text which. I parse the text like this :
$mail->isHTML(true);
$mail->Body = $_POST['mail_Text'];
What do I need to do ?
nl2br() should work for you.
So, add mail body like this.
$mail->Body = nl2br($_POST['mail_Text']);
nl2br() returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).
Using headers, the html tags would be parsed in Mail Content/Body, But what if we want to have some html tags in Mail Subject. Is there any way out for this
Even if you put html tags in mail subject, the mail client won't render it as html
As per email RFC, email subject can't have a content type
From rfc http://www.w3.org/Protocols/rfc822/
3.1.2. STRUCTURE OF HEADER FIELDS
Once a field has been unfolded, it may be viewed as being composed of a field-name followed by a colon (":"), followed by a field-body, and terminated by a carriage-return/line-feed. The field-name must be composed of printable ASCII characters (i.e., characters that have values between 33. and 126., decimal, except colon). The field-body may be composed of any ASCII characters, except CR or LF. (While CR and/or LF may be present in the actual text, they are removed by the action of unfolding the field.)
Certain field-bodies of headers may be interpreted according to an internal syntax that some systems may wish to parse. These fields are called "structured fields". Examples include fields containing dates and addresses. Other fields, such as "Subject" and "Comments", are regarded simply as strings of text.
Note:
Any field which has a field-body that is defined as other than simply <text> is to be treated as a structured field.
Field-names, unstructured field bodies and structured field bodies each are scanned by their own, independent "lexical" analyzers.
3.1.3. UNSTRUCTURED FIELD BODIES
For some fields, such as "Subject" and "Comments", no structuring is assumed, and they are treated simply as <text>s, as in the message body. Rules of folding apply to these fields, so that such field bodies which occupy several lines must therefore have the second and successive lines indented by at least one LWSP-char.
<?php
require_once('phpmailer/class.phpmailer.php');
$ = $_POST[];
$ = $_POST[];
$ = $_POST[];
$ = $_POST[];
$mail = new PHPMailer();
$mail->CharSet = "utf-8";
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Username = "example#example.com";
$mail->Password = "";
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = "465";
$mail->setFrom('emailaddress','name');
$mail->AddAddress('email address', 'name');
$mail->Subject = 'Example';
$mail->IsHTML(true);
ob_start();
?>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
<?php
$mail->Body = ob_get_clean();
if($mail->Send())
{
header('');
}
else
{
echo "Mail Error - >".$mail->ErrorInfo;
}
?>
Recently I use the mail($to, $subject, $content, $headers) to send email with a very long content including some images link like http://foo.com/image/1.jpg. The problem is that the email body sometimes break at some point and show the link like http://foo.com/image/1.jp+g, which will break the link.
Or sometimes it just break the html tags like break <div> in to < div>, and finally the tag is shown in somewhere that is not supposed to be. Making the email looks so wired.
This problem has bugged me for a whole morning.
Actually, I tracked down this problem and find that the body is too long that in the email, it will add newline automatically in the $content when applied in the mail() message body. As a result the image link will be likehttp://foo.com/image/1.jp
g
and the tags will be like <
div>
This problem has troubled me for a whole morning.
Now I found the solution would be adding \n at some point in your message so that your mail body is not only on one line, and the mail() will not add its own new line at the point that you do not want it to.
Hope this can help someone if they have the similar problem.
<a href='http://stackoverflow.com' style='color:red;'>testing</a>
The above is my text inside textarea. I am passing this textarea value as a mail body. The word "testing" appears neither anchored nor red.
However, when I write this:
$body="<a href='http://stackoverflow.com' style='color:red;'>testing</a>";
..and pass $body as my mail body, it works fine.
What could cause this behavior?
my first guess is that you have "Magic Quotes" option on in the php settings, and the mail body ends up like:
<a href=\'http://stackoverflow.com\' style=\'color:red;\'>testing</a>
Try to use "view source" on the recived mail, or use "save as" and open the file in a simple text editor, and you should se how the mail ended up.
if my guess was right, you could either turn of magic quets, or use stripslashes() to remove the extra slaches
Hy Ho,
It is possible to maintain the format of a text area with a PHP form so that a message that is mail'd to the admin is formatted nicely.
ie. If someone writes in the textarea,
Dear Sir,
I am writing in connceti....
Many Thanks,
At the moment it emails as
Dear Sir, I am writing in connceti...
Many Thanks,
If not then I suppose the solution is Rich Text Editor replacement textarea. Thats all well and good, but what if javascript is disable.
Any ideas,
Marvellous
You probably need to replace (depending on system):
\n\r or \n or \r
By:
<br />
PHP function needed:
nl2br()
At this point it seems as though your emails might be sent in HTML format. HTML format automatically removes the line breaks if the given input is not in HTML format. If you don't want HTML formatting then try sending the message plain text, then your line breaks should still show.
To send the message as plain text, simply use the PHP mail() function, without any additional headers:
mail('john#gmail.com', 'Test Email', 'My Message...
with a line break!');
If you are still having problems, try integrating an HTML WYSIWYG editor, such as the powerful TinyMCE editor.
Note: for the HTML editor to send emails properly, you will need to send emails messages in HTML format (where you would need to supply headers as the fourth parameter in the mail() function). This will, for sure, solve your line break issues.
Hope that helps,
spryno724
The newlines in a <textarea> are preserved by both the browser and PHP. Probably they get lost in the email body itself or they are simply not being displayed by the browser even if they are there.
If you are sending a HTML mail you need to do nl2br(htmlspecialchars($your_textarea_data)) to add the necessary <br> tags.
If you are sending a plain text mail check if you are mixing LF (Unix-style) and CR+LF (DOS-style) newline sequences that maybe confuse your e-mail client.
This depends purely in your email format.
When you type that text, it actually looks like this
Dear Sir, \n
\n
I am writing in connceti....\n
\n
Many Thanks,
If you're sending text mail, you have to make sure the linebreaks are not stripped out. And if you're sending out HTML email, you have to make sure you replace those linebreaks for <br> tags