-2

Is it necessary to have a mailid in which site am using the code? It is showing mail sent but it doesn't send anything to the mailid mentioned in the code. and I have a doubt from which emailid, mail will be sent? For that have I to create an id on my domain?

$msg=" ".$a." ".$b." ".$c." ".$d." ".$e;
$to="email@example.com";
$sub=$a;
$msg = wordwrap($msg, 70);
#echo $msg;

$r=mail($to,$sub,$msg);

     if( $r== true ) {
echo"<script>alert('mailsent');</script>";
     }else {
        echo"<script>alert('failure');</script>";
     }

The code I have used:

the code i hav used

Blue
  • 22,608
  • 7
  • 62
  • 92
  • What are you thinking an "emailid" is? This question doesn't make sense. – Jonnix Aug 12 '16 at 07:37
  • Welcome to Stack Overflow! [Please don't post your code as an image.](//meta.stackoverflow.com/q/285551) – Blue Aug 12 '16 at 07:38
  • Possible duplicate of [PHP mail form doesn't complete sending e-mail](http://stackoverflow.com/questions/24644436/php-mail-form-doesnt-complete-sending-e-mail) – Blue Aug 12 '16 at 07:40
  • Do you check spam folder?
    Do you have correct email?
    Do you configure DKIP on your domain and server?
    and etc... Any way it's very complex task. Use SASS service for send emails like mailchimp
    – Michail M. Aug 12 '16 at 07:36

1 Answers1

1

using mail() to send emails is unreliable at best you don't know if the email is actually sent or not like you've discovered passing headers to mail increases the chances of it being delivered.

$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

if(mail($to, $sub, $msg, $headers)){
  echo "<p>Email Sent</p>";
} 

A better approach is to use phpmailer - https://github.com/PHPMailer/PHPMailer

Here's their sample of sending email

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

PHPMailer is very flexible and can be used with very little options, it can also be configured to use SMTP to then emails can be sent by authenticated accounts or even use third party email services like SendGrid to delivery and monitor your emails. https://sendgrid.com/

Dave
  • 878
  • 8
  • 19