Send email using the GMail SMTP server from a PHP page
// Pear Mail Library
require_once "Mail.php";
$from = '<fromaddress@gmail.com>';
$to = '<toaddress@yahoo.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'johndoe@gmail.com',
'password' => 'passwordxxx'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
Using Gmail SMTP to send email with PHP
Use this class: http://www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html
The sample code I use:
require("smtp/smtp.php");
require("sasl/sasl.php");
$from = 'youraddress@gmail.com';
$to = 'some@email.com';
$smtp=new smtp_class;
$smtp->host_name="smtp.gmail.com";
$smtp->host_port='465';
$smtp->user='youraddress@gmail.com';
$smtp->password='XXXXXXXXX';
$smtp->ssl=1;
$smtp->debug=1; //0 here in production
$smtp->html_debug=1; //same
$smtp->SendMessage($from,array($to),array(
"From: $from",
"To: $to",
"Subject: Testing Manuel Lemos' SMTP class",
"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
),
"Hello $to,\n\nIt is just to let you know that your SMTP class is working just fine.\n\nBye.\n"));
Use php mailer to send via Gmail - SMTP error
This is possible duplicate of Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587. How to fix?
Probably you didn't allow for less secured apps in Gmail account security dashboard.
Need help sending email in PHP using Gmail SMTP
as per Gmail
policy, You cannot use plain password (house22)
, you have to use encrypted password(Gh45515HGGujKkkHk5554)
, for that you need two way authentication should be enable on your gmail account. and after that, create Application Specific Password and then use that password for your SMTP, I hope that will work for you. here is the link1 Link2
Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587. How to fix?
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email@gmail.com";
$mail->Password = "password";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
This code above has been tested and worked for me.
It could be that you needed $mail->SMTPSecure = 'ssl';
Also make sure you don't have two step verification switched on for that account as that can cause problems also.
UPDATED
You could try changing $mail->SMTP to:
$mail->SMTPSecure = 'tls';
It's worth noting that some SMTP servers block connections.
Some SMTP servers don't support SSL
(or TLS
) connections.
Related Topics
Php Get Number of Week for Month
How to Run Angular App on Xampp Server
How Would I Skip Optional Arguments in a Function Call
SQL Injection That Gets Around mysql_real_escape_string()
Why Shouldn't I Use MySQL_* Functions in PHP
Php Session Lost After Redirect
Build a Tree from a Flat Array in PHP
PHP Code Is Not Being Executed, But the Code Shows in the Browser Source Code
How to Get PHP Errors to Display
Difference Between Single-Quoted and Double-Quoted Strings in PHP
How to Block Direct Access to My JavaScript Files
How to Increase Maximum Execution Time in Laravel
How to Convert Ereg Expressions to Preg in PHP
Regex to Check the String Contains Only Letter and Numbers But Not Only Numbers
How to Add Class to Link in Wp_Nav_Menu
How to Show Checkboxes as Checked When Values Are Set in the Database in Laravel