Sowmiya P Sowmiya P - 3 months ago 13
PHP Question

How to send an email using php

When I execute this code, a new tab is opened. It displays my code. How do I fix this?

<?php

$mail = new PHPMailer();

//Send mail using gmail
if($send_using_gmail){
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "myemailid@gmail.com"; // GMAIL username
$mail->Password = "mypassword"; // GMAIL password
}

//Typical mail data
$mail->AddAddress('myemailid@gmail.com', 'Name');
$mail->SetFrom('myemailid@gmail.com', 'Name');
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";

try{
$mail->Send();
echo "Success!";
} catch(Exception $e){
//Something went bad
echo "Fail :(";
}

?>


I am trying to send an email using PHP here.

Answer
$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");


if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

try this one it worked for me. If didnt work let me know