Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Could you point what is preventing PHP mail function to work out in this case?

Posted on 2016-09-05
10
Medium Priority
?
55 Views
Last Modified: 2016-09-08
Hi Experts

Could you point what is preventing PHP mail function to work out in this case?

PHP 5.6

Accordingly to
<?

   $host = "mysql.XXXXX.com.br"; // Host
   $usermysql = "XXXXXXXXXX"; // Mysql username
   $passmysql = "XXXXXXXXXX"; // Mysql password
   $db_name = "XXXXXXXXXX"; // Database name locaweb

   mysql_connect("$host", "XXXXXXXXXX", "XXXXXXXXXX") or die("cannot onnect");

   mysql_select_db("$db_name") or die("cannot select DB");

   $sql = mysql_query("SELECT username , email , realname
		FROM forum_user
	WHERE substring(username,1,5)='teste'");

   $row = mysql_num_rows($sql);

   $conteudo = "<img src='Splash-Screen.png'/>";

   if ($row)
   {

       for ($i = 0; $i < $row; $i++)
       {
           $nome = mysql_result($sql, $i, "username");
           $email = mysql_result($sql, $i, "email");

           // Corpo da Mensagem
           $mensagem = '<html>
                <body>
                <font face=Arial size=2>
		<TABLE BORDER="0" CELLSPACING="2" align="left">
		<TR>
		<TH>  </TH>
		</TD>
		<TH ALIGN="LEFT"> 
	<PRE>

<font face=Arial size=2>
Prezado(a)  ';

           $mensagem = $mensagem . $nome . '    /';
           $mensagem .= '<p>
           
Saudações !

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Agradecemos antecipadamente a divulgação !
<r>
Abraço <A HREF="http://www.xxxxx.com.br/email.php"></A>.
Equipe <A HREF="http://www.xxxxx.com.br">Espiriplug</A>
</font>
		</PRE>
		</TD>
		</TR>
		</TABLE>
		</TD>
		<TD><PRE> </PRE></TD>
		</TR>
		<TR>
		<TD ALIGN=:LEFT"><B><A HREF="http://www.xxxxxx.com.br/email.php"><LI>Contato online </B></TD>
		<TD><BR></TD>
		<TD><A HREF="http://www.espiriplug.com.br"><IMG SRC="http://www.xxxxxx.com.br/Splash-Screen.png"> </A></TD>
		</TR>
		</TABLE>  
		</font>
                </body>
                </html>';

           $assunto = "eSpiriplug - Boletim 021 - Atualização de versão Sistema de Gestão de Bibliotecas Circulantes ";
           $remetente = "contato@xxxxxxx.com.br";

           // Headers
           $headers = "MIME-Version: 1.0\n";
           $headers .= "Content-type: text/html; charset=iso-8859-1\n";
           $headers .= "From: Contato On-Line <contatol@espiriplug.com.br>\n";
           $headers .= "Return-Path: <contato@espiriplug.com.br>\n";

           echo 'Email........' . $email . "<br/>";
           echo 'Assunto......' . $assunto . "<br/>";
           echo 'Mensagem........' . $mensagem . "<br/>";
           echo 'Headers......' . $headers . "<br/>";

           try
           {

	       echo "Passed here OK !!! </br>";

              $result = mail($email, $assunto, $mensagem, $headers);

               if ($result)
               {
                ?> <p>
			<?php
                ?> <p>
 		<?php
                   echo '_____________________________' . '</br>';
                   echo "Email mala ==>OK.";
                   echo '_____________________________' . '</br>';

            ?> <p>
			<?php

               } else
               {
                   echo "Email mala NOK.";
               }
           }
           catch (exception $e)
           {
               var_dump($e->getMessage());
           }
       }
   }

?>

Open in new window


mail function is fired but doesn't work, and no error is captured.

img002
Thanks in advance.
0
Comment
Question by:Eduardo Fuerte
10 Comments
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 1200 total points
ID: 41785142
Two things - you are using the MySQL library - this has been deprecated.
Try using the PHPMailer library https://github.com/PHPMailer/PHPMailer

Have you tried a simple script of
<?php
$to = "youremail@somewhere.com";
$subject = "Testing my php script";
$message = "I hope I get this";
mail($to, $subject, $message);

Open in new window

1
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41785173
Try to replace:
$headers .= "Return-Path: <contato@espiriplug.com.br>\n";

Open in new window

with
$headers .= "Reply-To:  <contato@espiriplug.com.br>\n";

Open in new window

0
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 400 total points
ID: 41785267
The PHP mail() function is very simple minded and will not create a proper exception.  It 'hands off' the data you provide to an external function and only reports success or failure of the transfer.  It does Not tell whether the email was sent.  Try Julian's example to see whether it even works on you server.

http://php.net/manual/en/function.mail.php
http://php.net/manual/en/language.exceptions.php
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Eduardo Fuerte
ID: 41785288
Hi

Yes, I'm following Julian suggestion,  just installed   PHPMailer-master and I'm configuring it to my code.
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41785375
Some linux mail servers require both a carriage return and a new line at the end of each header. Try
           $headers = "MIME-Version: 1.0\r\n";
           $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
           $headers .= "From: Contato On-Line <contatol@espiriplug.com.br>\r\n";
           $headers .= "Return-Path: <contato@espiriplug.com.br>\r\n";

Open in new window

1
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 41785391
PHPMailer is a good solution, if you really want to do the email yourself.  As American presidential candidate Hillary Clinton has discovered, email is not nearly as simple as one might think.  This is because there are many, many moving parts to email, only a few of them occur on your server, and none of the external errors are reported back to you.  At best, you're shooting in the dark.

Here's how and why you must get off MySQL.  You need to make this a priority.
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

Here's why you should use the full <?php tag instead of the <? short-open tag. (TL;DR It makes your script dependent on a configuration setting that you may not be able to control)
http://php.net/manual/en/language.basic-syntax.phptags.php
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 41785624
@Eduardo,

Before you try PHPMailer - did you try the short code snippet I sent you. That is the one with the fewest moving parts. If that does not work then you are dealing with a configuration problem. If it does work then you can slowly add to it until you figure out the problem.

PHPMailer simplifies some of the more complicated aspects of sending a mail but essentially it is working off the same base.
0
 

Author Comment

by:Eduardo Fuerte
ID: 41785938
Hi for All

And thank you for the replies.

I really try every suggestion you gave me as a workaround of the original mail() problem during my PHP Mailer code convertion with no success, the fact reinforced the migration.

Now I just converted and tested it I opened another question just to possibly have  your opinion of
in my case. It worked out for a small test.

https://www.experts-exchange.com/questions/28967921/Could-you-have-a-look-at-this-PHP-script-using-PHPMailer-to-see-its-correction.html

The problem I had is better explained there!

(Maybe something related to what @Ray advertised about "independent  living"(or some bit erratic?)  of the emails after sent)
0
 

Author Closing Comment

by:Eduardo Fuerte
ID: 41789554
Thank you for the advices!
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 41789651
You are welcome Eduardo,
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question