Click here to Skip to main content
15,885,885 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello everyone
I have developed a static website with one email script in PHP. I am able to send and receive emails but only if the recipient or sender email address is info@mydomain.com. If i try both the sender and receiver address to be gmail/ yahoo then the script is not working.
When i contact the hosting company for the same, they ask me to code for bounce back email so that a reason for the failure could be detected.
Now, i need code for that bounce back email script in php.
Thank you
the code is:
PHP
<?php
$to= "mymail@gmail.com";
$emailSubject = "Here is an email ";
$emailContext = "Sending content using PHP mail function";

$emailHeaders = "Cc: mymail@yahoo.co.in" . "\r\n";

$from= "-fsomeone@yahoo.co.in";
$emailStatus = mail($to, $emailSubject, $emailContext, $emailHeaders, $from);
if($emailStatus) {
echo "Email Sent!";
} else {
echo "Email not sent";
}
?>


This code is not working because there is no address like info@mydomain.com, if i replace any of the toaddress or fromaddress with info@mydomain.com, then the script will working not otherwise
Posted
Updated 23-Mar-15 7:47am
v3
Comments
Sergey Alexandrovich Kryukov 23-Mar-15 11:00am    
"Is not working" is not informative at all. You need to explain everything in proper detail, starting from your ultimate goals and your scenario.
—SA
ZurdoDev 23-Mar-15 13:54pm    
Sending email to the other server is an asynchronous process so how would php catch any result? It could take hours to get a result back.

Hello friends
I did not try email bounce script as was asked by the hosting account support but i try the following code which work very good but the only issue was that I was getting the emails like conversations in my gmail id which I managed to rectify by just adding the name field concatenated by the Subject and now the script is working just fine.
Hope that it will help someone
if you have any other idea or a better solution, please share it here.
Thank you

XML
<?php

$msg="";
if(isset($_POST['book']))
{
    $name=$_POST['name'];
    $address=$_POST['address'];
    $email=$_POST['email'];
    $mobile=$_POST['mobile'];



    $from_add = "info@yourdomain.com";
    $from_rep=$email;

    $to_add = "mygmail@gmail.com"; //<-- put your yahoo/gmail email address here

    $subject = "Booking Details".$name;
    $message = "Name:"."\t".$name."\r\n";
    $message.="Address:"."\t".$address."\r\n";
    $message.="Mobile:"."\t".$mobile."\r\n";





    $headers = "From: $from_add \r\n";
    $headers .= "Reply-To: $from_rep \r\n";
    $headers .= "Return-Path: $from_add\r\n";
    $headers .= "X-Mailer: PHP \r\n";


    if(mail($to_add,$subject,$message,$headers))
    {
        ?>
        <center>
        <b>Thank you <?php echo $name; ?></b>
        <br><b>Your email has been sent!</b>
        <p>we will get back to you soon!</p>
        </center>
        <?php
    }
    else
    {
       $msg = "Error sending email!";
    }

}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
</head>

<body>

<div>
<center>

<p><a href="index.html">Click here to continue</a></p>

</center>
</div>

</body>
</html>
 
Share this answer
 
You don't need a "bounce back email script"; you need to find out why your email isn't being sent.

The reason your server cannot send email from a Yahoo / GMail address to another Yahoo / GMail address is that your local server is not a Yahoo / GMail server. Unless you send the email through their servers, using proper authentication, they will reject the message as "spoofed".

It's as if a random person has walked up to the front door of your office, claimed to be from your internal IT department, and demanded to inspect your servers. Since you know they're not from your IT department, you wouldn't let them in.

To send email from a Yahoo or GMail address, you need to own that address. You will then be able to authenticate to the Yahoo / GMail mail server, and send the message via that server.

Depending on your setup, you might need to use an alternative email library. For example:
PHPMailer - A full-featured email creation and transfer class for PHP[^]
Send Mail through GMail SMTP Server with PHP[^]
 
Share this answer
 
Comments
Programmer Kreative 22-Sep-18 1:48am    
Try to reply what you have ask for. If you don't know then shut your mouth instead of giving response of something else.
Richard Deeming 24-Sep-18 13:29pm    
If you'd actually bothered to read and understand anything on this page, you'd see that I did reply to what was asked for.

The fact that the reply was advice, rather than a naive code-dump, doesn't make the reply any less valid.

And telling people to "shut up" just because you don't like their answer is considered abuse. Keep it up if you want to get banned from the site!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900