Solved

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

Posted on 2016-09-05
10
41 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 51

Accepted Solution

by:
Julian Hansen earned 300 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 30

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 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 100 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
 

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 21

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 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 51

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 51

Expert Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now