Jibran
Jibran

Reputation: 99

PHP code to send Email with Smtp

I am new to PHP and trying to send an email with PHP script.

I did change "php.ini" settings to SMTP = "in.mailjet.com" smtp_port= 25 auth_username="abc" auth_password="abc" sendmail_from = "[email protected]"

Simple HTML file to call php file is form name="contactform" method="post" action="PHPMailer/email.php"
style="padding-left:5%;"> <td colspan="2" style="text-align:center"> <br> <input class="btn btn-info" type="submit" value="Download">

email.php file

    require 'class.phpmailer.php';
    $mail = new PHPMailer(true);
    $mail->IsSMTP();
    $mail->Host="in.mailjet.com";
    $mail->SMTPAuth = true;
    $mail->Username = 'abc';
    $mail->Password = 'abc';
    $mail->SetFrom('abc','Your Name');
    $mail->AddAddress('[email protected]','Their Name');
    $mail->Subject = 'Your email from PHPMailer and Mailjet';
    $mail->MsgHTML('This is your email message.");
    $mail->Send();

I am trying to send email from IIS server(test server), but I am not been able to send please help me out or let me know what I am missing.

Thank You

Upvotes: 1

Views: 2022

Answers (1)

Dhaval Bharadva
Dhaval Bharadva

Reputation: 3083

Try below code:

require 'class.phpmailer.php';

$mail = new PHPMailer();

$mail->isSMTP(); // send via SMTP

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
//$mail->SMTPDebug = 2;

$mail->Host = $host; //SMTP server
$mail->Port = $port; //set the SMTP port; 587 for TLS
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail

if (strlen($username)) {
    $mail->SMTPAuth = true;
    $mail->Username = $username;
    $mail->Password = $password;
} else {
    $mail->SMTPAuth = false;
}

$mail->From = $from; // FROM EMAIL
$mail->FromName = $fromName; // FROM NAME
$mail->addAddress($to);
$mail->addReplyTo($from);

$mail->IsHTML($html);

$mail->Subject = $subject; // YOUR SUBJECT
$mail->Body = $message; // YOUR BODY
$mail->addAttachment($attachment); // YOUR ATTACHMENT FILE PATH

if ($mail->send()) {
    echo "Message Sent";exit;
} else {
    echo "Message Not Sent<br>";
    echo "Mailer Error: " . $mail->ErrorInfo;exit;
}  

Note: Replace all variables with yours appropriately

Upvotes: 1

Related Questions