3

I have php mail without smtp aunthetication and mail server doesn't allow me to send email without using smtp. I have tried many options but I can't fix it; I'm new in php world. Please help me fix it. This is my php mail code:

<?php

/**
 * Sends an e-mail based on a template
 *
 * @param $template_name
 * @param $template_data
 * @param $recipient_mail
 * @param $subject
 * @param bool $is_admin
 * @throws Exception
 * @throws SmartyException
 */
function send_email_template($template_name, $template_data, $recipient_mail, $subject, $is_admin = false){

    global $template;

    $template->assign('mail', $template_data);

    if($is_admin) {
        $body = $template->fetch(ADMIN_TEMPLATE . 'email/' . $template_name);
    }else{
        $body = $template->fetch(CURRENT_TEMPLATE . 'email/' . $template_name);
    }

    $headers = sprintf('From: %s', get_setting('email_from')) . '\r\n';
    $headers  .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    mail($recipient_mail, $subject, $body, $headers);

}

/**
 * Checks if email exists based on given $email
 *
 * @param $email
 * @return bool
 */
function email_exists($email){

    $sql = 'select * from members where members_email = ' . make_safe($email);
    return fetch_row($sql);

}

/**
 * Updates the email code for a member based on their email
 *
 * @param $email
 * @param $email_code
 * @return bool
 */
function update_email_code($email, $email_code){

    global $database;

    try {

        $rs = $database->Execute('select * from members where members_email = ' . make_safe($email));

        $member = array();

        $member['members_email_code'] = $email_code;

        $updateSQL = $database->GetUpdateSQL($rs, $member);
        $database->Execute($updateSQL);

        return true;

    } catch (exception $e) {

        error_log($e);

        return false;

    }

}
Cameron
  • 3,098
  • 1
  • 22
  • 40
Xhulo
  • 31
  • 1
  • 4
  • You tagged this with [PHPMailer](https://github.com/PHPMailer/PHPMailer), so why not use it? It can do SMTP with auth very easily. – Synchro Aug 17 '16 at 11:35

2 Answers2

3

Use this script to send email using SMTP. In this case, we are using Google SMTP server, use this link to learn about Google SMTP https://support.google.com/a/answer/176600?hl=en.

<?php
    ini_set("SMTP", "aspmx.l.google.com");
    ini_set("sendmail_from", "YOURMAIL@gmail.com");

    $message = "The mail message was sent with the following mail setting:\r\nSMTP = aspmx.l.google.com\r\nsmtp_port = 25\r\nsendmail_from = YourMail@address.com";

    $headers = "From: YOURMAIL@gmail.com";


    mail("Sending@provider.com", "Testing", $message, $headers);
    echo "Check your email now....<BR/>";
?>
Synchro
  • 35,538
  • 15
  • 81
  • 104
muya.dev
  • 966
  • 1
  • 13
  • 34
  • Please note that if you are using Google SMTP, you should enable it at your Gmail account. Use this link to learn how to send Email using SMTP http://stackoverflow.com/questions/712392/send-email-using-the-gmail-smtp-server-from-a-php-page – muya.dev Aug 17 '16 at 11:39
0

Below code works for me

require_once('../class.phpmailer.php');

//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$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");

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

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

And if your are using Gmail as your SMTP

Go to your account settings ->https://myaccount.google.com/security?utm_source=OGB&pli=1#connectedapps

click on the "allow less secured app" to ON.

Kaviyarasu Arasu
  • 329
  • 5
  • 13