I am trying to send an email to user and admin using php-mailer.But when I send the mail, the user Acknowledgement mail is also getting sent to the admin. The e mails getting sent are in proper format. What is the issue here?
My Code is Here:
$name = "User Name";
$email = "user#gmail.com";
$mobile = 'User Phone No.';
$body = "<div><div>Name: <b>$name</b></div> <div>Email: <b>$email</b></div> <div>Mobile: <b>$mobile</b></div></div>";
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require 'PHPMailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail -> isSMTP();
function sendEmail($mail, $from, $password, $to, $body, $subject) {
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail -> SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail -> Debugoutput = 'html';
//Set the hostname of the mail server
$mail -> Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail -> Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail -> SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail -> SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail -> Username = $from;
//Password to use for SMTP authentication
$mail -> Password = $password;
//Set who the message is to be sent from
$mail -> setFrom($from, $from);
//Set who the message is to be sent to
$mail -> addAddress($to, $to);
//Set the subject line
$mail -> Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail -> msgHTML($body);
//Replace the plain text body with one created manually
$mail -> AltBody = 'This is a plain-text message body';
$sucess = $mail -> send();
// //send the message, check for errors
// if (!$sucess) {
// echo "Mailer Error: " . $mail -> ErrorInfo;
// } else {
// echo "Message sent!";
// }
}
$from = "noreplyadmin#gmail.com";
$maiAdmin = "admin#gmail.com";
$password = "password";
$subject = "Enquiry";
// Send Mail to admin
sendEmail($mail, $from, $password, $maiAdmin, $body, $subject);
// Send Mail to User
$userMessage = "We Received your request. Our representative will contact you soon.";
sendEmail($mail, $from, $password, $email, $userMessage, 'Acknowlwdgement');
You are using the same instance and have not cleared the previous out of the PHPMailer $mail instance. You can either clear the old data out of the PHPMailer Instance or do this:
$adminmail = new PHPMailer();
$adminmail -> isSMTP();
$usermail = new PHPMailer();
$usermail -> isSMTP();
sendEmail($adminmail, $from, $password, $maiAdmin, $body, $subject);
$userMessage = "We Received your request. Our representative will contact you soon.";
sendEmail($usermail, $from, $password, $email, $userMessage, 'Acknowlwdgement');
Whatever floats your boat I suppose.
Related
I am going to send different emails to different people.
First email sent normal but second one is waiting for 180 sec and then start to send. I couldn't find any default settings. Once sent an email others are going to POOLING and failed it.
I have different bodies and different subjects.
code 1:
sendEmail(false, $email, $message, $subject, $dep_type);
sendEmail(true, $email, $message_client, $subject_client);
sendEmail function :
function sendEmail($client, $email, $message, $subject, $dep_type = null)
{
$from_mail = 'hello#example.com';
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->Host = 'email-smtp.us-east-1.amazonaws.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->From = $from_mail;
$mail->FromName = "SenderName";
if ($client) {
$mail->addAddress($email);
} else {
$mail->addAddress('welcome#example.com');
}
$mail->addReplyTo($from_mail, 'name');
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
}
https://aws.amazon.com/de/premiumsupport/knowledge-center/ec2-port-25-throttle/
Amazon EC2 throttles traffic on port 25 of all EC2 instances by
default, but you can request for this throttle to be removed.
I can actually send the emails but I want to change the email from name. When I send emails, it comes into the receiver mailbox with my mail id. I want to change that to some other names. Kindly help me to do that. This is my code
<?php
$mailto = $_POST['mail_to'];
$mailSub = $_POST['mail_sub'];
$mailMsg = $_POST['mail_msg'];
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail ->IsSmtp();
$mail ->SMTPDebug = 0;
$mail ->SMTPAuth = true;
$mail ->SMTPSecure = 'ssl';
$mail ->Host = "smtp.gmail.com";
$mail ->Port = 465; // or 587
$mail ->IsHTML(true);
$mail ->Username = "dkdev006#gmail.com";
$mail ->Password = "password";
$mail ->SetFrom("Kreatz.in");
$mail ->Subject = $mailSub;
$mail ->Body = $mailMsg;
$mail ->AddAddress($mailto);
if(!$mail->Send())
{
echo "Mail Not Sent";
}
else
{
echo "Mail Sent";
}
Look at the docs on this method.
You have $mail ->SetFrom("Kreatz.in");, which is neither a "nice" name nor an email address, so I don't know what you're trying to do there. You probably want to do this:
$mail ->setFrom('dkdev006#gmail.com', 'Kreatz.in');
Bear in mind that you're using gmail to send through, so you are not allowed to set arbitrary from addresses, though you can create fixed aliases in gmail prefs.
You're also using an old version of PHPMailer, and have based your code on an obsolete example, so get the latest version.
I am using a code to find the individuals that check in late and send emails to them. also I find those that haven't come at all and send emails to them too. However, it is not working. I get the names and emails correctly, but the $mail object is null, and I don't understand why.
This is my code:
mail_sender.php (this is what i call to send the message)
<?php
function custom_mail($name, $surname, $email, $message, $subject){
//$mail->SMTPDebug = 3; // Enable verbose debug output
require './PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer();
global $mail;
var_dump($mail);
$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 = '****'; // SMTP username
$mail->Password = '****'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = ****; // TCP port to connect to
$mail->From = '****';
$mail->FromName = '****';
$mail->addAddress($email, $name." ".$surname); // Add a recipient
$mail->addCC('****');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = ucwords($name).' '.ucwords($surname).'! <br />'.$message;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//
if(!$mail->send()) {
echo 'email -> '.$email.' name -> '.$name.' surname -> '.$surname.'<br />';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else {
$mail->ClearAllRecipients(); //clears the list of recipients to avoid other people from getting this email
}
}
?>
I think this may be your problem:
$mail = new PHPMailer();
global $mail;
var_dump($mail);
This just doesn't look like a good idea - if you already have a variable called $mail defined globally, it may overwrite your PHPMailer instance, making it null. Change the order to this:
global $mail;
$mail = new PHPMailer();
var_dump($mail);
I don't see that there's much reason to make this available globally at all - if you want to re-use the instance across multiple calls, this won't help - you should be declaring it statically to do that, like this:
static $mail;
if (!isset($mail)) {
$mail = new PHPMailer();
}
var_dump($mail);
I searched web for this issue but none of them solve my problem. I am trying to send single mail using PHPMailer. But I am getting this error in my browser screen.
SMTP -> FROM SERVER:
SMTP -> ERROR: RSET failed:
SMTP -> FROM SERVER:
SMTP -> ERROR: MAIL not accepted from server:
The following From address failed: mymailid#gmail.com : MAIL not accepted from server,,
SMTP server error:
Mailer Error: The following From address failed: mymailid#gmail.com : MAIL not accepted from server
My code to send mail is.
<?php
require_once('mailer/class.phpmailer.php');
date_default_timezone_set('Asia/Kolkata');
$to = $_POST['to'];
$subject = $_POST['subject'];
$msg = $_POST['msg'];
$mail = new PHPMailer();
$mail -> SMTPDebug = 2;
$mail -> IsSMTP();
$mail -> SMTPSecure = 'tls';
$mail -> Host = 'smtp.gmail.com';
$mail -> Port = 587;
$mail -> SMTPAuth = true;
$mail -> Username = 'mymailid#gmail.com';
$mail -> Password = '*********';
$mail -> setFrom("mymailid#gmail.com","Name");
$mail -> addReplyTo("mymailid#gmail.com","Name");
$mail -> Subject = $subject;
$mail -> msgHTML($msg);
$mail -> addAddress($to);
if(!$mail -> send()) {
echo "<h3>Mailer Error: ". $mail-> ErrorInfo . "</h3>";
}
else {
echo "<h1>Email Sent Successfully.</h1>";
}
?>
Please help me to solve this problem. Thanks in advance.
Gmail didn't accept your email and password as correct. You should use real credentials to authentificate or they will block it
I want to send an email in php from my localhost which includes mail.php file.
Here is my code for test.php file:
<?php
include_once("Mail.php");
$From = "Sender's name <testing123xyz#gmail.com>";
$To = "Recipient's name <test2#gmail.com>";
$Subject = "Send Email using SMTP authentication";
$Message = "This example demonstrates how you can send email with PHP using SMTP authentication";
$Host = "mail.gmail.com";
$Username = "testing123xyz";
$Password = "testing";
// Do not change bellow
$Headers = array ('From' => $From, 'To' => $To, 'Subject' => $Subject);
$SMTP = Mail::factory('smtp', array ('host' => $Host, 'auth' => true,
'username' => $Username, 'password' => $Password));
$mail = $SMTP->send($To, $Headers, $Message);
if (PEAR::isError($mail)){
echo($mail->getMessage());
} else {
echo("Email Message sent!");
}
?>
When i hit the url of test.php file, server is throwing following error :
Failed to connect to smtp.gmail.com:25 [SMTP: Failed to connect socket: No connection could be made because the target machine actively refused it. (code: -1, response: )]
Please help me out.
Thanks.
Download and use phpmailer from github:https://github.com/PHPMailer/PHPMailer
Also, check this tutorial, it will help you: http://phpmailer.worxware.com/?pg=tutorial
This is the php code to send emails:
<?php
function email($recipient_email_id,$senders_name){
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
//IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "sender#gmail.com"; // SMTP username
$mail->Password = "xxx"; // SMTP password
$webmaster_email = "reply#gmail.com"; //Reply to this email ID
$email = "$recipient_email_id"; // Recipients email ID
$name = "$senders_name"; // sender's's name
$mail->From = $webmaster_email;
$mail->FromName = $name;
$mail->AddAddress($email, $name);
$mail->AddReplyTo($webmaster_email, "Name");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
}
?>
My guess is that you are using localhost. Try using Test Mail Server
EDIT as of this link, you are using wrong host. Change it to smtp.gmail.com