0

I trying to send an email from php using php mail() function. Each time when I try to send an email it displays the error message.I don't know what went wrong.Here is my code. Can anyone tell me what went wrong ?

$message = "<html><body>";
    $message .= "<table rules='all'>";
    $message .= "<tr><td><strong>Name: Ramya</strong> </td></tr>";
    $message .= "<tr><td><strong>Email: ramyaroy</strong> </td></tr>";
    $message .= "</table>";
    $message .= "</body></html>";       

    $to = 'ramya@example.com';
    $email='vinay@example.net';

    $subject = 'Website Change Reqest';

    $headers = "From:".$email.PHP_EOL;
    $headers .= "Reply-To:".$email.PHP_EOL;
    $headers .= "MIME-Version: 1.0".PHP_EOL;
    $headers .= "Content-Type: text/html; charset=ISO-8859-1".PHP_EOL;

    if (mail($to, $subject, $message, $headers)) {
      echo 'Your message has been sent.';
    } else {
      echo 'There was a problem sending the email.';
    }
Pranav MS
  • 2,235
  • 2
  • 23
  • 50
Ramya Roy
  • 229
  • 4
  • 10

2 Answers2

0

There's no error in the code.

The problem could be because of your hosting. Usually, hosting account will allow to send a mail from only that domain account.

Change the $email to vinay@[your_domain_name].com

Let me know after you try this.

0
<?php
/*
    Below code works on live and local both server , also on SSL
    which I describe all options on comments
    you need to set SMTP server username and password ,
    this is same as your google email id and password

*/

/* You need to download php-mailer class 
 https://github.com/PHPMailer/PHPMailer */

define('SMTP_USERNAME','your smtp username');
define('SMTP_PASSWORD','password');
define('FROM_EMAIL','from email');
define('ADMIN_EMAIL','replay to email');
define('SITE_NM','your site name');

print_r(sendEmail('john.doe@gmail.com','Lorem Ipsum','anything....'));

function sendEmail($to, $subject, $message) {

    require_once("class.phpmailer.php");

    // create a new object

    $mail = new PHPMailer(); 

    // enable SMTP
    $mail->IsSMTP();

    // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPDebug = 0;

    // authentication enabled
    $mail->SMTPAuth = true;

    //mail via gmail
    //$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
    //$mail->Host = "smtp.gmail.com";
    //$mail->Port = 465; // or 587
    //mail via hosting server
    $mail->Host = SMTP_HOST;

    if (SMTP_HOST == 'smtp.gmail.com') {
        $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
        $mail->Port = 465; // or 58
    }else{
        $mail->Port = 587; // or 58
    }

    $mail->IsHTML(true);
    $mail->Username = SMTP_USERNAME;
    $mail->Password = SMTP_PASSWORD;
    //$mail->SetFrom(SMTP_USERNAME);
    $mail->SetFrom(FROM_EMAIL);
    $mail->AddReplyTo(ADMIN_EMAIL, SITE_NM);
    $mail->Subject = $subject;
    $mail->Body = $message;
    $mail->AddAddress($to);
    // to add bcc mail list
    $mail->AddBCC("example@gmail.com", "your name");
    $mail->AddBCC("example2@gmail.com", "XXX XXXXX");
    $result = true;
    if (!$mail->Send()) {
        //echo "Mailer Error: " . $mail->ErrorInfo;
        $result = false;
    }
    return $result;
}



?>