• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 68
  • Last Modified:

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

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
Eduardo Fuerte
Asked:
Eduardo Fuerte
3 Solutions
 
Julian HansenCommented:
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
 
Marco GasiFreelancerCommented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Eduardo FuerteAuthor Commented:
Hi

Yes, I'm following Julian suggestion,  just installed   PHPMailer-master and I'm configuring it to my code.
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
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
 
Ray PaseurCommented:
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
 
Julian HansenCommented:
@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
 
Eduardo FuerteAuthor Commented:
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
 
Eduardo FuerteAuthor Commented:
Thank you for the advices!
0
 
Julian HansenCommented:
You are welcome Eduardo,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now