I am using PHPMailer to send email using a contact form on my web page. I am using the code
$msg = $mail->Send();
echo $msg;
but nothing is printed out.. Obviously I have all my email/domain settings above these lines. Funny thing is that the email is sending correctly, but I get no response (true OR false) from the send method, thus I cannot run an if statement to redirect to another page according to whether the email has been sent or not..
Replace your $mail->Send() call with:
if(!$mail->Send()) {
echo $mail->ErrorInfo;
}
The most likely reason you're getting no output when you echo $msg is because the result is false. When you echo false it will display nothing. Use var_dump instead of echo.
Related
I am learning PHP and from what I understand the mail function has a to parameter that needs to comply with a certain string format - php doc. I have read that if I parse an empty string then the mail function will return false (not 1). However, when I try this the mail function never fails. Is there something I have missed?
Code:
<?php
if (mail('', 'mySubject', 'myMessage')) {
echo "Success!";
} else {
echo 'Failure!';
}
?>
The output is "Success!"
Only by removing the argument entirely gets the else statement to execute. Does the to parameter need to be of a certain string format like the documentation states and if not, then how can I get this function to fail? Thanks
The mail() function does not validate the input. It more or less just takes the data and hands it over to the system mailer daemon.
If that handoff was successful, the method returns true.
It it likely that your local mailer daemon will log an error that it couldn't process the email
php mail() function is just a middle-man between your mail daemon and your code.
You need to manually validate email addresses, typically using something like
if( filter_var( $email_address ,FILTER_VALIDATE_EMAIL ) )
{
mail(parameters);
}
else {
// handle error
}
Like #skaveRat said, you'll most probably find something in your email daemon log that this mail was not sent.
I am using PHPMailer in order to send a mail as follows
$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';
$mail->isHTML();
$mail->setFrom('from#server.com', 'server');
$mail->addReplyTo('user#myfavoritemailprovider.com', 'user');
$mail->addAddress('admin#server.com');
$mail->Subject = $subject;
$mail->AltBody = getPlainMail($data);
$mail->Body = getHTMLMail($data);
try {
$mail->send();
echo "SUCCESS";
} catch(phpmailerException $e) {
echo "FAILURE: ", $e->errorMessage();
}
This works perfectly fine to send mails using MAMP on my good old Mac OS X, but when I run this code on the Linux server where it should end up, something strange happens to the mails. Spaces appearing randomly in the content and styles not being applied randomly turned out to be a consequence of blank lines between every two lines in the body of the received mail. For a simple example this could look like
<!DOCTYPE html><html><head><title>Test</title></head><bo=
dy><main><section style=3D"color:red;"><p style=3D"color=
:black;">test</p></section></main></body></html>
whereas it should be something like
<!DOCTYPE html><html><head><title>Test</title></head><bo=
dy><main><section style=3D"color:red;"><p style=3D"color=
:black;">test</p></section></main></body></html>
in order to be correct. I tried to figure out where these blank lines come from, but I could not find anything. The strangest thing might even be that when I replace $mail->send(); echo "SUCCESS"; by
$mail->preSend();
echo htmlspecialchars($mail->getSentMIMEMessage());
I get the expected result.
Would anyone have an idea where these blank lines are coming from? Any hints are appreciated
PHPMailer 5.2 has a problem with inconsistent line break formats. Have a try with the 6.0 branch (not yet released) which avoids the problem, or try setting $mail->LE = "\r\n"; and ensuring all your content uses the same line break format.
I am using a if condition and in that there is some script and then header.
But the script wont work and directly header works.
if(strpos(mysql_error(),'Email')!=false)
{
print '<script type="text/javascript">';
print 'alert("The email address is already registered")';
print '</script>';
header('Location: register.php');
}
Replace header with typical JS. That does the trick.
if(strpos(mysql_error(),'Email')!==false)
{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('The email address is already registered')
window.location.href='register.php';
</SCRIPT>");
}
There are a few different things wrong in your code.
First, the header calls should be the first thing you output. You don't do that. You have unbuffered prints before it. From the (very good) PHP documentation:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Second, even if you would have used buffered output, the Javascript would never execute as the body is not evaluated when there is a Location header.
Third, using mysql_error to find out if a certain record exists isn't the way to go. Better would be something along the lines of (pseudocode):
SELECT email FROM table WHERE email="someEmail"
if (rows found) {
alert "email already exists"
}
Finally, use mysqli or PDO.
Good luck learning webdevelopment!
Use:
window.location = "example.html";
See #Bart Friederichs's answer first, even if header works perfectly, the JavaScript alert wont work Because the page will be redirected once its completed and the browser will not execute the JavaScript for you.
<?php
print '<script type="text/javascript">';
print 'alert("The email address is already registered");';
print 'window.location = "register";';
print '</script>';
?>
You could also output it without PHP! for example:
if(strpos(mysql_error(),'Email')!==false)
{
?>
<script type="text/javascript">
alert("The email address is already registered");
window.location = "register.php";
</script>
<?php
}
?>
Is there any way possible to find out the total number of sent emails from php mail function. My mail function is inside a while loop and I want to know the number of sent emails.
Thanks
If you just want to know the number of mails accepted for delivery in the while loop, add a counter variable:
$mailsSent = 0;
while($condition) {
if (mail('foo#example.com', 'My Subject', 'My Message')) {
$mailsSent++;
}
}
echo $mailsSent;
For the total amount of mails accepted for delivery, you can configure a log file in php.ini
mail.log string
The path to a log file that will log all mail() calls. Log entries include the full path of the script, line number, To address and headers.
Reference: http://php.net/manual/en/mail.configuration.php#ini.mail.log
If you want to know the number of mails which actually got sent, check the sendmail log.
Re-Edited the Answer! Please check now. I was confused at first!
You can use this way to check how many mails have been sent by using this script:
<?php
$count = 0;
while ($condition) {
if(mail($to, $subject, $message))
$count++;
}
echo "Totally, $count messages have been sent!";
?>
I'm using PHPMailer and $mail->Send() is returning an error, my problem is when I use this email-string "noreply#pleasenoreply.com" within $mail->SetFrom(), but in the other hand it works fine with almost any other email i.e "hello#hello.com".
After debugging the code I found out that the problem is in the file class.phpmailer.php over the function ValidateAddress(). It seems that the email "noreply#pleasenoreply.com" is not valid by FILTER_VALIDATE_EMAIL nor the preg_match
PHPMailer - class.phpmailer.php - line 550:
public static function ValidateAddress($address) {
if (function_exists('filter_var')) { //Introduced in PHP 5.2
if(filter_var($address, FILTER_VALIDATE_EMAIL) === FALSE) {
return false;
} else {
return true;
}
} else {
return preg_match('/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+#(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/', $address);
}
}
Why is that possible?? does anyone have any idea what is going on??? why this email "noreply#pleasenoreply.com" is not allow?
my problem is when I use this email-string "noreply#pleasenoreply.com" within $mail->SetFrom()
I don't know why that specific address is being rejected and others aren't, but generally, you need to specify not only a valid E-Mail address as the from address, but one that is handled on the mail server you're sending the message from.
Otherwise, either the sending server is going to deny sending, or the receiving server is very likely to throw away the message as spam.
The usual policy is to specify noreply#yourdomain.com (yourdomain.com being your website domain). On some servers, you need to actually set up that address to be allowed to send mail from it.