1
I have a PHP website with a contact form that allows me to upload a file. This file must then be sent in an e-mail together with the other data. The email is not being received, although you will receive the send confirmation message. It is also not in spam folders. I tried the company email, Gmail and Hotmail.
There is a PHP file with HTML that then makes a post (jQuery and AJAX) to a script PHP file. In this script file is the sending of email. I know that the sending function is being executed because I receive message from it.
html code:
<div id="inscreverOportunidade" class="hideform inscreverOportunidade" style="display:none;">
<div id="boxForm">
<div class="formProf">
<div style="width:100%;" class="big-new-close" onclick="javascript:showForm('');"><span>X</span></div>
<div class="to">
<div class="small-text"> NOME*</div>
<input type="text" required="true" class="input-text" value="" id="NomeO" name="NomeO" onfocus="this.value = '';" onblur="if (this.value == '') {
this.value = '';
}">
</div>
<div class="to email">
<div class="small-text"> EMAIL*</div>
<input type="email" required="true" class="text" value="" id="EmailO" name="EmailO" onfocus="this.value = '';" onblur="if (this.value == '') {
this.value = '';
}" >
</div>
<div class="to">
<div class="small-text"> CONTACTO*</div>
<input type="text" required="true" class="input-text" value="" id="ContactoO" name="ContactoO" onfocus="this.value = '';" onblur="if (this.value == '') {
this.value = '';
}">
</div>
<div class="to email">
<div class="small-text"> LOCALIDADE*</div>
<input type="text" required="true" class="text" value="" id="LocalidadeO" name="LocalidadeO" onfocus="this.value = '';" onblur="if (this.value == '') {
this.value = '';
}" >
</div>
<div class="to">
<div class="small-text"> PROFISSÃO*</div>
<input type="text" required="true" class="input-text" value="" id="ProfissaoO" name="ProfissaoO" onfocus="this.value = '';" onblur="if (this.value == '') {
this.value = '';
}">
</div>
<div class="to email">
<div class="small-text"> ANEXAR CURRÍCULO*</div>
<input type="file" required="true" name="CV" id="CV" class="text" accept=".docx, .doc, .pdf">
</div>
<div class="info">Os campos marcados com * são de preenchimento obrigatório</div>
<div class="submit-curriculo"><input type="submit" name="submit" id="submit" value="Inscrever" onclick="javascript:sendFormRegO();
return false;"></div>
</div> <!--end formProf-->
</div> <!--end boxForm-->
</div>
<script>
function sendFormRegO()
{
var proceed = true;
//simple validation at client's end
//loop through each field and we simply change border color to red for invalid fields
$("#inscreverOportunidade input[required=true]").each(function(){
$(this).css('border-color','');
if(!$.trim($(this).val())){ //if this field is empty
$(this).css('border-color','red'); //change border color to red
proceed = false; //set do not proceed flag
}
//check invalid email
var email_reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if($(this).attr("type")=="email" && !email_reg.test($.trim($(this).val()))){
$(this).css('border-color','red'); //change border color to red
proceed = false; //set do not proceed flag
}
});
if(proceed) //everything looks good! proceed...
{
//data to be sent to server
var m_data = new FormData();
m_data.append( 'nome', $('input[name=NomeO]').val());
m_data.append( 'email', $('input[name=EmailO]').val());
m_data.append( 'contacto', $('input[name=ContactoO]').val());
m_data.append( 'localidade', $('input[name=LocalidadeO]').val());
m_data.append( 'profissao', $('input[name=ProfissaoO]').val());
m_data.append( 'file', $('input[name=CV]')[0].files[0]);
//instead of $.post() we are using $.ajax()
//that's because $.ajax() has more options and flexibly.
$.ajax({
url: '<?php echo Yii::app()->createUrl('site/inscricaooportunidade'); ?>',
data: m_data,
processData: false,
contentType: false,
type: 'POST',
dataType:'json',
success: function(response){
alert(response.text);
//load json data from server and output message
if(response.type == 'error'){ //load json data from server and output message
output = '<div class="error">'+response.text+'</div>';
}else{
output = '<div class="success">'+response.text+'</div>';
}
}
});
}
}
</script>
PHP code:
public function actionInscricaoOportunidade()
{
//if(Yii::app()->request->isAjaxRequest)
if($_POST)
{
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'Sorry Request must be Ajax POST'
));
die($output); //exit script outputting json data
}
//Sanitize input data using PHP filter_var().
$nome = filter_var($_POST['nome'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$contacto = filter_var($_POST['contacto'], FILTER_SANITIZE_STRING);
$localidade = filter_var($_POST['localidade'], FILTER_SANITIZE_STRING);
$profissao = filter_var($_POST['profissao'], FILTER_SANITIZE_STRING);
//$message->addTo(Yii::app()->params['adminEmail']);
$to_email = "[email protected]";
$from_email = $email;
//email body
$message_body = "Foi efectuada uma nova inscrição na oportunidade XXX com os seguintes dados:<br /><br />";
$message_body .= "<strong>NOME:</strong> " . $nome . "<br />";
$message_body .= "<strong>EMAIL:</strong> " . $email . "<br />";
$message_body .= "<strong>CONTACTO:</strong> " . $contacto . "<br />";
$message_body .= "<strong>Localidade:</strong> " . $localidade . "<br />";
$message_body .= "<strong>Profissão:</strong> " . $profissao . "<br /><br />";
$message_body .= "<strong>O Curriculo do candidato encontra-se anexado a este email</strong>";
### Attachment Preparation ###
$file_attached = false;
if(isset($_FILES['file'])) //check uploaded file
{
//get file details we need
$file_tmp_name = $_FILES['file']['tmp_name'];
$file_name = $_FILES['file']['name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$file_error = $_FILES['file']['error'];
//exit script and output error if we encounter any
if($file_error>0)
{
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder" );
$output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error]));
die($output);
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
//now we know we have the file for attachment, set $file_attached to true
$file_attached = true;
}
if($file_attached) //continue if we have the file
{
# Mail headers should work with most clients
$headers = "MIME-Version: 1.0\r\n";
$headers = "X-Mailer: PHP/" . phpversion()."\r\n";
$headers .= "From: ".$from_email."\r\n";
$headers .= "Subject: Confirmação de Inscricao - Oportunidade\r\n";
$headers .= "Reply-To: " .$email. "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=".md5('boundary2')."\r\n\r\n";
$headers .= "--".md5('boundary2')."\r\n";
$headers .= "Content-Type: text/plain; charset=utf-8\r\n\r\n";
$headers .= $message_body."\r\n\r\n";
$headers .= "--".md5('boundary2')."--\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: ".$file_type."; ";
$headers .= "name=\"".$file_name."\"\r\n";
$headers .= "Content-Transfer-Encoding:base64\r\n";
$headers .= "Content-Disposition:attachment; ";
$headers .= "filename=\"".$file_name."\"\r\n";
$headers .= "X-Attachment-Id:".rand(1000,9000)."\r\n\r\n";
$headers .= $encoded_content."\r\n";
$headers .= "--".md5('boundary1')."--";
}else{
//proceed with PHP email.
$headers = 'From: '.$nome.'' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
}
$send_mail = mail($to_email, "Confirmação de Inscricao - Oportunidade", $message_body, $headers);
if(!$send_mail)
{
//If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$nome .' Thank you for your email'));
die($output);
}
}
}
Voce could use this extension http://www.yiiframework.com/extension/yiimailer/ which already has what you want in a practical way
– Otto
I also tried to use yiimailer, which was implemented by those who developed the project earlier, but it wasn’t working and I couldn’t fix it. I have never worked with the Yii platform before, and my knowledge of PHP is really very little
– Hahn86
if the knowledge is little it advises you to study a lot to enter a framework without the knowledge due it is quite complicated
– Otto
That’s what I’ve been doing, but this issue is something urgent
– Hahn86