Tony Stark Tony Stark - 1 year ago 129
PHP Question

How to add email servern & port into this PHP

I have a PHP code at my server in which if data is added to the database, it sends a mail to the admin. Recently I bought a webhosting and uploaded my files. Problem is my mails from the server were getting delayed so I asked the webhosting. They said me to add this "relay-hosting.secureserver.net" and port 25 in my PHP to receive the mail fast. I don't have much knowledge of PHP. Can you help me where to insert this in the code below:

<?php

// get data
$name = $_POST['name'];
$alamat = $_POST['alamat'];
$kota = $_POST['kota'];
$provinsi = $_POST['provinsi'];
$name2 = $_POST['name2'];
$date_n_time = $_POST['date_n_time'];
$phone = $_POST['phone'];
$order_list = $_POST['order_list'];
$comment = $_POST['comment'];
$emails = $_POST['email'];

$sql_query = "set names 'utf8'";
$stmt = $connect->stmt_init();
if($stmt->prepare($sql_query)) {
// Execute query
$stmt->execute();
// store result
$stmt->close();
}

// insert data into reservation table
$sql_query = "INSERT INTO tbl_reservation(Name, Alamat, Kota, Provinsi, Number_of_people, Date_n_Time, Phone_number, Order_list, Comment, Email)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

$stmt = $connect->stmt_init();
if($stmt->prepare($sql_query)) {
// Bind your variables to replace the ?s
$stmt->bind_param('ssssssssss',
$name,
$alamat,
$kota,
$provinsi,
$name2,
$date_n_time,
$phone,
$order_list,
$comment,
$emails
);
// Execute query
$stmt->execute();
$result = $stmt->affected_rows;
// store result
//$result = $stmt->store_result();
$stmt->close();
}

// get admin email from user table
$sql_query = "SELECT Email
FROM tbl_user";

$stmt = $connect->stmt_init();
if($stmt->prepare($sql_query)) {
// Execute query
$stmt->execute();
// store result
$stmt->store_result();
$stmt->bind_result($email);
$stmt->fetch();
$stmt->close();
}

// if new reservation has been successfully added to reservation table
// send notification to admin via email
if($result){
$to = $emails;
$subject = $reservation_subject_2;
$message .= "Dear " .$provinsi."\r\n";
$message .= $reservation_message_2."\r\n\n";
$message .= "Pick Drop :" .$name2."\r\n";

$from = $admin_email;
$headers = "From:" . $from."\r\n".
mail($to,$subject,$message,$headers);
echo "OK";
}else{
echo "Failed";
}?>

Answer Source

Your code is using PHP's built in mail() command to send the message - you can see that line almost at the end of your code:

mail($to,$subject,$message,$headers);

The server and port that mail() uses are configured in PHP's configuration file, php.ini. You may be able to override those settings by adding this to your code - anywhere above the mail() line.

ini_set('SMTP', 'relay-hosting.secureserver.net');
ini_set('smtp_port', 25);

If you want to use that server/port always, from other PHP code, you'll either need to make the same change in the other files that send mail, or update the main php.ini configuration file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download