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.
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 = 'user#example.com'; // 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 = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$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';
}
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "username"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
Please try this
Create library file for the SMTP Settings 'library.php':
<?php
error_reporting(0);
define("SMTP_HOST", "SMTP_HOST_NAME"); //Hostname of the mail server
define("SMTP_PORT", "SMTP_PORT"); //Port of the SMTP like to be 25, 80, 465 or 587
define("SMTP_UNAME", "VALID_EMAIL_ACCOUNT"); //Username for SMTP authentication any valid email created in your domain
define("SMTP_PWORD", "VALID_EMAIL_ACCOUNTS_PASSWORD"); //Password for SMTP authentication
?>
Make the form post and do the below actions:
<?php
include 'library.php';
include "classes/class.phpmailer.php"; // include the class file name
if(isset($_POST["send"])){
$email = $_POST["email"];
$mail = new PHPMailer; // call the class
$mail->IsSMTP();
$mail->Host = SMTP_HOST; //Hostname of the mail server
$mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587
$mail->SMTPAuth = true; //Whether to use SMTP authentication
$mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain
$mail->Password = SMTP_PWORD; //Password for SMTP authentication
$mail->AddReplyTo("reply#yourdomain.com", "Reply name"); //reply-to address
$mail->SetFrom("from#yourdomain.com", "Asif18 SMTP Mailer"); //From address of the mail
// put your while loop here like below,
$mail->Subject = "Your SMTP Mail"; //Subject od your mail
$mail->AddAddress($email, "Asif18"); //To address who will receive this email
$mail->MsgHTML("<b>Hi, your first SMTP mail has been received. Great Job!.. <br/><br/>by <a href='http://asif18.com'>Asif18</a></b>"); //Put your body of the message you can place html code here
$mail->AddAttachment("images/asif18-logo.png"); //Attach a file here if any or comment this line,
$send = $mail->Send(); //Send the mails
if($send){
echo '<center><h3 style="color:#009933;">Mail sent successfully</h3></center>';
}
else{
echo '<center><h3 style="color:#FF3300;">Mail error: </h3></center>'.$mail->ErrorInfo;
}
}
?>
Please edit your email and password correctly.
You may see the demo and source code on Click here
you can employ the use of the phpmailer library since the mail() function has limitations in its use. so I prefer using PHPmailer it is easier to work with than the mail() function.
I also used the mail() function it requires a lot of settings here and there and cannot be used to send remote mail messages.
so in short, the mail() function is only suitable when working with the local server but not suitable when you want to work with a remote server.
Related
I am trying to send mail using phpmailer but I found that error, My username and password is perfect and also password doesn't contain any special character.
error showing: SMTP Error: Could not authenticate. phpmailer
//------------------------- Mail to company section -----------------
$to='realbantimadrid#gmail.com';
$sender=$_POST['guest_name'];
$mail_id=$_POST['guest_mail'];
$cont_no=$_POST['guest_cont'];
$company=$_POST['guest_comp'];
$msg_txt=$_POST['guest_msg'];
if($sender== '' || $mail_id== '' || $cont_no== '' || $company== '' || $msg_txt== ''){
echo "check the fields";
}else{
$subject='Query from '.$sender;
$message='Dear Sir,<br><br>'.$msg_txt.'<br><br>From: '.$sender.'<br>Contact: '.$cont_no.'<br>Company Name: '.$company;
//---------------------------------- SMTP Authenticated Mail coding --------------------------
include "smtpmail/library.php"; // include the library file
include "smtpmail/classes/class.phpmailer.php"; // include the class name
$email = $to;
$mail = new PHPMailer; // call the class
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com"; //Hostname of the mail server
$mail->Port = 465; //Port of the SMTP like to be 25, 80, 465 or 587
$mail->Username = "mailid#gmail.com"; //Username for SMTP authentication any valid email created in your domain
$mail->Password = "password"; //Password for SMTP authentication
$mail->AddReplyTo($mail_id, $sender); //reply-to address
$mail->SetFrom($email, $sender); //From address of the mail
// put your while loop here like below,
$mail->Subject = $subject; //Subject of your mail
$mail->AddAddress($email, "NYCLD Test Mailer"); //To address who will receive this email
$mail->MsgHTML($message); //Put your body of the message you can place html code here
// $mail->AddAttachment("images/asif18-logo.png"); //Attach a file here if any or comment this line,
$send = $mail->Send(); //Send the mails
if($send)
{
echo "Thank you for your Feedback";
}
}
//------------------------ End of SMTP mail code ---------------------------------------
//--------------------------------------------------------------------
//------------------------- Mail to sender Section -----------------
$to=$mail_id;
$subject='Acknowledgement of Query from '.$sender;
$message='Dear '.$sender.',<br><br>Thank you for your feedback/query. Your mail has been delivered to concerned department. You will be contacted soon reagrding your query.<br><br>Regards,<br><br>Test Department';
//---------------------------------- SMTP Authenticated Mail coding --------------------------
include "smtpmail/library.php"; // include the library file
include "smtpmail/classes/class.phpmailer.php"; // include the class name
$email = $to;
$mail = new PHPMailer; // call the class
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com"; //Hostname of the mail server
$mail->Port = 465; //Port of the SMTP like to be 25, 80, 465 or 587
$mail->Username = "mailid#gmail.com"; //Username for SMTP authentication any valid email created in your domain
$mail->Password = "password"; //Password for SMTP authentication
$mail->AddReplyTo($mail_id, "Fitwell India"); //reply-to address
$mail->SetFrom($email, "Fitwell India"); //From address of the mail
// put your while loop here like below,
$mail->Subject = $subject; //Subject of your mail
$mail->AddAddress($mail_id, $sender); //To address who will receive this email
$mail->MsgHTML($message); //Put your body of the message you can place html code here
// $mail->AddAttachment("images/asif18-logo.png"); //Attach a file here if any or comment this line,
$send = $mail->Send(); //Send the mails
//------------------------ End of SMTP mail code ---------------------------------------
Try to change $mail->SMTPSecure = "tls"; and use as $mail->Port = 587; also do not forget to enable access to your gmail account from outside app, you should receive email invitation to your gmail account about access attempt.
I am using wamp server and I have following code in my final.php. I am also
adding error that appears as below after code as comment kindly check that as.
I am newer to php. I am finding simple code so that I can sent message through my account, but I don't understand why this code do not have option for password.
Is password require? Please help me on this with detail and I also need to send email to multiple users simultaneously.
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->From = "63yogesh#gmail.com";
$mail->AddAddress("11ce028#charusat.edu.in");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
//The following From address failed: 63yogesh#gmail.com Message was not sent.Mailer error: The following From address failed: 63yogesh#gmail.com
SMTP server error: 5.7.0 Must issue a STARTTLS command first. jw8sm18271654pbc.73 - gsmtp
The example here says you have to set several data, such as authentication:
//Username to use for SMTP authentication
$mail->Username = "yourname#example.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
and SMTP port number:
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
EDIT:
This is the Basic Example using SMTP from their official site, try to edit it with your infos
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!";
}
I am having trouble figuring out how to send an email to recipient1 with their information, then an email to recipient2 with their information, recipient3 with their information, and so on, all within the same script.
$date=date("Y-m-d");
$time=date("H:i");
$result=mysql_query("select * from reminder where R_Date='$date' && R_Time='$time'");
date_default_timezone_set( "Asia/Kuala_Lumpur");
$receiver=array();
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
while($row=mysql_fetch_assoc($result)){
if($row){
$mail->From = 'from#example.com';
$mail->FromName = 'CIMB Clicks';
$mail->addAddress($row['R_Email'], $row['R_ID']); // Add a recipient
$mail->addAddress($row['R_Email']); // Name is optional
$mail->WordWrap = 1000; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
$body="Greetings from Clicks!<br><br>".
$row['R_Title'].".<br>".
"This is My Reminder from Clicks regarding ".$row['R_Title'].".<br><br>".
"Thank you & have a good day ahead!<br><br>
$mail->Subject = 'My Reminder from Clicks';
$mail->Body = $body;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
}
}
my Database
R_ID R_Title R_Date R_Time R_Email
1 Top Up 2014/10/15 19:41 email#hotmail.com
2 Transfer 2014/10/15 19:41 email#live.com
Assuming you have an array of E-Mail address you could make a foreach-loop. But your question isnt that detailed.
$recipients = array('peter#anymail.com', 'paul#anymail.com', 'mary#anymail.com');
$content = 'same content';
$subject = 'same subject';
foreach($recipients as $address) {
mail($address, $subject, $content, 'FROM: me#anymail.com');
}
define('SITE_EMAIL', 'example#mydomain.la');
$to1= SITE_EMAIL;
$subject1 = "Contacto Web";
$message1 = 'Hi!';
$headers1 = "MIME-Version: 1.0\r\n";
$headers1 .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers1 .= "From: ".$_POST['name']." <".$_POST['email'].">\r\n";
mail($to1, $subject1, $message1, $headers1);
print "message send!";
I'm using this code to send an email from a contact form. But mostly goes to spam or even the mail is not sent. What they recommended was to validate the header. The form and the php file is in my domain but I use google apps, so I think I have to use the google smtp. But I really don't know how...
I don't think there's an easy way to use mail() with gmail.
Give phpmailer a try.
This is a sample of using it with gmail:
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->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$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!";
}
I have a PHPMailer script, and I've checked all the other threads that are similar to this, and tried all the solutions that follow the question. But here's the error I'm receiving
2014-01-03 02:25:16 SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. Mailer Error: SMTP connect() failed.
And here's my script:
$body = "test";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // I have also tried tls
$mail->SMTPKeepAlive = true;
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // I have also tried 25
$mail->Username = "xxxx#gmail.com"; // GMAIL username
$mail->Password = "xxxxx"; // GMAIL password
$mail->SetFrom("johndoe#gmail.com","John Doe");
$mail->AddReplyTo("johndoe#gmail.com","John Doe");
$mail->Subject = "Booking from " . $_POST['person'] . ' at ' . $_POST['name'];
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "johndoe#hotmail.co.uk";
$mail->AddAddress($address, "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
However I just cannot get it to work. I've tried to many different solutions. I'm even happy to use the default php mail however I just need it to send, the php mail wouldn't even get past the hotmail spam filters (by that I mean it didn't even send it to spam).
Any solutions or suggestions here? Thanks a lot!
Just comment this line:
$mail->IsSMTP();
Did you make sure your php.ini mail settings are the same on both your live server and your localhost server?
Install PHP Mailer From
https://github.com/Synchro/PHPMailer
And Read "A Simple Example"
<?php
require_once('PHPMailer/PHPMailerAutoload.php'); // Load your PHPMailerAutoload.php Correctly Here
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;// debugging: 1 = errors and messages, 2 = messages only // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'yourmail#gmail.com'; // SMTP username
$mail->Password = '**********'; // SMTP password
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // or 587 // TCP port to connect to
$mail->setFrom('fromemail#gmail.com', 'Vijay Rami');
$mail->addAddress('toemail#gmail.com', 'Vijay Rami'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//$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';
}