This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 9 years ago.
I'm sending an email via PHP's mail() function. In the message I set a link that looks like this:
$message = "<a href='". $link. "'>" .$title. "</a>\n\n";
However, when the email is received, the email body shows the html code instead of the title as a hyperlink. I'm not very familiar with html emails. How could I achieve what I am trying to get?
Try to add an header, so that the mail client doesn't believe it is plain text:
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
See PHP mail function manual:
Example #4 Sending HTML email:
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$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 it
mail($to, $subject, $message, $headers);
?>
Please see the documentation for PHP mail() at http://php.net/manual/en/function.mail.php
You need to specify a Content Type of HTML in your function.
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$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 it
mail($to, $subject, $message, $headers);
Although it's generally recommended to avoid using mail() alone.
You should consider using PHPMailer, for example.
You have to tell the e-mail client that there is an HTML portion to the e-mail. I'd recommend something like Swiftmailer to do the work instead of doing everything yourself.
Related
How do I format the information so when it's displayed in the Email client, it'll show line breaks and other html tags that' i've set in the .php file?
//proceed with PHP email.
$headers = 'From: '.$user_Email.'' . "\r\n" .
'Reply-To: '.$user_Email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$sentMail = #mail($to_Email, $subject, $user_Message .' -'.$user_Name, $headers);
would it be something like:
$message = '<b>$user_Message';
Im trying to write a simple script to send a message to .csv list of e-mail addresses like this:
mail1#mail.com,
mail2#mail.com,
mail3#mail.com,
Here is what I have tried:
<?
$csv = array();
$file = fopen('test.csv', 'r');
while (($result = fgetcsv($file)) !== false){
$csv[] = $result;
$to = $result[0];
$subject = "My mail subject";
$message .= '<html><body>';
$message .= 'My message here';
$message .= "</body></html>";
$from = "example#example.com";
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $message, $headers);
}
fclose($file);
?>
But the message is sent three times, and includes the message three times in the body. How can I make the message only be included and sent one time to each line?
You should remove $subject, $message, $from and $headers definition from the while loop. Define all of them before and in the while define only the $to field and keep the mail() command.
Either follow the above suggestion (good approach) or simply remove concatenation operator "." form first assignment to variable $message i.e. change
$message .= '<html><body>'; TO
$message = '<html><body>';
I'm getting exclamation points in random spots in the result of this PHP email function. I read that it's because my lines are too long or I have to encode the email in Base64 but I do not know how to do that.
This is what I have:
$to = "you#you.you";
$subject = "Pulling Hair Out";
$from = "me#me.me";
$headers = "From:" . $from;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 64bit\r\n";
mail($to,$subject,$message,$headers);
How do I fix this so there's no random ! in the result? Thanks!
As stated here: Exclamation Point in HTML Email
The issue is that your string is too long. Feed an HTML string longer than 78 characters to the mail function, and you will end up with a ! (bang) in your string.
This is due to line length limits in RFC2822 https://www.rfc-editor.org/rfc/rfc2822#section-2.1.1
Try to use this piece of code:
$to = "you#you.you";
$subject = "Pulling Hair Out";
$from = "me#me.me";
$headers = "From:" . $from;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 64bit\r\n";
$finalMessage = wordwrap( $message, 75, "\n" );
mail($to,$subject,$finalMessage,$headers);
The problem is that one line should not be longer than 998 characters. (see also https://stackoverflow.com/a/12840338/2136148)
You are right, that is because your email is too long. Try replacing with this line in your mail header.
Content-Transfer-Encoding: quoted-printable
The answers here have the correct information regarding the line length, however none of them provided me with the sufficient code snippet to fix the issue. I looked around and found the best way to do this, here it is;
<?php
// send base64 encoded email to allow large strings that will not get broken up
// ==============================================
$eol = "\r\n";
// a random hash will be necessary to send mixed content
$separator = md5(time());
$headers = "MIME-Version: 1.0".$eol;
$headers .= "From: Me <info#example.com>".$eol;
$headers .= "Content-Type: multipart/alternative; boundary=\"$separator\"".$eol;
$headers .= "--$separator".$eol;
$headers .= "Content-Type: text/html; charset=utf-8".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol.$eol;
// message body
$body = rtrim(chunk_split(base64_encode($html)));
mail($email, $subject, $body, $headers);
// ==============================================
Is there a possible way, to insert data from table to mail sending script? I have made this simple script, but it doesnt work. How to mix these 2 codes?
$result = mysql_query("SELECT * FROM tablename WHERE ID =1" ) or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo ''. $row['maillist'] .''; }
$to = 'here must be maillist row';
$subject = 'my subject:';
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message = 'html content with img src tag';
mail($to, $subject, $message, $headers);
My second question is: If i am using bcc, than gmail or other mail services displaying full code of message with all tags, but not displaying image. So, is there a possible way to fix this problem?
My third question is: If i am inserting image to message (watch the code), then the message appears in SPAM, but if i am using only basic text, than its all normal. How to fix it?
I will be grateful for any answers and help!
$q = "SELECT email FROM table WHERE id = '" . $id . "'";
$r = mysql_query($q) or die(mysql_error().'<br />'.$q);
$d = mysql_fetch_assoc($r);
$to = $d['email']:
$subject = 'my subject:';
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message = 'html content with img src tag';
mail($to, $subject, $message, $headers);
If you wanna send email to several people :
$subject = 'my subject:';
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message = 'html content with img src tag';
$q = "SELECT email FROM table WHERE id = IN (" . $array_ids . ")";
$r = mysql_query($q) or die(mysql_error().'<br />'.$q);
while($row = mysql_fetch_assoc($r)) {
$to = $row['email']:
mail($to, $subject, $message, $headers);
}
For the SPAM problem, I'd say try to set you header like this
$headers .= 'Content-type: image/jpeg' . "\r\n";
I'm having a textarea in my contact form with php mail function. In php mail i have set the header to html.
But even if the user types like this
Line1....
Line2....
I'm getting in mail like this.
Line1....Line2....
What could be the reason?
Update:
The text area is as simple as this.
<textarea id ="msg" name="message" cols="" rows="5" class="msg">Message</textarea>
Its posted to this script with jquery ajax function
<?php
$sub = "Message Posted";
$email = "some#somewhere.com";
$message = "<b>Message :</b><br/>" . $_REQUEST["msg"];
$message = wordwrap($message, 70);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ifthi#ifthi.com' . "\r\n" .
'Reply-To: '. $_REQUEST["email"] . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send
mail($email, $sub, $message,$headers);
?>
But when getting in email all are in a single line. Even if you write in 2 line and submit.
if you set your mail to HTML you should replace all line breaks with HTML Tags.
I think the PHP function you need is:
string nl2br ( string $string [, bool $is_xhtml = true ] )
<?php
//whatever you want to replace new lines with
$newLineCode = "<br/>";
$message = $_POST['myTextArea'] ; //unadulterad text we got via Post
$modifiedTextAreaText = ereg_replace( "\n", $newLineCode, $message);
echo " Result of Code Snippet " . $modifiedTextAreaText ;
?>