My Php mail code is like this i want to send html content in mail but i can not send html template with this code
$to = $email;;
$subject = 'New Order Detail';
$message = 'Hi Jane';
$from = 'info#abc.co.in';
// Sending email
if(mail($to, $subject, $message))
{
echo 'Your mail has been sent successfully.';
}
else
{
echo 'Unable to send email. Please try again.';
$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 = '<table border="1" align="center" width="50%">';
$message .= '<tr><td>Test Mail';
$message .= '</td></tr>';
//$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</table>';
$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.';
}
}
This kind of output i get in mail i do not know where is my mistake in this code..
Output
<html><body>
<table border="1" align="center" width="50%">
<tr><td>Test Mail</td></tr>
</table>
</body></html>
There are some errors in your code and also wrong if statement I would like to recommend following script to you for sending email.
$to = $email;
$subject = 'New Order Detail';
$message_text = 'Hi Jane';
$from = 'info#abc.co.in';
$from_name = 'John';
$to_name = 'Smith';
// Sending email
if(!empty($to) && !empty($message_text ))
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' . ucwords($to_name) .' <'.$to.'>' . "\r\n";
$headers .= 'From: ' . ucwords($from_name) . ' <'. $from. '>' ."\r\n";
// Compose a simple HTML email message
$message = '<html><body>';
$message = '<table border="1" align="center" width="50%">';
$message .= '<tr><td>Test Mail';
$message .= '</td></tr>';
$message .= '<p style="color:#080;font-size:18px;">'.$message_text .'</p>';
$message .= '</table>';
$message .= '</body></html>';
// Sending email
mail($to, $subject, $message, $headers);
echo 'Your mail has been sent successfully.';
}else{
echo 'Unable to send email. Please try again.';
}
add proper header contents.
// Compose a simple HTML email message
$message = '<html><body>';
$message = '<table border="1" align="center" width="50%">';
$message .= '<tr><td>Test Mail';
$message .= '</td></tr>';
//$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</table>';
$message .= '</body></html>';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers
$headers[] = 'To: abc <abc#example.com>, xyz <xyz#example.com>';
$headers[] = 'From: ABC <Abc#example.com>';
$headers[] = 'Cc: abc#example.com';
$headers[] = 'Bcc: xyz#example.com';
// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
Set your character set to UTF-8:
$headers .= "Content-type:text/ html;charset=UTF-8" . "\r\n";
That should fix it.
A variation on a theme perhaps and redundant as you have an answer. You should be able to add styles within the style tag region of the below code - or you could potentially add a link to an external css file but that could cause issues.
$to = $email;
$subject = 'New Order Detail';
$message = 'Hi Jane';
$from = 'info#abc.co.in';
$obj=new StdClass;
$obj->success='Your mail has been sent successfully.';
$obj->fail='Unable to send email. Please try again.';
$obj->nl="\r\n";
$status = #mail( $to, $subject, $message );
if( $status ) {
exit( $obj->success );
} else {
$headers = array();
$html = array();
$headers[]="MIME-Version: 1.0";
$headers[]="From: {$from}";
$headers[]="Reply-To: {$from}";
$headers[]="X-Mailer: PHP/". phpversion();
$headers[]="MIME-Version: 1.0";
$headers[]="Content-type: text/html; charset=iso-8859-1";
$html[]="";
$html[]="<html>";
$html[]="<head>";
$html[]="<title>{$subject}</title>";
$html[]="<style>";
$html[]="p{color:red;font-size:18px;}";
$html[]="table{border:1px solid black;width:50%;display:table;}";
$html[]="td{text-align:center;background:yellow;color:blue;}";
$html[]="</style>";
$html[]="</head>";
$html[]="<body>";
$html[]="<table>";
$html[]="<tr><td>Test Mail</td></tr>";
$html[]="<tr><td><p>Will you marry me?</p></td></tr>";
$html[]="</table>";
$html[]="</body>";
$html[]="</html>";
$status = #mail( $to, $subject, implode( $obj->nl, $html ), implode( $obj->nl, $headers ) );
exit( $status ? $obj->success : $obj->fail );
}
Related
I am using postfix to send an email to the user, but the problem is it breaks the words where it finds the space.
Here is the screenshot:
postfix-send-email
PHP code to send an email:
<?php
$subject = "Status Of mail";
$message = "Test Email using Postfix Apache2";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: 'The Travel Worthy' 'pathik#gmail.com"\r\n";
$send = mail('test#yahoo.com', $subject, $message, $headers);
if($send)
{
return 1;
}
else
{
return 0;
}
?>
Try replacing
$headers .= 'From: 'The Travel Worthy' 'pathik#gmail.com"\r\n";
with
$headers .= "From: The Travel Worthy <pathik#gmail.com>\r\n";
<?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: '.$user_email."\r\n". 'Reply-To: '.$user_email."\r\n" . 'X-Mailer: PHP/' . phpversion(); // Compose a simple HTML email message
$message = '<html><body>'; $message .= '<h3 style="color:#f40;">Email:<?php echo $from;?></h3>'; $message .= '<h3>Phone:<?php $phone;></h3>'; $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.';
}
?>
$message = '<html><body>';
$message .= '<h3 style="color:#f40;">Email:'.$from.'</h3>';
$message .= '<h3>Phone:'.$phone.'</h3>';
$message .= '</body></html>'; // Sending email
If Its a PHP script, then you can use any PHP function including echo.
Clean way to do this -
echo '<h3>'.$phone.'</h3>';
I am trying to add my companies logo inside of a php email that I will be sending out to customers after they order. However, it is not working. The actual link to my image is a public url.
What am I doing wrong?
$logoImage = 'https://example.com/images/BFBlogo1.gif';
// Prepare the Email
$to = $email;
$subject = 'Your Example order'. $AuthorrizeResponseText; transaction Id or invoice #, however you set it up as.
$message = '<img src="'.$logoImage.'">';
$message = 'Thank you for ordering with us! ';
$from = "auto-confirm#example.com";
$cc = "order-receipts#example.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";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Cc: '.$cc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
My second attempt:
$message = '<img src="'.$logoImage.'">';
$message .= 'Thank you for ordering with us! ';
You forgot to concate:
$message = '<img src="'.$logoImage.'">';
$message = 'Thank you for ordering with us! '; // here $message is overwrtting.
Should be like this:
$message = '<img src="'.$logoImage.'">';
$message .= 'Thank you for ordering with us! ';
Update
logoImage = 'https://example.com/images/BFBlogo1.gif';
// Prepare the Email
$to = $email;
$subject = 'Your Example order'. $AuthorrizeResponseText;
$message = '<img src="'.$logoImage.'">';
$message .= 'Thank you for ordering with us! ';
$from = "auto-confirm#example.com";
$cc = "order-receipts#example.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";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Cc: '.$cc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
You set the image tag:
$message = '<img src="'.$logoImage.'">';
But then on the very next line you overwrite it:
$message = 'Thank you for ordering with us! ';
Maybe you meant to concatenate instead?:
$message .= 'Thank you for ordering with us! ';
Please note also that if nearly the entire body of the message is an image then I wouldn't be at all surprised if the mail is treated as spam by almost any system that looks at it.
I am not receiving emails from this contact form, but the message seems to sending okay and it also redirects me to the sent page.
I don't have access to the server only via FTP.
PHP
<?php
$to = 'test#gmail.com';
$subject = $_POST['subject'];
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$body = <<<EMAIL
<html>
<p><h3>Email Submited From Website.</h3></p>
<p><strong>Name:</strong> $name</p>
<p><strong>Email:</strong> $email</p>
<p><strong>Subject:</strong> $subject</p>
<p><strong>Message:</strong> $comment</p>
</html>
EMAIL;
// 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: test email' . "\r\n";
$headers .= 'From: <website#mt.co.uk>' . "\r\n";
//$headers .= 'Cc: noreply#example.com' . "\r\n";
//$headers .= 'Bcc: noreply#example.com' . "\r\n";
if ($_POST['submit']){
mail($to, $subject, $body, $headers);
header ("Location: message-sent.php");
die();
} else {
header ("Location: message-failed.php");
die();
}
?>
Check if mail is actually being sent:
if (mail($to, $subject, $body, $headers)===false) {
echo "Not sent!";
} else {
echo "Sent!";
}
Change this:
$headers .= 'To: test email' . "\r\n";
$headers .= 'From: <website#mt.co.uk>' . "\r\n";
To this:
$headers .= "To: $to <test email>\r\n";
$headers .= "From: website#mt.co.uk <website#mt.co.uk>\r\n";
Also, you need to sanitize the subject and body of the email so that the email arrives, but this will usually be reflected in the results after email() reports a success, in that case the email will bounce, go to the spambox, or simply be refused.
If your hosting provider doesn't have an email server, you could try to use a free email server and phpMailer. https://github.com/PHPMailer/PHPMailer
Here is the code:
$email_body = "You have received a new message. ".
" Here are the details:\n\n Name: $name \n Email: $email_address \n Message \n $message";
I want to add bold text to: "Name:", "Email:", and "Message"
I have tried using the "< b > name: < /b>" (without space of course), but it doesn´t work. I want the text to be bold when i receive the 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";
$email_body = "You have received a new message. ".
" Here are the details:\n\n <strong>Name:</strong> $name \n <strong>Email:</strong> $email_address \n Message \n $message";
// Mail it
mail($to, $subject, $message, $headers);
You need To Set Header Content Type Like This:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Here is Example ....
<?php
$to = 'recivermail#email.com';
$subject = 'Your Subject';
$from = 'sender#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;">Your Title</h1>';
$message .= '<b style="color:#080;font-size:18px;">How Are You?</b>';
$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.';
}
?>