Php mailer sending mail but not received - php

I am using this code. It gives me reply mail sent successfully but the mail not received at the address?
function sendmail($to,$subject,$message) {
$mail = new PHPMailer; // call the class
$mail->IsSMTP();
$mail->Host = 'smtp.pepipost.com'; //Hostname of the mail server
$mail->Port = 587; //Port of the SMTP like to be 25, 80, 465 or 587
$mail->SMTPAuth = true; //Whether to use SMTP authentication
$mail->Username = 'mahadev'; //Username for SMTP authentication any valid email created in your domain
$mail->Password = 'Arena#123'; //Password for SMTP authentication
$mail->AddReplyTo("info#pepisandbox.com", "Sudam Wanve"); //reply-to address
$mail->SetFrom("info#pepisandbox.com", "Sudam SMTP Mailer"); //From address of the mail
// put your while loop here like below,
$mail->Subject = "Test MAil"; //Subject od your mail
$mail->AddAddress("imranq031#gmail.com"); //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
//Attach a file here if any or comment this line,
$send = $mail->Send(); //Send the mails
echo $send;
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;
}
}

Related

SMTP Error: Could not authenticate. phpmailer

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.

PHPMailer: Don't understand the from address

I'm trying to learn PHPMailer and I don't understand why it needs an email address and password.
$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
It's not coming from a persons email, it's coming from the site. What email am I supposed to put here and why?
The email address is the address which the emails are sent from. The username and password are required for SMTP servers that require authentication in order to send mail.
The way email works is that party 1 has an email account which it uses to send mail to party 2. Party 1 can be a site, a person, a script or something else, but it still needs to have an email account to be able to send email.
This is the way SMTP (Simple Mail Transfer Protocol) has been defined, and you need to follow the definitions of that protocol to get your mail delivered.
It is not necessary to provide password -
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.example.com"; // SMTP server
$mail->From = "example#gmail.com";
$mail->AddAddress("example#gmail.com");
$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.';
}
?>
Click here

How to send email with SMTP in php

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.

PHP MAILER - EMAIL SENDING

I want to send a confirmation mail to an user who is trying to register. Iam using PHP Mailer for sending mails. Here is my code:
require_once("PHPMailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;// turn on SMTP authentication
$mail->Username = "root";// SMTP username
$mail->Password = "";// SMTP password
$mail->SetLanguage("en","PHPMailer/language");
//compose mail
$mail->From = "admin#localhost.com";
$mail->FromName = "Cinema Admin";
$mail->AddAddress($_POST['email']);
$mail->AddReplyTo("admin#localhost.com", "Cinema Admin");
$mail->Subject = 'Your confirmation link here';
$mail->Body = "Your confirmation link\r\n";
$mail->Body .= "Click on this link to activate your sccount\r\n";
$mail->Body .= "http://localhost/user.login_plugin/confirm.php?passkey=$confirm_code";
//send mail
if (!$mail->Send())
{
echo "Message was not sent <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
When I execute the file,it says
Message was not sent
Mailer Error: SMTP connect() failed.
Could anyone suggest me for sending mails from localhost to the registered user's emailid.
I had searched with few titles regarding "ending emails" in stack overflow, but couldn't find any solution from any posts.
Thank You in advance
Could you try remove the smtp part? From your comment , you do not have mail server setup.
$mail = new PHPMailer();
$mail->SetLanguage("en","PHPMailer/language"); // remove smtp things
You must need a SMTP details in order to send an email using SMTP, For example if you want to setup SMTP using gmail,
$mail->IsSMTP(); // telling the class to use SMTP
$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
If you don't want use SMTP, you can called default mail function
$mail->IsMail(); //Sets Mailer to send message using PHP mail() function.

Sending PHP emails via gmail account having non gmail domain

I have been trying to send PHP emails via gmail account having non gmail domain but the email address is registered at gmail.com. The emails are not going via this account but when I give an account having #gmail.com, then the email is transmitted but it is not sending to non gmail email addresses. The code I have been using is php mailer code.
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // 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->IsHTML(true);
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
Can someone suggest what am I doing wrong.
Try use host:
$host = "ssl://smtp.gmail.com";
You should register the application who sends the mail in your gmail account:
Start session in your browser in the gmail account.
In the same browser with the session opened, go to https://accounts.google.com/b/0/DisplayUnlockCaptcha
Send a mail executing your php in the desired server you want to enable.

Categories