This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 4 years ago.
This is in code-igniter. To send mail I have written a function using php mail(). The code is:
function mailsend()
{
$to="mymail#gmail.com";
$subject="test";
$message="<table>
<tr>
<td>Hello</td>
<td>World</td>
</tr>
</table>";
echo $message;
mail($to,$subject,$message);
}
When I send mail from server side, I get the html code in my mail, not the real table. But the $messageshows the real table in browser. Is there any way in mail() to get the real table in the mail?
You can send the html content in email via setting the header portion. Below is the sample code you can use.
<?php
$to = 'maryjane#email.com';
$subject = 'Marriage Proposal';
$from = 'peterparker#email.com';
// 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 .= '<h1 style="color:#f40;">Hi Jane!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</body></html>';
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
In order to send html content in your email with mail function you will need to add header options to it, like so:
$headers = 'From: test#yourdomain.com' . "\r\n" .
'Reply-To: test#yourdomain.com' . "\r\n" .
"Content-type:text/html;charset=UTF-8" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($sendTo, $subject, $message, $headers);
Add an header to your mail function like this
$headers = 'From: test#yourdomain.com' . "\r\n" .
'Reply-To: hello#example.com' . "\r\n" .
"Content-type:text/html;charset=UTF-8" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Related
I can't send html email through PHP.
in yahoo email, it is showing text based email. How can i make email to show normal html based.
Output of my email in yahoo mail:
<html><head></head><body><h2>test heading</h2><br><table><tr><td>Color / Pattern: </td><td>yellow</td></tr></table></body></table>
PHP code:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$email."\r\n".
'Reply-To: '.$reply_to."\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = "<html><head></head><body><h2>test heading</h2><br><table><tr><td>Color / Pattern: </td><td>yellow</td></tr></table></body></table>";
mail($mail_to, $subject, $message, $header);
I tried sending HTML mails through PHP but nothing is working. I tried two options.
One with file_get_contents:
<?php
$to = 'teas#gmail.com';
$subject = 'Marriage Proposal';
$from = 'support#blabla.com';
// 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();
$message = file_get_contents("email_template.html");
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
And one with HTML in a PHP string:
<?php
$to = 'anthony#gmail.com';
$subject = 'Marriage Proposal';
$from = 'peterparker#email.com';
// 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 .= '<h1 style="color:#f40;">Hi Jane!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</body></html>';
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
The response for both functions is:
Unable to send email. Please try again. Unable to send email. Please
try again.
Can anyone tell me whats wrong?
Just enable the HTML in php mailer
$mail->isHTML(true);
For refrence please see here the example in github
I'm looking for a way to send 2 different email messages with different recipients.
I know that I can send the same message to a list of emails, but what I need is to send one text to certain recipients and other text to other list of emails.
I need this because my message contains approval information (which should only be seen by an admin) and I need to sent at the same time other mail just for telling the user "your request have been sent and will be reviewed".
Is there a function in mail() that can do this?
-As requested-
Unfortunately, PHP's mail() function can only handle this by using seperate mail() functions.
You can send the same email to multiple recipients at the same time, but to send two different messages (and two different subjects) intended for two different recipients requires using two different mail() functions, along with two different sets of recipients/subjects/messages/headers.
For example:
/* send to 1st recipient */
$to_1 = "recipient_1#example.com";
$from = "from_recip#example.com";
$subject_1 = "Subject for recipient 1";
$message_1 = "Message to recipient 1";
$headers_1 = 'From: ' . $from . "\r\n";
$headers_1 .= "MIME-Version: 1.0" . "\r\n";
$headers_1 .= "Content-type:text/html;charset=utf-8" . "\r\n";
mail($to_1, $subject_1, $message_1, $headers_1);
/* send to 2nd recipient */
$to_2 = "recipient_2#example.com";
$from = "from_recip#example.com";
$subject_2 = "Subject for recipient 2";
$message_2 = "Message to recipient 2";
$headers_2 = 'From: ' . $from . "\r\n";
$headers_2 .= "MIME-Version: 1.0" . "\r\n";
$headers_2 .= "Content-type:text/html;charset=utf-8" . "\r\n";
mail($to_2, $subject_2, $message_2, $headers_2);
Just like this.
<?php
//first mail ////
$to = '1stRe#example.com';
$subject = 'the subject';
$message = '1st Message';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
//second mail ////
$to = '2ndRe#example.com';
$subject = 'the subject';
$message = '2nd Message';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
<?php
$to = 'abc#gmail.com';
$subject = 'the subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = file_get_contents('http://domain.com/newsletter.html');
$headers = 'From: xyz#gmail.com' . "\r\n" .
'Reply-To: xyz#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
This in my mail code. Here I am sending my newsletter to my existing customer.
Can anyone help me how can I send/open the newsletter as the page through mail ?
I suggest you use the following, yet you won't be able to use an http:// call, because this goes against security reasons.
By doing so, anyone could include whatever they want from whatever website. The file must reside on your server in order for the following to work.
Making use of the include(), ob_start() and ob_get_clean() functions.
You must use a relative path to the file you wish to include.
<?php
ob_start();
include 'file.xxx';
$message = ob_get_clean();
$to= 'email#example.com';
$subject = 'the subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: email#example.com' . "\r\n" .
'Reply-To: email#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)){
echo "Message sent.";
}
else{
echo "There was a problem. Check your logs.";
}
?>
Footnotes:
I quote jsalonen from a comment: "Keep in mind that CSS support in email readers is (probably intentionally) very limited: campaignmonitor.com/css - so if it works in a regular browser, it may or may not work in an email reader."
Most Email clients such as Google will ignore CSS, so it's best to use inline CSS.
Images:
Another thing you will need to do is, if you are using images, then that's where you will need to use an http:// call to the image location(s).
I.e.: <img src="http://www.example.com/images/image_1.jpg">
if allow_url_fopen is available in the ini file, you can do this:
$html = file_get_contents('http://domain.com/newsletter.html');
$message = $html;
The reason this should work is because most email clients can read emails sent as HTML (as long as you're sending emails as HTML and not just plaintext emails).
You can use echo htmlspecialchars($html); to see what HTML you're sending.
I am sending email using php script and i am setting from address as
$headers = "MIME-Version: 1.0" . "$from" . "\r\n";
$headers.= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
So, the mail coming to my mail address as from support#mysite.com, But i need to set from address as support#gmail.com.But it is not coming with the modified from address. So, please help to solve this problem..
You try this...
<?php
$to = 'nobody#example.com';
$subject = 'Mail Subject';
$message = 'Test';
$headers = 'From: support#gmail.com' . "\r\n" .
'Reply-To: support#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Hop this will works...
Replace header text,
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "X-Mailer: PHP \r\n";
$headers .= "From: YourName <support#gmail.com>";
To get more information about mail function
may this help you.