HTML in php mail function - php

I have a contact form on my website so users can send me an email but I have run into a problem.
I want to use an HTML link inside the email and I also want the content the user is sending to me to be formated how they would like it.... let me explain.
If a user sends this:
Hello World!
Isnt it a great Day?
without using headers to enable html, then it says formated like that when it reaches me.
If I use headers (MIME) to enable html, to also include a link in the email (which I would like to do), then it reaches me as:
Hello World!Isnt it a great Day?
How can I include html, and also keep the email formatted properly?
Thanks
And hopefully all this makes sense :S

Use nl2br on your message - http://php.net/manual/en/function.nl2br.php
It will replace all newlines with HTML <br>
$message = nl2br($message, false);
The second parameter *is_xhtml* is optional if you want to send an HTML email instead of XHTML

Related

PHPMailer - Trying to set new line while sending emails

I've connected PHPMailer to my register system and once a user registers it auto-sends a email to the registered user, now I'm having a issue while attempting to set a new line while sending the email:
"Hello, ".$formvars['name']."<br>".
"Thank you for your registration with us and trusting us with your user account!"."<br>".
"If any problems arrise with logging into your account or using it, please adress this email."."<br>".
"\n".
"Kind Regards."."<br>".
"Auto-Bot!"."<br>";
I don't really see why it wouldn't make a new line properly, the \n or \r\n don't show in the email but also don't make a new line.
UPDATE:
Apologize for not providing enough code, the issue was that I called isHTML and tried to do that, I changed the lines to split up using
At a guess (because you didn't post your code), you're probably sending plain text as HTML, and HTML will ignore line breaks, so omit this line from your code:
$mail->isHTML();
If you want to use HTML, you can use HTML tags such as <p> and <br> to break up your text.

PHP - Send an array with <pre> tags via email

I've created my first multi-page form using php.
I like the way that the form data can be output using -
echo '<pre>'.print_r($_POST,true).'</pre>';
However, instead of printing, I'd like to keep that formatting but have it sent via email using the mail function.
are you sending it as a html email? because it wont work in plain text
See example 4 http://php.net/manual/en/function.mail.php

PHP prepare MAILTO with HTML

Im looking for a solution, to prepare a complete email for opening with a mail client
PHP is used for generating the link.
Here is the usefull part of my code.
$body = str_repalace('&amp','%26',$body);
$mailtarget = "mailto:".$adress."?cc=".$ccs."&Subject=".rawurlencode($subject)."&Body=".$body.
header("Location = $mailtarget");
This solution works fine, unless i use html in the mail, which is necessary (<br>,<hr>, <table>.
If used HTML sometimes nothing gets in Body, and sometimes just a part of the text.
When I mail the HTML with sendmail.exe, it works perfect. But this is not an option, since its required to add attachments manually to the mail.
Is there any way, to get this properly to work?
It's not possible to include HTML in body's mailto.
You can try to url_encode your HTML and set it as body in your mailto link, but it has a lot of risks of not working in many email clients.
From this answer, it's even defined in the RFC 2368 :
The special hname "body" indicates that the associated hvalue is the
body of the message. The "body" hname should contain the content for
the first text/plain body part of the message. The mailto URL is
primarily intended for generation of short text messages that are
actually the content of automatic processing (such as "subscribe"
messages for mailing lists), not general MIME bodies.
Try: &Body=".urlencode($body);
Use rawurlencode function to insert carriage return :
<?php
$body = '
First line
Second line';
header("location: mailto:emailadresse#domain.com?subject=Hello&body=".rawurlencode($body));
?>

Processing html mail using phpmailer mail->Body fetching mail body using imap_fetchbody

Okay. So what i did was that i retrieved the body of an email using imap_fetchbody and forward the contents in it to another user's email using mail->Body.
The following are the 2 different formats of the mail i get from different settings:
eg1:
mail->isHtml(true);
eg2:
mail->isHtml(false);
The result i want to have is eg2 but i need to insert html code into the mail itself hence i cannot have mail->isHtml(false), i need it to be true. I'm not sure why this is happening but i realize that the cause in difference in formatting is due to this mail->isHtm() property.
What can i do?
Any help here will be greatly appreciated.

How to show Embedded image in an HTML email as normal images (PHP)

I am creating an email client and going through many problems. Now the latest problem which I am facing is to show embedded images on the HTML email body.
My email body code looks similar to this:
<img alt="image001" src="cid:image001.gif#01CCB988.809DD560" id="Picture_x0020_1" />
Few related posts are found, but they are not useful. Also looking for other possible similar problems while displaying mail contents.
I am using PHP IMAP with POP3 to fetch mails. Currently using gmail as mail server. Sending mails using SMTP (PhpMailer).
Thanks in advance.
In HTML, images are standalones documents with a specific URI (used by src="" attribute).
In your MIME boby, this src attribute refer to a relative MIME part.
So, you'll need to write all MIME parts contents in a distinct file (i.e. /tmp/[md5_MIME_PART_ID])
Then use a CGI script to rewrite the email body and forward specific mime request :
readmail.php
<?
if($_GET['mime']) {
readfile("/tmp/{$_GET['mime']}");
die;
}
$contents = preg_replace("/sid:/", "readmail.php?mime=$1", $contents);
echo $contents;
Of course, a proper database system will be more efficient here, but that's a start
Did you use:
$mail->AddEmbeddedImage(filename, cid, name);
//By using this function with this example's value above, results in this code:
$mail->AddEmbeddedImage('my-photo.jpg', 'my-photo', 'my-photo.jpg ');
If yes, what is going wrong? Why are you not happy.
Please note that by default, most email clients do not show embedded images, this is goes for Gmail and Outlook. You can not circumvent that. Its a security / bandwith saving feature. The user would first have to press 'Show images' before they appear.

Categories