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...
Related
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...";
}
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 been struggling to send an email using the mail function in PHP for a while. I want to send 2 separate emails to 2 different email addresses.
$allowHtmlHeader = "MIME-Version: 1.0\r\n";
$allowHtmlHeader .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers = "From: webmaster#xyz.com\r\nReply-To: webmaster#xyz.com\r\n" . $allowHtmlHeader;
$customerHeaders = "From: a.b.c#gmail.com\r\nReply-To: a.b.c#gmail.com\r\n" . $allowHtmlHeader;
$subject = "Enquiry";
$message = "Try me...";
$cMessage = "I score better!";
if ($_POST[cEmail] != null)
{
if (mail("a.b#gmail.com", $subject, $cMessage, $cHeaders)) {
echo("Message successfully sent!");
}
else {
echo("Message delivery failed...");
}
if (mail("a.b.c#gmail.com", $subject, $message, $headers)) {
echo("Message successfully sent!");
}
else {
echo("Message delivery failed...");
}
}
Using the above code, i could successfully send emails to a.b#gmail.com . But in all the attempts not a single mail reached a.b.c#gmail.com, though it always successfully reached a.b#gmail.com. I even tried replacing a.b#gmail.com with a.b.c#gmail.com and yet no mail was sent to that email address alone.
Maybe the mail function does not work with emails having more than 1 dot '.' in their names?
Any help would be greatly appreciated!.
SOLVED:
Following Pagerange`s advice, i attempted removing the dots / periods ('.') from the "to" email addresses and emails are sent flawlessly!.
PHP Mail function seems to have issues sending emails TO: #domain.edu.ag. Does the function support foreign domains?
The domain is hosted on Google Apps so mail should be received in the same way. No messages are in SPAM which lead me to explore further. So I created a test script to send mail, and noticed that mail doesn't seem to be sending.
This is what I used for testing purposes:
<?php
$to = "mymail#domain.edu.ag,mymail#gmail.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>
The result was that the #gmail message was received but not the #edu.ag email address. What's going on here? There are not errors or anything and mail is received just fine from other senders.
Try doing this instead:
$to = array("mymail#domain.edu.ag", "mymail#gmail.com");
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
foreach($to as $to_addr) {
mail($to_addr, $subject, $body)
}
So you are looping through the email addresses in the array, and sending the mail statement for each one. I've had some trouble in the past using a regular string with commas.