Public
Script
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

πŸ’¬ Val Town Email-to-SMS

Usage

import { sendSMS } from 'https://esm.town/v/iamseeley/sendSMS'; sendSMS(phoneNumber: string, message: string, carrier: string): Promise<void>

Parameters

  • phoneNumber: The recipient's phone number (string of digits, no spaces or dashes)
  • message: The text message you want to send
  • carrier: The recipient's cell phone carrier.

Supported carriers: 'att' (AT&T), 'tmobile' (T-Mobile), 'verizon' (Verizon), 'sprint' (Sprint)

List of Email-To-SMS Addresses

Comment on this val if you'd like me to add a carrier from the above list!

Example

import { sendSMS } from 'https://esm.town/v/iamseeley/sendSMS'; sendSMS('1234567890', 'Hello from Val Town!', 'verizon');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { email, IAddress } from "https://esm.town/v/std/email";
const carriers: { [key: string]: string } = {
"att": "txt.att.net",
"tmobile": "tmomail.net",
"verizon": "vtext.com",
"sprint": "messaging.sprintpcs.com",
};
export async function sendSMS(phoneNumber: string, message: string, carrier: string): Promise<void> {
const carrierDomain = carriers[carrier.toLowerCase()];
if (!carrierDomain) {
throw new Error("Unsupported carrier");
}
const to: IAddress = { email: `${phoneNumber}@${carrierDomain}` };
try {
console.log(`Attempting to send SMS to: ${to.email}`);
const result = await email({
to: to,
subject: "SMS",
text: message,
});
console.log("Email function returned:", result);
console.log("SMS sent successfully");
} catch (error) {
console.error("Error sending SMS:", error);
if (error instanceof Error) {
console.error("Error name:", error.name);
console.error("Error message:", error.message);
console.error("Error stack:", error.stack);
}
}
}
August 25, 2024