Can we send HTML emails using PHP? [duplicate] - php

This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 7 years ago.
I have been working on a email script and it works. Prob is I do it using a textarea . can I send an HTML email?if so , how?
Edit: Found the answer

Ofcourse you can. You have to set headers for that
$to = 'abc#example.com';
$subject = 'My Subject';
$message='<b>test</b>';
$headers = "From: test#test.com\r\n";
$headers .= "Reply-To: test#test.com\r\n";
$headers .= "CC: test2#test.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,$message,$headers);

Related

PHP fails to send UTF-8 Messages [duplicate]

This question already has answers here:
How to send UTF-8 email?
(3 answers)
Closed 5 years ago.
I have the following PHP code:
<?php
$to = 'info#neatgr.cf';
$subject = date("d/m/Y");
$message = 'Hey 123 !## αβγ';
$headers = "From: testsite < mail#testsite.com >\n";
$headers .= "Cc: testsite < mail#testsite.com >\n";
$headers .= "X-Sender: testsite < mail#testsite.com >\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path: mail#testsite.com\n"; // Return path for errors
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
mail($to,$subject,$message,$headers);
echo "<script type='text/javascript'>alert('Message sent! We will get back to you soon!');</script>";
echo "<script>window.location.href = 'http://example.com';</script>";
?>
The mail gets sent fine. The problem is that αβγ (Unicode Characters) doesn't get on the mail recipient's end correctly.
This is what the recipient sees: Hey 123 !## αβγ
This is what he should see: Hey 123 !## αβγ
I've looked everywhere and tried everything, changing headers, converting my string to unicode etc. etc., but nothing has worked. Maybe I'm doing something wrong?
Use the UTF-8 charset header, for example:
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
Goes to:
$headers .= "Content-Type: text/html; charset=UTF-8\n";

Mail sending error PHP, didn't get message [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
This is my code. I spent hours debugging it. I can't see the error. Why didn't I didn't receive the email?
if(isset($_POST['submit'])){
$to = "admin#example.com";
$subject = "Mesej Website";
$from = $_POST['email'];
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "Reply-To: ". strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h3>Mesej Daripada '.$_POST['name'].'</h3>';
$message .= '<div style="margin-top:20px;">'.$_POST['message'].'</div>';
$message = '</body></html>';
mail($to,$subject,$message,$headers) or die('mail sending error');
The in built mail function of PHP needs configuration for sending the email.However, if you are using the third party hosting provider this should be already configured or else you need to configure it in php.ini file.
To avoid these configuration issues you can use the phpmailer library where you can all the parameters easily and in a more efficient way.

HTML content not showing in mail [duplicate]

This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 7 years ago.
I had set up email sending in localhost and using that I can access my gmail account to send mail using php script.
<?php
$to = "example#gmail.com";
$subject = "HTML Mail";
$message = "<html><body>";
$message .= "<b>Hey! How are you today?</b>";
$message .= "<br>Regards";
$message .= "</body></html>";
$headers = "From: myemail#gmail.com\r\n";
mail($to,$subject,$message,$headers);
echo "<h1>MAIL SENT</h1>";
?>
When I send this email using this script, it gives out
<html><body><b>Hey! How are you today?</b><br>Regards</body></html>
Why html type output is not shown?
Hey! How are you today?Regards
Like this in gmail?
Could it be that you are missing headers to specify this is HTML content?
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "MIME-Version: 1.0\r\n";
Edit: As pointed by another answer, both headers are needed.
From the PHP documentation for mail():
// 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";
If these headers are missing, the message content is interpreted as plain text.
Hi for sending email with HTML body you need to specify headers. Something like this
//your code <br>
$headers = "From: myemail#gmail.com\r\n"; <br>
//HTML headers <br>
$headers .= 'MIME-Version: 1.0' . "\r\n"; <br>
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
so after that you can send the email with html body
mail($to,$subject,$message,$headers);
good luck

Output mutiple attatchments with PHP mail [duplicate]

This question already has answers here:
Sending multiple attachment in an email using PHP
(2 answers)
Closed 7 years ago.
I wonder if anyone can help me? I have done a lot of research on outputting multiple attachments using PHP mail but I just cant quite get my upload form working correctly.
Background:
I have a form which allows you to upload multiple images. The form itself is fine and i have had working with one one image no problem. The form sends a e-mail with an attachment. The problem has arisen when trying to send multiple attachments.
I can only ever get it to send one.
I have a piece of code below which deals with going through the array of files and adding them to headers of the e-mail. (see Below)
$headers = "MIME-Version: 1.0\r\n";
$headers = "X-Mailer: PHP/" . phpversion()."\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";
$headers .= "From: Sell My Rolex <info#sellmyrolex.today>\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=".md5('boundary2')."\r\n\r\n";
$headers .= "--".md5('boundary2')."\r\n";
$headers .= "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\n";
$headers .= $message."\r\n\r\n";
$x="0";
foreach($files as $file){
$handle = fopen($destination.$file, "r");
$content = fread($handle, filesize($destination.$file));
fclose($handle);
$attachment = chunk_split(base64_encode($content));
$headers .= "Content-Type:".$types[$x]."; ";
$headers .= "name=\"".$file."\"\r\n";
$headers .= "Content-Transfer-Encoding:base64\r\n";
$headers .= "Content-Disposition:attachment; ";
$headers .= "filename=\"".$file."\"\r\n";
$headers .= "X-Attachment-Id:".rand(1000,9000)."\r\n\r\n";
$headers .= $attachment."\n\n";
}
This it is sent out
$message = wordwrap($message, 70);
$mailSent = mail($to, $subject, $message, $headers);
However all i get in the e-mail above the main e-mail message is
Content-Type:image/png; name="image1.png"
Content-Transfer-Encoding:base64
Content-Disposition:attachment; filename="image1.png"
X-Attachment-Id:6299
--f2b57013356e3f84c2c6b1d9b77f95d2--Content-Type:image/png; name="cleanoffice.png"
Content-Transfer-Encoding:base64
Content-Disposition:attachment; filename="cleanoffice.png"
X-Attachment-Id:5833
Can anyone point me in the right direction? I know people may want to suggest using a library like PHPmailer rather than the basic php mail but i want to use php mail to get this complete.
Any help is much appreciated.
Thanks
You need to include multiple boundaries to mark different attachments. The following gives you a good example of how it would look like.
http://www.qcode.co.uk/post/70

php mail function is not working on online server [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
i want to send a mail the code is given below. i cant understand waht is happening . It works on localhost but not on live server.
if (isset($_POST['test_mail'])){
$to = '2606ankit#gmail.com'; //put email address on which mail send
$subject = "Newsletter"; //Put subject of mail here
$from = 'ankit#studiokrew.com'; //put email address from
//email body start
// $body .= file_get_contents('file/'.$filename.'');
$body .= 'anio';
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: '.$from. "\r\n";
//if you need to send cc mail then uncomment below line and change email address
//$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$body,$headers);
}
try this..
<?php if (isset($_POST['test_mail'])){
$host=$_SERVER['HTTP_HOST'];
$replyto="<no-reply >";
$to ='2606ankit#gmail.com';
$subject = "Newsletter";
$from = 'ankit#studiokrew.com';
$headers = "From: \"Invoice\"<noreply#$host>\n";
$headers .= "Reply-To: ".$replyto."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"abc"\"\r\n\r\n";
$headers .= 'From: '.$from. "\r\n";
$header .= "Content-Type:text/html; charset=\"iso-8859-1\"\n";
$body = "This is a multi-part message in MIME format.\r\n";
$body .= "Content-type:text/html; charset=iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= 'anio';
mail($to,$subject,$body,$headers);
}
?>
check the MTA logs on your server.also web server logs the answer will be there somewhere.
Test with a simple mail() script that does not require any external data, just to make sure that your server indeed can send mail. If that fails, you'd have to contact the server admins for help.

Categories