Question Detail

Sending mail through SMTP in php

6 years ago Views 1718 Visit Post Reply

require_once "Mail.php"; $from = "Sandra Sender "; $to = "Ramona Recipient "; $subject = "Hi!"; $body = "Hi,\n\nHow are you?"; $host = "tls://smtp.googlemail.com"; $port = "587"; $username = "ravi@gmail.com"; $password = "*****"; $headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject); $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo("

" . $mail->getMessage() . "

"); } else { echo("

Message successfully sent!

"); }


Thread Reply

Anonymous

- 6 years ago

Try this code : 

// Pear Mail Library
require_once "Mail.php";

$from = '<fromaddress@gmail.com>';
$to = '<toaddress@yahoo.com>';
$subject = 'Subject Here';
$body = "Hi,\n\nHow are you? This is my Message";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'ravi@gmail.com',
        'password' => 'xxxxxx'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}

Anonymous

- 5 years ago

Send email via SMTP server in CodeIgniter

At first, include the CodeIgniter email library. Now specify the SMTP host (smtp_host), port (smtp_port), email (smtp_user), and password (smtp_pass) in SMTP configuration ($config) as per your SMTP server.

//Load email library
$this->load->library('email');

//SMTP & mail configuration
$config = array(
    'protocol'  => 'smtp',
    'smtp_host' => 'ssl://smtp.example.com',
    'smtp_port' => 465,
    'smtp_user' => 'email@example.com',
    'smtp_pass' => 'email_password',
    'mailtype'  => 'html',
    'charset'   => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");

//Email content
$htmlContent = '<h1>Sending email via SMTP server</h1>';
$htmlContent .= '<p>This email has sent via SMTP server from CodeIgniter application.</p>';

$this->email->to('recipient@example.com');
$this->email->from('sender@example.com','MyWebsite');
$this->email->subject('How to send email via SMTP server in CodeIgniter');
$this->email->message($htmlContent);

//Send email
$this->email->send();

Anonymous

- 5 years ago

The following example code help to send HTML email in CodeIgniter using your Gmail account. You only need to specify your Gmail email address (smtp_user) and password (smtp_pass).

//Load email library
$this->load->library('email');

//SMTP & mail configuration
$config = array(
    'protocol'  => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'user@gmail.com',
    'smtp_pass' => 'gmail_password',
    'mailtype'  => 'html',
    'charset'   => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");

//Email content
$htmlContent = '<h1>Sending email via SMTP server</h1>';
$htmlContent .= '<p>This email has sent via SMTP server from CodeIgniter application.</p>';

$this->email->to('recipient@example.com');
$this->email->from('sender@example.com','MyWebsite');
$this->email->subject('How to send email via SMTP server in CodeIgniter');
$this->email->message($htmlContent);

//Send email
$this->email->send();

If you notice that the emails are sent to the spam folder, use Encrypt Class in CodeIgniter to solve this issue in Gmail. You need to load the CodeIgniter Encrypt library before sending the email. It will encrypt your email and help to avoid the spamming issue in Gmail.

$this->load->library('encrypt');