email by php using mail()

Asked

Viewed 91 times

0

I’m making a website for an emresa but I can’t make the code that sends the email do so I’m using the php mail function

<?php 

$Enviadopor = $_POST['e'];
$Assunto= $_POST['a'];
$Mensagem= $_POST['msg'];

$corpomail= 'Fale Conosco - comdica.com 
    Email: '.$Email.'
    Assunto:' .$Assunto.'
    Mensagem:' .$Mensagem.'';

if (mail( '[email protected]', $Assunto, $Mensagem)) {
    echo "mensagem enviada com sucesso";
}else{
    echo "mensagem nao enviada";
}

1 answer

2

Here is a functional example of sending email with php via POST.. in this case I did the sending of the information via Ajax.. more vc can feel free to implement the front-end in any way you wish! I also used Json returns to display error msgns and Success on the front!

Good luck!

<?php
header("Content-type: text/html; charset=utf-8");
$filter_rules = [
    'name'      => FILTER_SANITIZE_STRING,
    'email'     => FILTER_VALIDATE_EMAIL,
    'phone'     => FILTER_SANITIZE_STRING,
    'site'      => FILTER_SANITIZE_STRING
];
$validation = [
    'name' =>[
        'is_null'   => 'Preencha o campo <b>nome</b>!',
        'is_false'  => 'O Campo <b>nome</b> não é valido!'
    ],
    'email' =>[
        'is_null'   => 'Preencha o campo <b>e-mail</b>!',
        'is_false'  => 'O Campo <b>e-mail</b> não é valido!'
    ],
    'phone' =>[
        'is_null'   => 'Preencha o campo <b>Telefone</b>!',
        'is_false'  => 'O Campo <b>Telefone</b> não é valido!'
    ]
];
$data = filter_input_array(INPUT_POST, $filter_rules);
foreach ($data as $field => $value){
    if(empty($validation[$field])){
        continue;
    }
    if($value == null or $value == ''){
        echo json_encode(["status"=>false,"msg"=> $validation[$field]['is_null'] ]);exit;
    } elseif($value === false){
        echo json_encode(["status"=>false,"msg"=> $validation[$field]['is_false'] ]);exit;
    }

}
$email_sender = '[email protected]';
$subject = 'Subject';
// Message
$message = '
    <html>
        <head>
          <title>Solicitação do cliente</title>
        </head>
        <body>
          <p><b>Nome:</b>'.$data['name'] .'</p>
          <p><b>E-mail:</b>'.$data['email'] .'</p>
          <p><b>Telefone:</b>'.$data['phone'] .'</p>
          <p><b>Site:</b>'.$data['site'] .'</p>
        </body>
    </html>
    ';
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.1' ."\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' ."\r\n";
// Additional headers
$headers .= 'From: '. $email_sender ."\r\n";
$headers .= 'Return-Path: '. $email_sender ."\r\n";
$headers .= 'Reply-To: '. $data['email'] ."\r\n";
// Mail it
if(mail('[email protected]', $subject, $message, $headers, "-f$email_sender")){
    echo json_encode(["status"=>true, "msg"=>"Mensagem enviada com <b>sucesso</b>!"]);exit;
}else {
    echo json_encode(["status"=>false, "msg"=>"Desculpe ocorreu um <b>erro</b>!"]);exit;
}
  • In addition, check that the mail() function is not disabled on your server and that the SMTP and SMTP_PORT server in php.ini is configured correctly in the [mail Function] session. It’s already made a difference to me!

  • Well remembered! vlw man

  • Mto thanks for answering but in the end I ended up using the phpmailer library that I found easier

  • Blz, brother. vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.