why this code doesn't send mails?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ashiland
Forum Newbie
Posts: 18
Joined: Sat Jul 16, 2011 5:19 am

why this code doesn't send mails?

Post by ashiland »

Hi I have set up a license selling website, it works with paypal IPN, everything works well except the mail function, where could be the problem please?
here is the code in brief:

Code: Select all

<?php
$payer_email = $_POST['payer_email'];
$mail_To = $payer_email;
$mail_Subject = "Your license";
$mail_Body = $content;

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: '.$first_name.' <'.$payer_email.'>' . "\r\n";
$headers .= 'From: mysite <no-reply@mysite.com>' ."\r\n";

mail($mail_To, $mail_Subject, $mail_Body, $headers);
?>
Many thanks in advance.
Last edited by Benjamin on Sun Jul 31, 2011 12:03 am, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: why this code doesn't send mails?

Post by social_experiment »

ashiland wrote:where could be the problem please?
Are you receiving any error messages
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
ashiland
Forum Newbie
Posts: 18
Joined: Sat Jul 16, 2011 5:19 am

Re: why this code doesn't send mails?

Post by ashiland »

I don't get any error messages. actually before getting to these lines all the $_post data including $payer_email are stored in the database and everything works well, sending email is the last step.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: why this code doesn't send mails?

Post by social_experiment »

Code: Select all

<?php
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: '.$first_name.' <'.$payer_email.'>' . "\r\n";
$headers .= 'From: noreply@mysite.com' ."\r\n";
?>
It could be a problem with the headers, try the example above and remove the 'To' header aswell. Also, modify the 'To' header to only include the email address.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
phazorRise
Forum Contributor
Posts: 134
Joined: Mon Dec 27, 2010 7:58 am

Re: why this code doesn't send mails?

Post by phazorRise »

Instead of writing a new mail class or function you can use phpmailer or swiftmail libraries.
Main issue arises when you send the mail successfully and it gets marked as spam. So these libraries can be helpful for many purposes. You can send attachments, different content types, using external smtp's for sending mails.

But if you want to use your own, it has to be able to tackle the spam filters.
Last edited by phazorRise on Sun Jul 31, 2011 1:49 pm, edited 1 time in total.
ashiland
Forum Newbie
Posts: 18
Joined: Sat Jul 16, 2011 5:19 am

Re: why this code doesn't send mails?

Post by ashiland »

social_experiment is right, the problem is with this line :

Code: Select all

$headers .= 'To: '.$first_name.' <'.$payer_email.'>' . "\r\n";
it works when I change it to :

Code: Select all

$headers .= 'To: Mike <abc@example.com>' . "\r\n";
but this is not what I need, I don't know how I should fix this.
Last edited by Benjamin on Sun Jul 31, 2011 1:50 pm, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
Post Reply