I am trying to send a simple HTML e-mail from PHP. The code below simply results in a blank e-mail in GMail. It also has an empty attachment called 'noname', which is not at all what I want; though that might just be a symptom of it not working.
The code I am using is:
<?php
//define the receiver of the email
$to = '[email protected]';
//define the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: [email protected]\r\nReply-To: [email protected]";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
MIME-Version: 1.0
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b>formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
Use the following script. <? php $to = '[email protected]'; $subject = "Send HTML Email Using PHP"; $htmlContent = ' <html> <body> <h1>Send HTML Email Using PHP</h1> <p>This is a HTMl email using PHP by CodexWorld</p> </body> </html>'; // Set content-type header for sending HTML email $headers = "MIME-Version: 1.0" .
PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters.
HTML doesn't offer an option to create a form that sends an email after submission. However, you can use the form action element to set a mailto: address (in this case, your own), triggering the submitter's mail client to open.
If possible use the PHPMailer class. It will greatly simplify your work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With