There are two popular protocols to send Email: SMTP and ESMTP. PHPoC supports libraries for both prototol. Your mail server might use one of them. For example, Gmail uses ESMTP protocol. Therefore, to send email from a Gmail account, it needs to use ESMTP library.
This example shows how to send email using ESMTP protocol.For sending email using SMTP protocol, refer to Sending email (SMTP).
Hardware
- PHPoC Blue (+ USB WLAN) or PHPoC Black (+ Ethernet cable)
- Micro USB to USB Cable (to upload source code to PHPoC Device)
Quick Steps
Source code of this example is a part of PHPoC Support Packet (PSP). You need to:
- Download PHPoC Support Package.
- Upload example\net\01.php_task\email_msa to PHPoC Blue/Black.
- Modify the e-mail address and password in task0.php. For example, your email address is [email protected] and password is example_password
- Save the modified file.
- Configure network parameters (e.g. WiFi SSID, password, IP address ...).
Source Code
Source files includes:
- init.php: this file is run when PHPoC system is powered or reset. It is used to specify which file is run is system loop.
- task0.php: this file is run in system loop of PHPoC devices.
init.php
This file is run when PHPoC system is powered or reset. It is used to specify that task0.php is run is system loop.
PHP Code:
<?php
system("php task0.php");
?>
task0.php
[Full Code]
PHP Code:
<?php
if(_SERVER("REQUEST_METHOD"))
exit; // avoid php execution via http request
include_once "/lib/sn_dns.php";
include_once "/lib/sn_esmtp.php";
echo "PHPoC example : send email via out-going mail server\r\n";
//esmtp_setup(udp_id, tcp_id, "x.x.x.x");
//esmtp_hostname("from_domain.com");
esmtp_account("[email protected]_domain.com", "from_name");
esmtp_auth("msa_id", "msa_password");
esmtp_msa("smtp.gmail.com", 465);
//esmtp_msa("smtp.naver.com", 465);
//esmtp_msa("smtp.daum.net", 465);
$subject = "msa test";
$message = "Hi PHPoC\r\nThis is PHPoC msa test email\r\nGood bye\r\n";
$msg = esmtp_send("[email protected]_domain.com", "to_name", $subject, $message);
if($msg == "221")
echo "send mail successful\r\n";
else
echo "send mail failed\r\n";
?>
[Explanation]
Source code of this file does:
- Set account information and does the authentication.
- Creates subject and message to send.
- Sending email.
- Prints the sending result.
Test and Result
- Click "Run" button on PHPoC Debugger.
- See Result on PHPoC Debugger's Console.
- If successful, check your email box.
See Also
- DNS lookup
- Sending email (SMTP)
- MySQL - Inserting Data to Remote Server
- MySQL - Updating Data to Remote Server
- TCP Server - Single TCP Connection
- TCP Server - Multiple TCP Connections
- TCP Client - HTTP GET
Other Resources