PHP Sending Email Error - php

I'm trying to send an email using PHP code, I'm sure the code is right but I'm not receiving any emails. Can anyone see a problem with this code:
<?php
$to = 'i7906890#bournemouth.ac.uk';
$subject = 'Registration Complete';
$message = 'Thank you for joining us at Arsenic & Vice';
$header = 'From: admin#arsenicandvice.co.uk';
if (mail($to, $subject, $message, $header)){
echo('<p>Sent</p>');
} else {
echo('<p>Fail</p>');
}
?>

I've tested your code on my server and it works fine, I've received the e-mail.
I've also run it so you can receive it on i7906890#...
The problem is on your hosting. Either your e-mail server (qmail...) has a long queue or it is stuck. Contact your server admin.

Related

PHP Sending Email Problems

I have a few questions regarding sending email in PHP. I've been on google for the last few days and I'm still having trouble getting this to fully work.
My first question is how do I change the "From" section of my email? I have "To: support#mydomain.com" in my "from" section:
I'd like to have just the proper name of my domain (eg: "testingstuff.com" -> "Testing Stuff"). How could I achieve this?
Once I actually open the email everything in it is fine and correct, including the From email address being "support#mydomain.com".
Also my mail won't send to gmail addresses. It shows up in my mail queue and my logs say it is sent but it never is received on my gmail. Do I have to take extra steps for Google to accept my email? If so what are those? Do other major mail provides require the same steps, or are they different steps?
This is my code so far:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set("sendmail_from", "support#mydomain.com");
class email {
public static function send($to, $subject, $message) {
$headers = "From: Testing Stuff <support#mydomaincom>\r\n";
$headers .= "Reply-To: support#mydomain.com\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
}
}
?>
Usage:
require_once("../mail.php");
email::send("support#mydomaincom", "testing email subject", "testing email body");
Am I doing anything wrong in my code?
You need to check if the email is sent properly checking the mail() result, in this way:
$result = mail($to, $subject, $message, $headers);
if(!$result) {
echo "Error";
} else {
echo "Success";
}
this is inside your static function,
Also check your spam folder if the mail function return "success".

Sending email from html form using php [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
Ive been trying this out the whole day but I cant figure out how to send an email from my html contact form containing the information from the form to my email address. Im new to php.
Ive tried running this by uploading it to free web hosting. I get the message "success!" when I press the submit button on my html form but no email is actually sent.
Any help is appreciated.
PHP script:
<?php
//Subject
$subject ="Contact Form Submission";
// Name
$name =$_POST['InputName'];
// Message
$message =$_POST['InputMessage'];
//Mail of Sender
$email =$_POST['InputEmail'];
//From
$header = "From:$name<$email>";
$send_contact=mail("myemail#gmail.com",$subject,$message,$header);
//Check if mail was sent
if($send_contact){
echo "Success!";
}
else {
echo "Error!";
}
?>
EDIT: Figured it out after one whole day of trial and error. The problem was with the free web host I was using. Changed hosts and the code started working fine. Hope this helps someone in the future. Thanks all for the help.
I have a pretty good idea why your code is not working. It happened to me a long time ago. The reason why your code is not working is because :
When you pass "from" in headers, php expects an existing email account of your
server. For example : $headers = 'From: emailacc#yourserver.com';
So first thing you gotta do is create an email account on your server. And then put the From in header to the email address that you've just created.
The From field in the $headers is not the From as you think.
<?php
$email = $_POST["InputEmail"];
$subject = $_POST["InputSubject"];
$message = "From: ".$email.", ".$_POST["InputMessage"]; // you put the email address from the input form here
$headers = 'From: emailacc#yourserver.com'; // here is the email address specified from which u want to send the email.(i.e. your server email address)
mail($to, $subject, $message, $headers)
?>
I'm sure this will do the job :)
Have a shot at this.
I changed you're $header variable around a little bit, so that rather than setting the email as "$email", It'll actually pass through the posted email entered in the form. This apply's to the name too.
I also made it so that you pass the mail function through the parameters of the if statement, rather than setting a new variable.
$headers = "From: " . $name . "<" . $email . ">"; // notice new concatenation
if(mail("myemail#gmail.com", "Contact Form Submission", $message, $headers)){
// success message
} else {
// error message
}
Really hope this helps! :)
Try adding spaces after the "=" that might be the problem,
If that doesn't work you could try to use this
<?php
$emailvariable = $_POST['InputEmail']
$to = 'example#gmail.com';
$subject = "Form"
$message = $_POST['InputMessage'];
$headers = "From: $emailvariable";
mail($to, $subject, $message, $headers);
?>
Hope this helps

send private copy of autoresponder mail

I have some php here that works great. I want a mail to be sent to the user who submits a form and I also want a copy of that mail sent to myself but I don't want my email address to be made available to the user.
Here's the php I'm using to govern the mail sending ...
$to = 'xxxx#xxxx.com' . ', ';
$to .= $email;
$subject = 'xxxx';
$message = "Thank you for submitting the form.";
$headers = "From: xxxx#xxxx.com\r\nReply-To: xxxx#xxxx.com";
$mail_sent = #mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
When the code is parsed emails are duly sent to both the users submitted email ($email) and the address I enter in the first $to variable however the user can see the email address I enter as another recipient when they receive the email. Anyone know how I can get around this? Any help will be much appreciated. Thanks.
Use a BCC header instead of an additional To in your $headers string. It stands for "Blind Carbon Copy", and instructs the mail server to duplicate the mail to extra recipients, but remove that header from the original copy, so the main recipients can't know it was there.

not able to send E Mail using xampp

Hi I was trying to send Email using php in xammp
I have already started Mercury Service
Heres my code
<?php
$to = "nikhil08514#gmail.com";
$subject = "Hi!";
$body="test";
$headers = "From: root#localhost.com";
if (mail($to, $subject, $body, $headers))
{
echo "Message successfully sent!";
}
else
{
echo "Message delivery failed...";
}
?>
when i execute code i am getting output as
Message successfully sent!
But when i check my mail box.I dont see mail.I checked all folders in my Mailbox but its not present
your code is right try that on server it will work surely, xampp does not send directly like that from xammp. by using smtp you can send.

php sending double email

I realize that this is similar to this question, however, I only get a double email when sending to myself. I have sent emails to others with the same script, and they get a single email. This is the email script:
<?php
function sendEmail( $recipient, $sub, $msg )
{
$to = $recipient;
$subject = $sub;
$message = $msg;
mail( $to, $subject, $message );
}
?>
The code calling this is this:
if( $retVal != FALSE ) // No errors in execution of report generation
{
$subject = "Successful Report";
$message = "The report was successfully generated.";
// Notify people about success
sendEmail( $mailto, $subject, $message );
echo "Successful report generation\n";
}
else // Error in report generation
{
$subject = "Unsuccessful Report";
$message = "The report failed to generate.";
// Notify people about failure
sendEmail( $mailto, $subject, $message );
echo "Report generation was unsuccessful\n";
}
where $retval is the return value of system(). Can anyone shed some light on this issue? Or is this something that can be overlooked?
Thanks very much
-rusty
Based on the conversation: You are the only recipient that is receiving double email, and the string "Successful report generation" only prints once. It sounds like PHP is not the culprit here, rather something outside of PHP, such as the MTA.
Take a look at the headers of the two email you receive, particularly the MessageID header. If they are identical, then a single email was sent from PHP (good!) and somewhere along the line it got delivered to you twice.
If they are not identical (messy), then PHP did in fact send out two emails (not likely) or there is a resender somewhere between PHP and your mail client.
In the later case, I would take a deeper look at the headers in your email, to determine the source and route of each email, as well as the envelope to determine where the MTA though it was sending mail to.
I would add the output from debug_backtrace() into the body of your email. This will allow you to determine when the sendEmail function is called and who called it.
$message = "";
$header = "Content-Type: text/html; charset=UTF-8;\r\n";
mail($mailto, $subject, $message, $header);

Categories