air
air

Reputation: 6264

Sending Web Page through Email in php

as in asp we have function to send complete web page in email, which basically save lot of time for developer in creating & sending email

see the following code

     <%
    Set myMail=CreateObject("CDO.Message")
    myMail.Subject="Sending email with CDO"
    myMail.From="[email protected]"
    myMail.To="[email protected]"
    myMail.CreateMHTMLBody "mywebpage.html",cdoSuppressNone
    myMail.Send
    set myMail=nothing
    %>

as we know that CreateMHTMLBody will get data from mywebpage.html and send it as a body of email.

i want to know does any function like (CreateMHTMLBody) this is available in php ?

if Not can we crate any function if yes, please give me some hints.

Thanks

Upvotes: 4

Views: 21132

Answers (5)

theking2
theking2

Reputation: 2841

PHPMailer

Or use phpmailer. It has a better change to have the emails not to end up in the spam folder.

Install PHPMailer

  1. Get Composer installed and do composer phpmailer/phpmailer to install the PHPMailer class.
  2. Include the autoloader require "vendor/autoload.php"

Create and send an email

  $mail = new PHPMailer( true );

  try {
    //$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output

    // Server settings
    $mail->isSMTP();
    $mail->Host       = $mail_host;
    $mail->Port       = $mail_port;
    $mail->SMTPAuth   = true;
    $mail->Username   = $mail_username;
    $mail->Password   = $mail_password;
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;

    // Recipients
    $mail->setFrom( $fromEmail, $fromName );
    $mail->addAddress( $to );

    // Content
    $mail->isHTML( true );
    $mail->CharSet = 'UTF-8';
    $mail->Subject = $subject;
    $mail->AddEmbeddedImage( 'path-to-logo-image', 'logo' );
    $mail->Body    = $message;

    $mail->send();
    //LOG->info( "email sent", [ 'to' => $to ] );
  } catch ( Exception $e ) {

    //LOG->critical( "error sending email", [ 'error' => $e->getMessage() ] );
  }

Make sure to set the variables. You should probably sanitize the email addres with filter_var( trim( $email ), FILTER_SANITIZE_EMAIL ); The $message should be string containing a html page where <img src='logo' alt='logo'> could be used to include a png/jpg/gif image.

Upvotes: 0

Johannes Kolletzki
Johannes Kolletzki

Reputation: 29

Use the output buffer functions of PHP and include the desired webpage. Example:

// Start output buffering
ob_start();

// Get desired webpage
include "webpage.php";

// Store output data in variable for later use
$data = ob_get_contents();

// Clean buffer if you want to continue to output some more code
// in which case it would make sense to use this functionality in the very beginning
// of your page when no other code has been processed yet.
ob_end_clean();

Upvotes: 3

ceejayoz
ceejayoz

Reputation: 180075

To add to Erik's answer, if you want to import a local (or remote!) file instead of specifying the HTML in the code itself, you can do this:

// fetch locally
$message = file_get_contents('filename.html');

// fetch remotely
$message = file_get_contents('http://example.com/filename.html');

Upvotes: 4

MetaCipher
MetaCipher

Reputation: 274

Example below:

<?
    if(($Content = file_get_contents("somefile.html")) === false) {
        $Content = "";
    }

    $Headers  = "MIME-Version: 1.0\n";
    $Headers .= "Content-type: text/html; charset=iso-8859-1\n";
    $Headers .= "From: ".$FromName." <".$FromEmail.">\n";
    $Headers .= "Reply-To: ".$ReplyTo."\n";
    $Headers .= "X-Sender: <".$FromEmail.">\n";
    $Headers .= "X-Mailer: PHP\n"; 
    $Headers .= "X-Priority: 1\n"; 
    $Headers .= "Return-Path: <".$FromEmail.">\n";  

    if(mail($ToEmail, $Subject, $Content, $Headers) == false) {
        //Error
    }
?>

Upvotes: 9

Erik
Erik

Reputation: 20722

Here's how:

$to  = '[email protected]';
$subject = 'A test 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";

// Put your HTML here
$message = '<html><body>hello world</body></html>';

// Mail it
mail($to, $subject, $message, $headers);

You've just sent HTML email. To load an external HTML file replace $message = '' with:

$message = file_get_contents('the_file.html');

Upvotes: 1

Related Questions