Does php mail need configuration to work? - php

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.

Related

sending mail using smtp phpmailer doesn't work

I´m having trouble sending a e-mail with the PHPMailer class, after submit form i have a message mail send ok but i don't recive any mail.
I guess the problem is with the SMTP authentication, but I couldn´t find the problem.the source application are stored in a distant server with ip adress:175.2.3.69 and i use outlook count to send mail
The code with problem is:
require_once('../libs/PHPMailer/class.phpmailer.php');
//Ensuite on débute l'envoi de mail
$mail = new PHPmailer();
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "messagerie.abc.a.fr"; // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "username.name#a-bc.fr"; // SMTP account username
$mail->Password = "password"; // SMTP account password
$mail->AddReplyTo('username.name#a-bc.fr', 'First Last');
$mail->AddAddress('username.name#a-bc.fr', 'John Doe');
$mail->SetFrom('username.name#a-bc.fr', 'First Last');
$mail->AddReplyTo('username.name#a-bc.fr', '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->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!
}
PHPMailer will not throw exceptions unless you pass true to the constructor, like $mail = new PHPmailer(true);, so your code will not cause any exceptions to catch, nor report any errors. I suggest you set $mail->SMTPDebug = 3; to get more feedback on the problem.

AddStringAttachment with PDF in PHPMailer not work

I had used phpmailer for send email. Now i have to attach pdf file in email. It attached pdf but that pdf could not open. and is shows there is problem in format. Is AddStringAttachment doesn't work for pdf? What should i do?
require_once('././php_mailer/class.phpmailer.php');
$this->load->helper('mail_html');
$body2 = "You are receiving this email because the Transfer Application submitted for transferring to g is missing required documentation.
Please see the note below for details. The transfer application will not be reviewed until all of the necessary materials are received by the UHSAA.
";
$body = 'test';
$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 = 'smtp.gmail.com'; // SMTP server
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com'; // sets the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = 'nabina.smartmobe#gmail.com'; // SMTP account username
$mail->Password = '*******'; // SMTP account password
$mail->AddAddress('nabina.shahi#gmail.com','nabina');
$mail->SetFrom('no-reply#nayacinema.com.np', 'no-reply#nayacinema.com.np');
$mail->AddReplyTo('no-reply#nayacinema.com.np', '');
$mail->IsHTML(true);
$mail->Subject ='NayaCinema: Test';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
$mail->AddStringAttachment($body2, 'Filename.pdf', 'base64', 'application/pdf');
if($mail->Send()){
echo 'sent'; die;
return true;
}else{
echo ' not sent'; die;
return false;
}
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Thank you
You can create a .pdf with another PHP class TCPDF. You can see how it goes in examples. And if you dont want to save the PDF in the server you can send like you did, you only need to change the output line of the TCPDF $pdf->... codes, like this:
$attachdata = $pdf->Output('foo.pdf','S'); // return the document as a string (name is ignored)
then you can send it:
$mail->AddStringAttachment($attachdata, 'Filename.pdf');

PHPMailer not sending CC or BCC

I have been testing the following code for hours. The email will send to the addresses added through $mail->AddAddress() and in the received email it states the cc but the person cced does not receive the email. I have looked everywhere and can not find a solution to why this is happening. I have run tests and all variables are being submitted to this code properly.
My server is running Linux Red Hat
My Code:
require_once('../smtp/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$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->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the server
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = $port; // set the SMTP port for the GMAIL server 465 or 587
$mail->Username = $username; // GMAIL username
$mail->Password = $password; // GMAIL password
// Add each email address
foreach($emailTo as $email){ $mail->AddAddress(trim($email)); }
if($cc!=''){ foreach($cc as $email){ $mail->AddCC(trim($email)); } }
if($bcc!=''){ foreach($bcc as $email){ $mail->AddBCC(trim($email)); } }
$mail->SetFrom($emailFrom, $emailName);
$mail->AddReplyTo($emailFrom, $emailName);
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($content);
// $mail->AddAttachment('images/phpmailer.gif'); // attachment
// $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo'1';exit();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Old question, but I ended up here looking for an answer. Just learned elsewhere that those functions AddCC and AddBCC only work with win32 SMTP
Try using:
$mail->addCustomHeader("BCC: mybccaddress#mydomain.com");
See http://phpmailer.worxware.com/?pg=methods
Hope this helps someone, cheers!
$address = "xxxxx#gmail.com";
$mail->AddAddress($address, "technical support");
$address = "yyyyyy#gmail.com";
$mail->AddAddress($address, "other");
$addressCC = "zzzzzz#gmail.com";
$mail->AddCC($addressCC, 'cc account');
$addressCC = "bcc#gmail.com";
$mail->AddBCC($addressCC, 'bcc account');

Sending Emails With phpmailer From A php Project?

i want to send emails with phpmailer.
for doing that i searched google and i found the blow link :
http://phpmailer.worxware.com/
so i download their libraries from here (by download link in their site , php 4-5):
http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php4/0.90/
after that i refer to this example : (mean advanced using smtp)
http://phpmailer.worxware.com/index.php?pg=exampleasmtp
<?php
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$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\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
but i could n't find class.phpmailer.php file for this line -> require_once('../class.phpmailer.php'); from dowloaded file.
did i miss something?
would u plz help me for that?
best regards.
The "../" means "one directory level higher". That means that it should look for the file class.phpmailer.php in the directory one higher than that where this script is executing from. If that's not where you saved class.phpmailer.php relative to your code, then adjust the path.
Try this one.
<?php
include "/home/tnehme/public_html/smtpmail/classes/class.phpmailer.php"; // include the class name
$mail = new PHPMailer(); // 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\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
I'm using PHPMailer with the 5.5.10 and its working really well.
It was very easy to get up and running. Copy files into a directory (alongside your PHP files)
'include' it in your script... and use the example code they give you as a basis for your email.
To get the files visit:
https://github.com/PHPMailer/PHPMailer
good luck.
I also here good things about SwiftMailer.

Using PHPMailer to send emails to Variable addresses

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.

Categories