Mail content:
<html><head></head><body><p> Message : 鏽嫌╒杜米土 杯屎</p>Share Link : Press here to enter <br><img src ='http://203.80.1.28/FlippingBook/development/demo/medium/Web081112_P070_medium.jpg' /></body></html>
PHP:
if (isset($_POST["data"])){
$info = explode("&", $_POST["data"]);
$headers = 'MIME-Version: 1.0\r\n';
$headers .= "Content-Type: text/html; charset = \"UTF-8\";\n";
$headers = "From: =?UTF-8?B?" . base64_encode(substr($info[0],strpos($info[0],'=')+1, strlen($info[0]))) . "?=";
$to = substr($info[1],strpos($info[1],'=')+1, strlen($info[1]));
$subject = "=?UTF-8?B?" . base64_encode('日報分享') . "?=";
$message = trim(substr($info[2],strpos($info[2],'=')+1, strlen($info[2])));
$message = '<html><head></head><body><p> Message : '.$message;
$url = substr($info[3],strpos($info[3],'=')+1, strlen($info[3]));
$message = $message. '</p>Share Link : Press here to enter ';
if (isset($info[4])){
$firstImg = substr($info[4],strpos($info[4],'=')+1, strlen($info[4]));
$message = $message."<br><img src ='".$firstImg."' />";
}
if (isset($info[5])){
$secondImg = substr($info[5],strpos($info[5],'=')+1, strlen($info[5]));
$message = $message."<br><img src ='".$secondImg."' />";
}
$message = $message.'</body></html>';
if (mail($to, $subject, $message, $headers))
die ('Mail sent');
else
die ('Fail');
}else{
die ('Fail');
}
I am writing a simple program to send email. However, my mail content is not english based so I used utf-8 to encode.
When I changed the encode method, it can not send the processed html code, instead the mail content is the raw html code shown above, how to fix this problem?
If using PEAR lib is not a problem than you can look for Pear Mail-Mime lib to send mail or HTML contents. More details you can get from here - http://pear.php.net/manual/en/package.mail.mail-mime.example.php
Related
This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 3 years ago.
I am trying to send a text by mail in php. The message is placed inside a HTML tag. While sending, the mail part works but the text I receive has HTML tags in it.
Here is the code for better understanding of my issue:
$car = "1223455667";
$to = "something#sample.com";
$subject = 'Test Mail';
$message = '<p>' . $car . '</p>';
$from = 'mymail#sample.com';
if(mail($to, $subject, $message )){
echo 'success';
}else{
echo 'Unable to send the email. Please try again.';
}
Output I receive in mail:
I expect the output like this: 1223455667
Pass Headers in your mail
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
if(mail($to, $subject, $message, $headers )){
echo 'success';
}else{
echo 'Unable to send the email. Please try again.';
}
I have been trying to do tests to send an email through a form with php, but it does not work for me. The following message is returned: Error sending the email. That is, the value of the variable boolean $ sent is false.
I use Apache, XAMPP for Windows 10 with the Netbeans 8.2 development environment
Let's see if anyone can help me, thanks in advance.
This is the code that is giving faults:
<?php
$for = 'email#example.com';
$title = 'Sending email from PHP';
$message = '<html><head><title> Email with HTML </title></head>
<body><h1> Email with HTML </h1>
This is an email that is sent in HTML format
<hr>Sent by my program in PHP</body></html>';
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers. = 'Content-type: text / html; charset = utf-8 '. "\r\n";
$headers. = 'From: Name1 Name 2 <info#example.com>';
$sent = mail ($for, $title, $message, $headers);
if ($sent)
echo 'Email sent correctly';
else
echo 'Error in sending the email';
?>
as far as I can see, you have some concatenation issues, I m not sure those actually exist in your code or are due to copy paste. Try following, take care of $header concatenation and semicolons ; at the end of statements.
<?php
$for = 'email#example.com';
$title = 'Sending email from PHP';
$message = '<html>'.
'<head> <title> Email with HTML </title> </head>'.
'<body> <h1> Email with HTML </h1>'.
'This is an email that is sent in HTML format.'
'<hr>'.
'Sent by my program in PHP'.
'</body>'.
'</html>';
$headers = 'MIME-Version: 1.0'. "\ r \ n";
$headers .= 'Content-type: text / html; charset = utf-8 '. "\ r \ n";
$headers .= 'From: Name1 Name 2 <info#example.com>';
$sent = mail ($for, $title, $message, $headers);
if ($sent)
echo 'Email sent correctly';
else
echo 'Error in sending the email';
?>
I have a form I am using to simply capture a name and email from a user. When the user submits the form an email is then fired to the specified recipient containing a link.
I had this all working but realised I needed to add a html footer to the email which was originally being read as plain text.
After doing some digging I have now tried to set the footer to read as html ,but the email no longer sends. I am not a php developer.
My question is have I missed something or am I just way off?
<?php
// configure
$from = 'Ether Solutions Sales <email#domain.co.uk>';
$sendTo = $_POST['remail'];
$subject = "test email";
$name = $_POST['name'];
$okMessage = 'your message has been sent successfully, please ask the recipient to check their inbox';
$errorMessage = 'There was an error while submitting the form. Please try again later';
// 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";
// Create email headers
$headers .= 'From: '.$from."\r\n".'Reply-To: '.$from."\r\n" . 'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<p style="margin-bottom:30px;">regards dave</p>';
$message .= 'www.domain.co.uk';
$message .= '<p style="font-color:blue;">Office Tel: 0844 000 000</p>';
$message .= '<p style="font-color:blue;">Mobile: 0000 000 0000</p>';
$message .= '<p style="font-color:blue;">Technical Support: 0845 000 0000</p>
';
$message .= '<p style="font-color:blue; font-weight:bold;">Some tagline here</p>
';
$message .= '<img src="imageurl/test.png" alt="sample alt tag"/>';
$message .= '</body></html>';
// let's do the sending
try
{
$emailText = "{$name} you have been sent an electronic document to be signed.\nTo view your document please click the following link URL: /www.domain.com";
mail($sendTo, $subject, $emailText, $message, $headers, "From: " . $from);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
I tested your code and got this warning:
PHP Warning: mail() expects at most 5 parameters, 6 given in /home/ruemercc/public_html/lab/email2/mail.php on line 47
I would suggest you use PHPMailer library found here phpmailer
. It will allow you to even send bulk emails(Which I think will be an added advantege in your case).
Have fun coding
Your call of the mail() wrong. You are setting the parameters not correct.
Take a look in the PHP Manual for mail().
From the php manual
mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Your code mail($sendTo, $subject, $emailText, $message, $headers, "From: " . $from) uses message as $additional_headers which is wrong.
You have to merge your $message with the $emailText.
I'm trying to email a link thru PHPMail. Somehow link is showing as plain text in email.
I want that link should be clickable when received in email. Currently it is showing as plain code.
Below is the code, please help!
<?php
$name=$_POST['firstname'];
$email=$_POST['email'];
$phoneno=$_POST['phoneno'];
$com=$_POST['comment'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
?>
Print Boss your receipt
<?php
//$link="hello.php?a=$name&b=$email&c=$phoneno&d=$com";
$to = "ketanjoshi1993#gmail.com";
//$from = $email;
$subject = "send email in pdf format";
$message = "<a href='#'>test</a>";
$email = mail($to,$subject,$message);
if( $email == true )
{
echo "sent";
}
else
{
echo "not sent";
}
?>
You forgot to use your headers in your mail call:
$email = mail($to, $subject, $message, $headers);
So you forget to add the headers
try adding it as
$email = mail($to,$subject,$message, $headers);
It will work
You forget to add the headers
You can do like this :
$email = mail($to,$subject,$message, $headers);
If you don't pass the header parameter in PHP mail() function , default headers will be set & your will be treated as simple text body.
I've done some research on this issue but still can't find an answer that I understand. Basically I'm trying to add a Logo to the emails sent via my email PHP script. The code of my PHP script is:
<?php
$name = $_POST['Name'];
$email = $_POST['Email'];
$sub = $_POST['Subject'];
$message = $_POST['Message'];
$Subject = "$sub";
$Sendto = "Omitted for privacy reasons";
mail($Sendto,$Subject,
"$message
Email: $email
Name: $name
"
,"From: $email,$name");
?>
How do I go about putting an image held on my server in Images/logo above the email:$email part of the message?
thanks
As a side question can anyone provide me with a guide or reading on how to beautify the message as its so plain
What you might do is to send the mail not as "text/plain", but as "text/html".
You e-mail content is then basically a static webpage and you can insert images to.
Example Code for Mail with HTML Content
$to = 'to#someone.com';
$subject = 'The Subject';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: other#someone.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
// this is the important header to set the type
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// now $message can be a static html page like:
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '</body></html>';
// with image embedded
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '<img src="http://i.stack.imgur.com/Qmw3Z.jpg?s=32&g=1" alt="AVTR" />';
$message .= '</body></html>';
Important: If you insert something into the message coming from POST, do not forget to escape it properly with htmlentities($_POST['somevar']) or striptags.
Then send mail:
mail($to, $subject, $message, $headers);
Answer to side-quest: "can anyone provide me with a guide or reading on how to beautify the message as its so plain". When you send the mail as HTML you might apply CSS styles to your HTML.
Starter tutorial: http://webdesign.tutsplus.com/articles/build-an-html-email-template-from-scratch--webdesign-12770
you might also google for "html email templates", download one and modify to your needs