send smtp email in php code example
Example 1: core php mail function without phpmailer
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
session_start();
$to = '[email protected]';
$subject = 'Subject xxxxx xxxxxx';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=charset=utf-8\r\n";
$headers .= "From: " . $_POST['email'] . "\r\n";
//$headers .= "Reply-To: ". $_POST['email'] . "\r\n";
$headers .= "CC: [email protected]\r\n";
$message = "<html><body>";
$message .= '<table style="border-color: #666; background: #eee; cellpadding="10">';
$message .= "<tr><td><strong>Name:</strong> </td><td>" . $_POST['username'] . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . $_POST['email'] . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
echo $message;
//echo $headers;
$response=mail($to, $subject, $message, $headers);
if($response==1)
{
echo "<script language='javascript' type='text/javascript'>
window.location = 'index.html';
</script>";
}
else{
echo
"<script language='javascript' type='text/javascript'>
alert('mail send failed');
</script>";
}
?>
Example 2: how to send email with php
<?php
var Name = $_POST('name');
// grabbing the data from the form when posted
// make sure to add name="name" or name="email"
// in the input tag to grab the the specific elements
// data example <textarea name="message"> or
// <input type="number name="number">
var Email = $_POST('email');
var Number = $_POST('number');
$to_email = 'Your E-mail';
$subject = 'The Subject of the message';
$message = 'Name'.$name. "email" .$email. "number:" .$number.".";
$headers = 'From: noreply @ company . com'; //optional
mail($to_email,$subject,$message,$headers);
?>
Example 3: smtp in php mail function
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
require_once "Mail.php";
$host = "ssl://smtp.dreamhost.com";
$username = "[email protected]";
$password = "your email password";
$port = "465";
$to = "[email protected]";
$email_from = "[email protected]";
$email_subject = "Subject Line Here:" ;
$email_body = "whatever you like" ;
$email_address = "[email protected]";
$headers = array ('From' => $email_from, 'To' => $to, 'Subject' => $email_subject, 'Reply-To' => $email_address);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $email_body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
Example 4: send mail config using gmail php
Before sending emails using the Gmails SMTP Server, you to make some of the
security and permission level settings under your Google Account Security
Settings.
1. Make sure that 2-Step-Verification is disabled.
2. Turn ON the "Less Secure App" access or click here.
3. If 2-step-verification is enabled, then you will have to create app
password for your application or device.
4. For security measures, Google may require you to complete this additional
step while signing-in. Click here to allow access to your Google account
using the new device/app.
Note*: It may take an hour or more to reflect any security changes
===================================================================
PHP CODE CONFIG :=
$mail->SMTPDebug = 1;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Host = "smtp.gmail.com";
$mail->Username = "[email protected]";
$mail->Password = "your-gmail-password";
=================================================================
For More Info check this link : https://pepipost.com/tutorials/send-an-email-via-gmail-smtp-server-using-php/