I have a form I've created, and on completion they are asked to select person they want it emailed to from a drop down list.
My issue is how do I add that variable to the $mailer.
right now it is written like this
$mailer -> AddAddress('email#email.com','First Last');
how do i get my variable in there
$mailer -> AddAddress($emailAddress) - Doesn't work.
I've also tried
"'"$emailAddress"'" - this gives me - Invalid address: 'email#email.com' which is frustrating since that's the format it is looking for.
Thanks, let me know
here is the full code that I am using to call the emails
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$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->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
The code given below works perfectly for me.
$mail->AddAddress($_POST['email']); // Pass the value from html form directly with the phpmailer.
Try doing an
var_dump($emailAddress);
right before the ->AddAddress() call and see what comes out. If you're doing this within a function, it's possible you've not passed $emailAddress in as a parameter, of forgotten to make it global;
As well, don't surround the email address with double quotes. It's not necessary:
$emailAddress = 'email#email.com'; // correct
$emailAddress = "email#email.com"; // correct
$emailAddress = '"email#email.com"'; // incorrect
$emailAddress = "\"email#email.com\""; // incorrect.
If $emailAddress came from a POST, use stripslashes around the value.
Make sure the select box has the correct value in the markup (check your view source).
As suggested, echo the variable to check it.
I got it to work, there was an issue in my values.
Actually there were a couple.
Lets just saying some spelling was incorrect.
Thanks for all the info though!
Try strval($emailAddress), worked for me.
Related
I am trying to send two different emails to two different recipients using PHPmailer but only the second email is arriving.
My code:
/**
* This code shows settings to use when sending via Google's Gmail servers.
*/
//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();
//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';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//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 = "olaozias#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('olaozias#gmail.com', 'Department of Information Science');
//Set an alternative reply-to address
$mail->addReplyTo('olaozias#gmail.com', 'Department of Information Science');
//Set who the message is to be sent to
$mail->addAddress($email , 'Parent');
//Set the subject line
$mail->Subject = 'Student Attendance System';
//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(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->Body = 'Dear Parent \r\n This email is sent from the university of gondar , Department of information science to inform you that your child '. $firstname.' has been registered for semester '.$semister. ' in order to see your child attendance status and to communicate easily with our department use our attendance system. First download and install the mobile application which is attached in this email to your phone and use these login credentials to login to the system \r\n Your child Id: '.$student_no. '\r\n Password: '.$parent_pass.'\r\n Thank you for using our attendance system \r\n University of Gondar \r\n Department of Information Science ';
//Attach an image file
//$mail->addAttachment('AllCallRecorder.apk');
$mail->send();
$mail->ClearAddresses();
$mail->AddAddress($stud_email,'Student');
$mail->Subject = 'Student Attendance System';
$mail->Body = "email 2";
//send the message, check for errors
if (!$mail->Send()) {
//echo "Mailer Error: " . $mail->ErrorInfo;
echo '
<script type = "text/javascript">
alert("Mailer Error: " . $mail->ErrorInfo);
window.location = "student.php";
</script>
';
} else {
echo '
<script type = "text/javascript">
alert("student Added successfully and an Email the sent to email address provided");
window.location = "student.php";
</script>
';
//echo "Message sent!";
}
the second email is delivered successfully but the first one is not.
There are a couple of different possibilities. The fact that the second one is sending properly is a good indication that your code is working in general. Focusing on the first one, I'd suggest three things:
Add error checking to the first send() call. You have if (!$mail->Send()) {... on the second one, but you aren't checking the first one. You can use $mail->ErrorInfo as you have in a comment in the second part. (By the way, the $mail->ErrorInfo you have in the script tag will not work. Variables in single quoted strings like that will not be parsed, so you'll just get the literal string "$mail->ErrorInfo" there if there is an error.)
Add error checking to the first addAddress() call. PHPMailer will give you an error that you can check if the email address is invalid for some reason. As far as the code you've shown here, $email appears to be undefined, but so does $stud_email and you've said that one is working properly, so I assume those are both defined somewhere before the code that you've shown here, but a possible cause for this is that $email is undefined or doesn't have the value you expect it to.
The email is being sent, but not received. It's pretty easy for a message to be mis-identified as spam at multiple points between the sender and the receiver. This is more difficult to diagnose, but if you add the error checking to the first send() call and don't get any errors, you'll at least be able to rule that out as a point of failure.
you can do an array with de emails and subject.
$recipients = array(
'person1#domain.com' => 'Person One',
'person2#domain.com' => 'Person Two',
// ..
);
I got a contact us page using PHP. Like all contact us, after someone clicking send, it will send a warning to my email.
The problem is it doesn't work. The script works perfectly in my localhost, it doesn't work only on the server and it doesn't show any error.
$default_path = get_include_path();
set_include_path(dirname(__FILE__)."/../");
require_once("extensions/PHPMailer/class.phpmailer.php");
set_include_path($default_path);
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->SMTPSecure="ssl";
$mail->Host="smtp.gmail.com";
$mail->SMTPDebug =0;
$mail->Port=465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = Yii::app()->params['sender_email']; // SMTP username
$mail->Password = Yii::app()->params['sender_password']; // SMTP password
$webmaster_email = Yii::app()->params['webmaster_email']; //Reply to this email ID
$mail->From = $email_address;
$mail->FromName = "Webmaster";
$mail->AddAddress($email_address,"");
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 70; // set word wrap
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = $mailcontent->subject;
$mail->Body = $mailcontent->body;
//$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
$mail->Send();
I think it's because of server misconfiguration(this is my first time settings a server), but i didn't know what i did wrong.
The firewall already set to allow every traffic, so it's not firewall problem.
No error and no results makes me very confused.
Okay, after I checked everything, it turns out google block my email access from the server because of suspicious activity.
To unblock this, you need to visit this page : https://accounts.google.com/DisplayUnlockCaptcha
and then run the script again.
I am using php mailer class. I am getting :
SMTP Error: The following recipients failed: s_deshmukh88#hotmail.com
$mail = new phpMailer();
$body = "Hello, this is a test mail.";
//$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->IsSMTP(); // tell the class to use SMTP
//$mail->SMTPAuth = true; // enable SMTP authentication
//$mail->Port = 25; // set the SMTP server port
$mail->Host = "localhost"; // SMTP server
$mail->Username = "localhost"; // SMTP server username
$mail->Password = "password"; // SMTP server password
//$mail->SMTPSecure = "tls";
//$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("name#domain.com","First Last");
$mail->From = "name#domain.com";
$mail->FromName = "First Last";
$to = "s_deshmukh88#hotmail.com";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
if($mail->Send()){
echo 'Message has been sent.';
}
What can be the reason ?
Just try to put SMTPAuth to false and Host to'localhost'.
Thanks !! :)
I had this problem using authenticated SMTP. When I tested my mailing from home using xampp but connected to a (shared) server, emails went to users without problems. When I tried the same thing with the PHP on the host server, emails went through to addresses on my account but NOT to external addresses. (Ie. if my address is fred#bloggs.com and I send an email to fred#bloggs.com, it goes through but if I send it to jim#smith.com, it fails) The solution turned out to be to use the full host address when working on my local pc, ie $mail->Host = "mail.fred.bloggs.com", but $mail->Host = "localhost" on the server. May seem perverse but it works.
I just ran into this issue, and in my case, i believe it was the webhost's mail server that was not allowing a non-matching domain name for the From address. So for example, i was using mail.mydomain.com to send mail, but i wanted the from address to be test#test.com
When i switched the from address to test#mydomain.com, then the email was getting sent.
Using PHPMailer to send individual emails to recipients I'm getting nothing in the To: field when I add $mail->SingleTo = TRUE; to my code.
When I remove $mail->SingleTo = TRUE; I receive emails with an email address in the To: field that is correct.
This is what I get:
reply-to xxxxxx <xxxx#xxxx.com>, No Reply <no-reply#no-reply.com>
to
date Mon, Mar 21, 2011 at 5:07 PM
subject Testing
mailed-by gmail.com
signed-by gmail.com
(where xxxxxxx represents my email address.)
Here's my code:
if(isset($_POST['submit']))
{
require_once('PHPMailer_v5.1/class.phpmailer.php');
$mail = new PHPMailer();
$subject = $_POST['subject'];
$body = $_POST['emailbody'];
$to = $_POST['to'];
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "localhost"; // 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 = "SSL"; // 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 = "xxxxxxxxxx#gmail.com"; // GMAIL username
$mail->Password = "*********"; // GMAIL password
$mail->SetFrom('xxx#xxx.com', 'XXXXXX');
$mail->AddReplyTo("no-reply#xxxxx.com","No Reply");
$mail->Subject = $subject;
// After adding this line I'm getting an empty To: field
$mail->SingleTo = TRUE;
$mail->AddAddress("address1#xxxxxx.com", 'xyz abc');
$mail->AddAddress("address2#xxxxxx.com", 'abc xyz');
//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
if(!$mail->Send()) {
$message= "Mailer Error: " . $mail->ErrorInfo;
}
else
{
$message= "Message sent!";
}
}
You are using SMTP to send email. The phpmailer class is not using the SingleTo parameter when senting using Smtp.
More, if you see the CreateHeader function when the SingleTo == true the recipents are only aded to $this->SingleToArray and not to the header itself so basically it PHPmailer bug.
Looks like the only choice, unless you want to patch phpmailer, is to send email one-by-one without using SingleTo property
the solution will be
function & prepare_mailer($subject, $body) {
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "localhost"; // 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 = "SSL"; // 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 = "xxxxxxxxxx#gmail.com"; // GMAIL username
$mail->Password = "*********"; // GMAIL password
$mail->SetFrom('xxx#xxx.com', 'XXXXXX');
$mail->AddReplyTo("no-reply#xxxxx.com","No Reply");
$mail->Subject = $subject;
$mail->MsgHTML($body);
return $mail;
}
foreach( $_POST['to'] as $to ){
$mail = null;
$mail = & prepare_mailer($_POST['subject'],$_POST['body']);
$mail->AddAddress($to['address'], $to['name']);
$mail->Send();
}
Set up all the other parameters, then in a loop set the recipient, send the email, then use the ClearAllRecipients() function. Like so:
{ // loop start
$mail->AddAddress($person_email, $person_name);
$mail->Send();
$mail->ClearAllRecipients();
} // loop end
The problem is in the SmtpSend function (starting at line 701 in class.phpmailer.php). As you can see there, they don't take the singleTo setting into account, and just add all recipients and send the body of the message once.
So you should add some logic there to test if singleTo is true, and if it is modify the code so it sends these mails separately, prefixing the To: header to the body of the message. If singleTo is false you can call the code as-is (line 713 - 758).
Or alternatively, if you don't want to patch things, then you could use a transport method (ie. sendmail) that does seem to support the singleTo parameter.
$mail->AddAddress()
doesn't like CSV's
so if you have:
$Emails="addr1#host.com,addr2#host.net"; #etc;
do a for loop after a split:
$NewList = preg_split("/,/",$Emails);
foreach ($NewList as $k=>$email){
if ($k==0) $mail->AddAddress($email); # Add main to the "To" list.
else $mail->AddCC($email); # The REST to CC or BCC which ever you prefer.
}
-- BF.
SingleTo Is not a good idea. It only works with "sendmail" or "mail" transports, not with SMTP. If you use SingleTo with SMTP, this parameter is just ignored without any error or warning, and you may get duplicates.
Since you use both SingleTo an SMTP, as shown in your code, the SingleTo in your case is ignored.
The SMTP protocol is designed in a way that you cannot send one message to several different recipients, each having only its own address in the TO: field. To have each recipient have only its name in the TO:, the whole message have to be transmitted again. This explains why SingleTo is incompatible with SMTP.
According to the authors of the PHPMailer library, SingleTo is planned to be deprecated in the release of PHPMailer 6.0, and removed in 7.0. The authors have explained that it's better to control sending to multiple recipients at a higher level, since PHPMailer isn't a mailing list sender. They tell that the use of the PHP mail() function needs to be discouraged because it's extremely difficult to use safely; SMTP is faster, safer, and gives more control and feedback. And since SMTP is incompatible with SingleTo, the authors of PHPMailer will remove SingleTo, not SMTP.
I'm trying to do a very simple mail form in PHP but get the error:
PHP Notice: Undefined index: in /var/www/html/sites/send_contact.php on line 10, referer: http://example.com/contact2.php
My PHP file looks like this:
<?php // send_contact.php
// Contact subject
$subject =$_POST["$subject"];
// Details
$message=$_POST["$detail"];
// Mail of sender
$mail_from=$_POST["$customer_mail"];
// From
$name2=$_POST["$name2"];
$header="From: $mail_from\r\n";
// Enter your email address
$to="joe#mail.com";
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
echo "We've recived your contact information";
}
else {
echo "ERROR";
}
?>
The easiest guess is that you're doing a mistake accessing your variables.
instead of:
$name2=$_POST["$name2"];
use this:
$name2=$_POST["name2"];
Or, if you know the difference and are doing this on purpose, make sure your $name2 variable is defined with the correct name of the HTML form field.
As an aside, I would strongly recommend using a library like PHPMailer to send emails.
Your example is quite simple and the mail() should work just fine, but for anything more elaborate (ie. having attachments or html) or needing to send using an external mail server by SMTP (with or without authentication), it will do a much better job and save you lots of time.
For a better idea, check this example from their website:
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$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->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
The error is telling you exactly what is wrong:
Line 10:
$name2=$_POST["$name2"];
You are using '$name2' before it is defined.
PHP can substitute variables in strings:
$var1 = "bar";
echo "foo $var1"; // prints "foo bar"
In your HTML use something similar to the following:
<input type="...whatever..." name="name2" />
Then in PHP, assuming the data was POSTed, you would access it using:
$name2 = $_POST["name2"];
It seems that your code doesn't pass the $_POST data as you expect. I recommend you to check the keys of $_POST as follows:
<?php
print_r(array_keys($_POST));
?>
If it doesn't display the value of $name2, it means you should modify the web page sending form data to send_contact.php.