Topic: How to send email via php scripts?
- iRedMail version: 1.6.2
- Deployed with the downloadable installer
- Linux/BSD distribution name and version: Ubuntu 22
- Store mail accounts in which backend (LDAP/MySQL/PGSQL): MySQL
- Web server (Apache or Nginx):Nginx
- Manage mail accounts with iRedAdmin-Pro? No
- [IMPORTANT] Related original log or error message is required if you're experiencing an issue.
Hello,
After installed iRedMail successfully and emails can be sent using Roundcubemail, I am trying to send an email using php scripts and it fails. Is there any example for using php to send email using iRedMail? Thank you!
What I did after install iRedMail are as follow:
(1)installed phpmailer, using PHP to send an email, and it shows SMTP not connected. The mail.log shows SSL problem. Then I installed the new SSL successfully (tested ok by ssllabs , but the SMTP not connected problem remains the same.
php code:
require './PHPMailer-master/src/Exception.php';
require './PHPMailer-master/src/PHPMailer.php';
require './PHPMailer-master/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail=new PHPMailer();
echo 'begin to send mail to '.$email.'<br>';
$body="<h1>test</h1>";
$mail->IsSMTP();
$mail->SMTPAuth=true;
$mail->SMTPKeepAlive=true;
$mail->Host="mx.xxx.com";
$mail->Port = "587";//telnet mx.xxx.com 587 works
$mail->SMTPSecure = "tls";
$mail->Username=$senderEmail;
$mail->Password=$senderEmailPasswd;
$mail->From=$senderEmail;
$mail->FromName=$senderName;
$mail->Subject=$senderName." email test";
$mail->AltBody=$body;
$mail->WordWrap=50; // set word wrap
$mail->MsgHTML($body);
$mail->AddReplyTo($senderEmail,$senderName);
$mail->AddAddress($email,$emailName);
$mail->IsHTML(true);
if(!$mail->Send()){
echo "Mailer Error:".$mail->ErrorInfo;
}else{
echo "Message has been sent";
}
(2)since the phpmailer doesn't work properly, I tried to trace roundcubemail and mimic the sendding email scripts since the roundcubemail works. But I couldn't found any code examples and makes it hard to mimic the code.
Is there any example using php scripts to send email after iRedMail installation? Or any advice are welcomed. Thank you!
----
Spider Email Archiver: On-Premises, lightweight email archiving software developed by iRedMail team. Supports Amazon S3 compatible storage and custom branding.