News:

Wondering if this will always be free?  See why free is better.

Main Menu

How can I send sms via php's mail function()?

Started by irfanaspl, March 11, 2022, 06:42:33 AM

Previous topic - Next topic

irfanaspl

A few days ago, I created an alert system that sends me a mail whenever certain APIs are called (or others).

The system works perfectly on email. It is received in the mailbox where it is sent once the function returns true.

For some reason when sending to an SMS gateway (i.e. [email protected]) the function sends it (it does return true) however it never gets received on the other end.

I can send the same exact email address (that goes to the SMS gatekeeper) through an email client (like Zimbra or whatever) and it will pass through fine.

Honestly, I'm more of a web designer than an e-mail expert. My understanding is that it has to do with headers or something along those lines, but I have no knowledge of this technical subject.

In speaking with my IT guy, he said that it looks like the "From" address in the headers is incorrect. Rather than coming from a valid email address, the email looks like it is coming from "www.data@[server name]" rather than what I am sending via the header, which is a valid email address.

Here is my PHP code:

<?php

$carriers = array("@messaging.sprintpcs.com");

//get to email
if (isset($_POST['to'])) {
$to = $_POST['to'];
}

// get from email
if (isset($_POST['from'])) {
$from = $_POST['from'];
$fromHeader = "From: ".$from."\r\n Sender: ".$from."\r\n";
}

// get subject
if (isset($_POST['subject'])) {
$subject = $_POST['subject'];
}

// get message
if (isset($_POST['message'])) {
$message = $_POST['message'];
}

// get cc
if (isset($_POST['cc'])) {
    if ($_POST['cc']!="") {
    $ccHeader = "CC: ".$_POST['cc']."\r\n";
    }
    else {
    $ccHeader="";
    }
}
else {
$ccHeader="";
}

// get bcc
if (isset($_POST['bcc'])) {
    if ($_POST['bcc']!="") {
    $bccHeader = "Bcc: ".$_POST['bcc']."\r\n";
    }
    else {
    $bccHeader="";
    }
}
else {
$bccHeader="";
}

// get reply to
if (isset($_POST['replyTo'])) {
    if ($_POST['replyTo']!="") {
    $replyToHeader = "Reply-To: ".$_POST['replyTo']."\r\n";
    }
    else {
    $replyToHeader="";
    }
}
else {
$replyToHeader="";
}

$additionalHeaders = "Content-Type: text/plain; charset=\"utf-8\" Content-Transfer-Encoding: 8bit \r\n";

$headers = $fromHeader.$ccHeader.$bccHeader.$additionalHeaders;

foreach ($carriers as $carrier) {

    $number = get_numeric_only($to).$carrier;

    if (mail($to,$subject,$message,$headers)) {
        $response = array("response" => "SUCCESS");
    }
    else {
        $response = array("response" => "ERROR");
    }

}

echo json_encode($response);

?>

Aleksi "Lex" Kilpinen

#1
You may have a sender address defined for PHP in php.ini that you are unable to override.
Also sending mail from any address that is not actually hosted on the same server may quite often get you flagged as spam, it is basically always better to use SMTP if it's an option. More on PHP Mail: https://www.php.net/manual/en/function.mail.php
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

Advertisement: