0

I'm experiencing the following problem:

I Have a small form on my site that pointed to a small php processor to send a mail. There "works" but doesnt send a mail. I tested the script in other hosting and works like a charm.

I called the hosting provider to see where is the problem. The hosting company provided the following PHP script to test the mail sending:

<?php
$to = "myemail@myemail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "test@pizanoecheverri.co";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

Now they told me that I do the required editing so it can works. So I did. I have this small form in the home page (you can see it in pizanoecheveri.co):

<form name="form1" method="post" action="testm/mailing.php" class="miscclass" id="form1">
<div style="text-align: center;" id="contbaloon"><img border="0" alt="" src="/images/misc/baloon1.png"></div>
<input type="text" name="email" class="contactfield" id="textfield">
<p style="text-align: right;"><input type="submit" value="Enviar" name="button" style="background-color: #bbbdc0; border: 0 none; color: #ffffff; margin-bottom: 17px; margin-top: 10px;" class="contacto-x" id="contactbutton"></p>
</form>

And i have done the editings on the code like this:

<?php
$to = "myemail@myemail.com";
$subject = "Lista de correos.";
$message = "El siguiente correo se debe agregar a la lista de correo. ".$_POST["email"];
$from = $_POST["email"];
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
echo $message;
?>

I noticed this to the hosting provider and they say that is a problem on the programming. This is something i don't know sincei tested the same code in other hosting and works perfectly.

In short i don't understand why the provided code works and when i edit the code doesnt work on THAT HOSTING, when in other it works correctly and there is no syntax error, as far as i see it and Dreamweaver shows it.

The only detail i know from that hosting is that is Windows based running plesk, while the other hosting where i tested the code (where it works) is a Linux based apache. Both are running PHP 5.2.17.

Thanks in Advance

EDIT: i tested the following modification and didn't worked as well on that hosting

<?php
$to = "myemail@myemail.com";
$subject = "Lista de correos.";
$message = "El siguiente correo se debe agregar a la lista de correo. ".$_POST["email"];
$from = $_POST["email"];
#$headers = "From:" . $from;
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
echo $message;
?>
  • The `Mail Sent` message will be displayed every time the script gets executes. Stop *assuming* things and check the return values of `mail()` function before echoing the 'Success' message. If it's still not working, check your/receipient's spam folder. If you still don't have the email, check your server logs. – Amal Murali Oct 25 '13 at 12:52
  • That's why I tested in other hosting in first place. In the other hosting the script executes and the message arrives normally. In the hosting where is needed to execute doesnt work my edition. As I said before. the provided script from the Hosting company worked, but my editions doesnt work in the hosting. Such editings i tested them in the other hosting and work perfectly. – Aldemar Hernández Oct 25 '13 at 13:00

2 Answers2

0

Please try this tested code. Just Copy and pest.

change only header section

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
0

The problem is because no Sendmail installed on the host, which is a Linux distributed extension for sending mails via the IMAP protocol.. At modern times, maybe all of the mail providers disable to handle these request because no valid sender needed to handle the request..

The solution for this is use the SMTP protocol, which validates the sender and the receiver email account as well, so it is slower, but safer..

You do not need to know how the protocol works, you can use a PHP library to do this, namely the open-source PHPMailer..

Example

require_once('class.phpmailer.php');

$mail = new PHPMailer(true);
$mail->IsSMTP();

try {
    $mail->Host       = "mail.yourdomain.com"; // SMTP server
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
    $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "yourname@yourdomain"; // SMTP account username
    $mail->Password   = "yourpassword";        // SMTP account password

    $mail->AddReplyTo('name@yourdomain.com', 'First Last');
    $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
    $mail->SetFrom('name@yourdomain.com', 'First Last');

    $mail->Subject = 'Subject';
    $mail->MsgHTML(file_get_contents('contents.html'));
    $mail->AddAttachment('images/phpmailer.gif');
    $mail->Send();

    echo "Message sent!";
} 
catch (PHPMailerException $e) {
    echo $e->errorMessage();
} 
catch (Exception $e) {
    echo $e->getMessage(); //Boring error messages from anything else!
}
Eru
  • 187
  • 4