Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send email with SMTP in php

Tags:

php

email

smtp

I want to send email with SMTP in my project, previously i write php mail() in my project but now my client want that i should use SMTP. I search about this but i get nothing any proper solution for this.

In my php mail() i send name, subject and comment, so how can i do this in SMTP.

Here is my code:

$payer_email = "Your Email";
$subject = "Your Subject";
$message = 'Dear '.$name.',
            Thank you for your purchase from '.$site_url.'. The details of your purchase are below.
            Transaction ID: '.$txn_id.'
            Item Name: '.$item_name.'
            Payment Amount: '.$payment_amount.'
            Payment Amount: '.$payment_status.'
            Paid to: '.$receiver_email.'
            Thanks and Enjoy!';

$headers .= 'From: ' .$from. "\r\n" .'Reply-To: ' .$from . "\r\n";
$headers  .= 'MIME-Version: 1.0' . "\r\n";
$headers  .= "Content-Type: text/html; charset=iso-8859-1 ";

//mail to buyer
mail( $payer_email , $subject, $message, $headers );

Please give me some suggestions or simple and nice tutorials.

like image 522
deemi-D-nadeem Avatar asked Sep 18 '14 09:09

deemi-D-nadeem


People also ask

Does PHP mail function use SMTP?

PHP mailer uses Simple Mail Transmission Protocol (SMTP) to send mail. On a hosted server, the SMTP settings would have already been set. The SMTP mail settings can be configured from “php. ini” file in the PHP installation folder.

Can you use PHP in email?

PHP is a widely used server side scripting language. It provides many built-in functions which we call predefined functions of PHP. PHP provides email support via a built-in mail() function. Using this function, you can easily send emails directly through your PHP script.

Can I send email from localhost PHP?

Send Email from Localhost with PHPSet SMTP credentials (host, username, password, and port). Specify sender name and email ( $mail->setFrom ). Set recipient email address ( $mail->addAddress ). Set email subject ( $mail->Subject ).


1 Answers

Take a look at PHP Mailer:

https://github.com/PHPMailer/PHPMailer

Example from that page:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '[email protected]';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = '[email protected]';
$mail->FromName = 'Mailer';
$mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
$mail->addAddress('[email protected]');               // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
like image 122
Jonathon Avatar answered Oct 05 '22 01:10

Jonathon