PHP CODE BELOW: Already done configuration in php.ini & sendmail.ini
<?php
$to_email = "receipient#gmail.com";
$subject = "Simple Email Test via PHP";
$body = "Hi,nn This is test email send by PHP Script";
$headers = "From: sender\'s email";
if (mail($to_email, $subject, $body, $headers)) {
echo "Email successfully sent to $to_email...";
} else {
echo "Email sending failed...";
}
Getting the following error:
To insert a PHP variable in a string you have to use curly brackets.
Like this:
<?php
$to_email = "receipient#gmail.com";
$subject = "Simple Email Test via PHP";
$body = "Hi,nn This is test email send by PHP Script";
$headers = "From: sender's email";
if (mail($to_email, $subject, $body, $headers)) {
echo "Email successfully sent to {$to_email}..."; // <-- here the brackets
} else {
echo "Email sending failed...";
}
Related
The first code without $_Post works, the second one with $_POST doesnt. It is on the server in cpanel. Anything helps :)
I tried various things like changing the code in HTML, placing slashes, also using $_request over $_post.
This works -
$to_email = "example#example.hr";
$subject = "Simple Email Test via PHP";
$body = "Hi,nn This is test email send by PHP Script";
$headers = "From: me#example.hr";
if ( mail($to_email, $subject, $body, $headers)){
echo("Email successfully sent to $to_email...");
} else {
echo("Email sending failed...");
}
$to_email = "example#example.hr";
$subject = "Simple Email Test via PHP";
$body = "Poruka:$_POST[poruka]";
$headers = "From: $_POST[email]\n";
if ( mail($to_email, $subject, $body, $headers)){
echo("Email successfully sent to $to_email...");
} else {
echo("Email sending failed...");
}
</code></pre>
The first code sends the email, while the second one goes into echo ("Email sending failed...")
I have the following code saved as send.php:
<?php
$to_email = "person#gmail.com";
$subject = $_POST['subject'];
$body = "Hi, This is test email send by PHP Script";
$headers = "From: Me#myWebsite.com";
if ( mail($to_email, $subject, $body, $headers)) {
echo("Email successfully sent to $to_email...");
} else {
echo("Email sending failed...");
}
?>
The email sends if I navigate to https://myWebsite.com/Stuff/send.php
and make $subject = "something"
But if I set $subject to be a POST variable. It does not send the subject when I write this URL in the browser (as in, the email still sends but the subject is now blank):
https://myWebsite.com/Stuff/send.php?subject=notworking
To get both $_POST and $_GET you can use $_REQUEST which will work for both.
In your code replace
$subject = $_GET['subject'];
With
$subject = $_REQUEST['subject'];
If someone direct hit the URL in browser without paramter subject you can prevent them using
if(!empty($_REQUEST['subject']))
{
$to_email = "person#gmail.com";
$subject = $_REQUEST['subject'];
$body = "Hi, This is test email send by PHP Script";
$headers = "From: Me#myWebsite.com";
if ( mail($to_email, $subject, $body, $headers))
echo("Email successfully sent to $to_email...");
else
echo("Email sending failed...");
}
If you want to extract the values from the URL parameters then you need to use the $_GET
global variable to get the data like this,
<?php
$to_email = "person#gmail.com";
$subject = $_GET['subject'];
$body = "Hi, This is test email send by PHP Script";
$headers = "From: Me#myWebsite.com";
if ( mail($to_email, $subject, $body, $headers)) {
echo("Email successfully sent to $to_email...");
} else {
echo("Email sending failed...");
}
?>
Try it.
<?php
$to_email = "person#gmail.com";
$subject = (isset($_GET['subject']) && (!empty($_GET['subject']))?$_GET['subject']:"No Subject";
$body = "Hi, This is test email send by PHP Script";
$headers = "From: Me#myWebsite.com";
if ( mail($to_email, $subject, $body, $headers))
echo("Email successfully sent to $to_email...");
else
echo("Email sending failed...");
?>
I have a problem with my web host, I been trying to call the support but no answer yet, so I trying here
I have this simple PHP code that sends an mail to my gmail, the code works fine on my other hosting site, but on www.one.com the php just shows "Your email was sent." but there is no email send. I have set the permisions to 777 on the file, but no luck.
Is there anything i can do to fix this or is it an problem for my web host?
This is my code
<?php
$name = "MyName";
$email = "MyName#gmail.com";
$message = "This is a test";
$to = 'Myothermail#gmail.com';
$subject = 'the subject';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message;
$headers = 'From: www.mysite.se' . "\r\n";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message, $headers); //This method sends the mail.
echo "<div class='alert alert-success alert_box'>Your email was sent.</div>"; // success message
}
else
{
echo " <div class='alert alert-error alert_box'>Your email address is invalid. Please enter a valid address.</div>";
}
?>
Your script shows that your email was sent because you're not actually checking anything. mail() returns true if successful and false otherwise so:
if(mail($to, $subject, $message, $headers)) { //This method sends the mail.
echo "<div class='alert alert-success alert_box'>Your email was sent.</div>"; // success message
} else
{
echo "<div class='alert alert-error alert_box'>Mail send problem.</div>";
}
}
else
{
echo " <div class='alert alert-error alert_box'>Your email address is invalid. Please enter a valid address.</div>";
}
Can you check the logs, or enable error reporting?
I'm creating a 'forgot password' page where the user enters their email address and the script finds the password associated to that email, and sends it to the stored email address.
I believe the problem has to do with my SMTP Mailserver. I am using WAMP which doesn't have one so I downloaded one that was free.
This is the php script I'm using:
$id = checkEmail($email);
$record = readMemberRecord($id);
$password = #mysql_result($record,0,'cred');
if($password){
$email_subject = "Email Request";
$email_body = "Your password is ".$password.".";
$header = "NinjaMan";
mail($email, $email_subject, $email_body,$header);
$msg = "Your password has been sent to ".$email.".";
}else{
$msg = "Sorry, the email ".$email." wasn't found.";
}
The $msg outputs properly so I know the code is passing the mail function.
Try sending in a proper "From" in $header.
$emailFrom = "admin#yourdomain.com"; // match this to the domain you are sending email from
$email = "example#example.com";
$subject = "Email Request";
$headers = 'From:' . $emailFrom . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "Return-path: " . $email;
$message = "Your password is ".$password.".";
mail($email, $subject, $message, $headers);
See details of the mail() function.
If this doesn't work, try using PHPMailer. You configure it in code, no need to edit php.ini.
I've used it in some projects (v 2.0.4, I see the latest is 5.1) and had no problems.
Try using Google's server to send mails, you can see how to do that here
Try using this
//Email information
$to = "garggarima#gmail.com";
$subject = "Test mail";
$message = "Hello! This is a test email message.";
$from = "support#sltechsoft.com";
$headers = "From:" . $from;
$mail=mail($to,$subject,$message,$headers);
if($mail) {
echo "Thanks for mail";
} else {
echo "Mail not Sent";
}
//Email response
echo "Thank you for contacting us!";
I have this script which sends an email for users to activate their accounts on my website, its working fine my only problem is that its sending the body of the message twice, Can anybody see and explain why?
$toemail = $_POST['produgg_email'];
// Send activation email
$to = $toemail;
$subject = "Website Activation";
$headers = "From: support#website\r\n" .
$body = "To activate your website.co.uk please click <a href='http://www.website.co.uk/activateuser.php?email=$toemail'>here</a>";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
You have pasted the code? If so, you have a dot instead of a ; at line 5...