tags:

views:

518

answers:

8

Hello everyone. How can i send mails through the php script?? I am trying to do somthing like this:

for($k=0;$k<=$x->length-1;$k++)
{
    for($l=0;$l<=$j-1;$l++)
    {
        if($y->item($k)->nodeValue==$JobNoArr[$l] && $AcceptanceDateArr[$l]=='0000-00-00')
        {   
            //echo  $v->item($k)->nodeValue ;
            $email = $v->item($k)->nodeValue . ",";
            $to = $email;
            $subject = "My subject";
            $txt = "Hello world!";
            $headers = "From: [email protected]" . "\r\n" .
            "CC: [email protected]";
            mail($to,$subject,$txt,$headers);
        }
    }
}

Please help me in this issue.

Best Zeeshan

+3  A: 

first do

echo $result = mail($to,$subject,$txt,$headers);

and see what u get , error ?

I recommend to use a class such phpMailer

why you have comma in the end of ths line ?

$email = $v->item($k)->nodeValue . ",";

you send to one mail every time.

Haim Evgi
+6  A: 

You're trying too much all at once. Try going one step at a time. First send a simple email, with hard-coded parameters to get that working, then troubleshoot it within the context of your nested loops.

karim79
my code works for a few domains, like google.. but not all of them.
Zeeshan Rang
You may be hitting spam-filters, etc.
Jonathan Sampson
+4  A: 

The code (the inner most block) looks correct. Make sure your environment is setup correctly. http://ca3.php.net/manual/en/mail.setup.php

Lawrence Barsanti
+15  A: 

I strongly advise against sending mail using PHP's mail() function. Composing valid emails and delivering them successfully is trickier than it seems at first glance. There is encoding, putting parts together, validation & sanitation, error reporting (more than bool mail(...)), support for authentication and on and on ... The mail() function does not offer any of these things.

Try SwiftMailer or similar. You can configure it to use PHP's mail() function and so much more. I highly recommend it.

VolkerK
SwiftMailer is rather nice, we make our end users use that now because the mail() function on Windows PHP is so broken.
Kev
The mail function on Windows PHP is _not_ broken. I use it all the time.
Charlie Somerville
notepad.exe isn't broken either, still it's not my first choice for C# development.
VolkerK
+5  A: 

the problem is mail function is very unreliable, especially when sending large amount of emails.

i would recommend looking into PHPmailer library (uses direct SMTP connection): http://phpmailer.codeworxtech.com/

dusoft
+2  A: 

php-s mail function uses sendmail as a MTA, so if some mails go through and some not, I would look at sendmail's log for errors.

Marie Fischer
Not really. PHP uses the sendmail API. Any self-respecting MTA on Unix/Linux can emulate sendmail through a wrapper.
Sander Marechal
Correct. Still, my point is that mail sent through PHPs mail function goes out through the system mailer and for me, that would be the first place to look for errors.
Marie Fischer
+1  A: 

If you are on a shared web host, or your home computer, the main domain for the server will be something like

server.your-isp-or-host.com

The spam filter will then see the email claiming to be from

yourdomain.com

when it really came from the first address, and would then delete it.

This would explain the hit-and-miss nature of your error.

If you are on a dedicated server, or a static IP pointing to your home computer with properly set up DNS, the above does not apply.

Macha
+1  A: 

if it works sending emails to gmail then it should work fine sending emails to yahoo too

you may find that the issue is not the sending of the emails, but maybe yahoo is marking them as spam or blocking them at the gateway

i notice you are appending a comma to the end of the email address, what is the point of that ?

there may be other problems, are your loops correct, are they covering all the cases you expected.

are you sending thousands of emails ? can your mta handle the rate at which you are putting emails into the queue

is your script hitting max_execution time and stopping ?

bumperbox
none of the above.. my script is working fine for gmail, and other mail servers, but not working for yahoo .. I am not sending thousands of mail, but ya a couple hundred.
Zeeshan Rang