How to use own SMTP servers PHP mail - php

I am using default PHP mail() function to send emails as below . I have two email servers. How can I set my code to use these two mail server? I am running my PHP code on Linux.
mail($currEmail, $HOT_EMAIL_SUBJECT, $body, 'From: '.$pollsConfig_senderEmail);

Use PEAR::Mail, you can specify a SMTP server with it.
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$obj = Mail::factory ('smtp',
array ('host' => $host,
'port' => $port));
$obj->send ($to, $headers, $body);

Related

Pear PHP Mail sending CC doesn't seem to work

I'm using PEAR's mail library via SMTP on my server and whilst I can get emails to generate, adding CC's doesn't seem to work. Basically the CC recipients never get their email even though the main recipient of the same email does.
My basic setup is as follows, all the recipient variables ($to,$cc,$bcc) are string variables containing either a single recipient email addresses or comma separated email addresses.
$headers = array (
'From' => $from,
'To' => $to,
'Cc' => $cc,
'Bcc' => $bcc,
'Subject' => $subject,
'Reply-To' => $from,
'X-Mailer' => 'PHP/' . phpversion(),
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=ISO-8859-1'
);
$smtp = Mail::factory('smtp', array (
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password
));
$result = $smtp->send($to, $headers, $message);
I've read that sending BCCs is more complex, so lets stick to the CCs... why aren't they being received ? Is there anything obvious that I'm doing wrong here ?
In order to send an email to CC or BCC with SMTP, you must list all email addresses both under recipients to the send() function and in the CC key within the header.
$to = "john#example.com";
$cc = "doe#example.com";
$recipients = $to . ", " . $cc;
$headers["From"] = "john#example.com";
$headers["To"] = $to;
$headers["Subject"] = "Hello World!";
$headers["Cc"] = "doe#example.com";
$headers["Reply-To"] = "john#example.com";
$send = $mail->send($recipients, $headers, $body);

Pear Php smtp relay server issue hosted iat GoDaddy

I have an HTML form for a website hosted on GoDaddy, I'm trying to allow users to use a contact form to contact me via my website, I've created an smtp PHP script however it doesn't seem to work, the script runs and the page reloads but I haven't received any income mail for this test.
<?php
include('Mail.php');
$host = 'localhost';
$username = '******#***********.com';
$password = '*******';
$subject = 'Test';
$from = '******#*********.com';
$to = '******.*******#***.com';
$cc = 'person to CC';
$recipients = $to . ", " . $cc; //
$headers = array ('From' => $from,
'To' => $to,
'Cc' => $cc,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => false,
'username' => $username,
'password' => $password,
'port' => '25'
));
$mail = $smtp->send($recipients, $headers, $body);
if ( PEAR::isError($mail) ) {
echo("<p>Error sending mail:<br/>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message sent.</p>");
}
header("Location:contact-us.html");
?>

Set time or sending limit for PHP mailing script

I have a simple php registration form that captures a name and email address. After registering, I have a confirmation email set to go to the registrant and one to go to the admin.
I've had an issue where the user only registered once, but some issue caused the email to be sent 50+ times to both the registrant and admin. It's only happened a few times, but I'm trying to prevent it from happening in the future.
I'm thinking of a time limit on running the script, but wondering the best way to handle this. Below is a snippet of the mailing portion of the script.
2 notes, in case they are of use:
I'm using PEAR Mailer
My mail is being sent via MailGun
$headers = array ('From' => $from,
'To' => $to,
'Bcc' => $bcc,
'Reply-to' => $email,
'Subject' => $subject,
'MIME-Version' => 1,
'Content-type' => 'text/html;charset=iso-8859-1');
$headers_admin_notification = array ('From' => $from_admin_notification,
'To' => $to_admin_notification,
'Subject' => $subject_admin_notification,
'MIME-Version' => 1,
'Content-type' => 'text/html;charset=iso-8859-1');
// credentials for SMTP Authentication included from config file
$smtp = Mail::factory('smtp', array ('host' => 'ssl://smtp.mailgun.org',
'auth' => true,
'port' => 465,
'username' => 'XXXXX',
'password' => 'XXXXX'));
$mail = $smtp->send($to, $headers, $body);
$mail_admin_notification = $smtp->send($to_admin_notification, $headers_admin_notification, $body_admin_notification);
if (PEAR::isError($mail) || PEAR::isError($mail_admin_notification)) {
echo("<p>" . $mail->getMessage() . "</p>");
echo("<p>" . $mail_admin_notification->getMessage() . "</p>");
}
else {
header("Location: LINK-TO-MY-CONFIRMATION-PAGE?fname=$fname&session=$readable_date_and_time");
}
Thanks!

Pear PHP Mailer Emails Sender and Recipient

I'm using Pear PHP Mailer to send an email through SMTP to a number of recipients, but for each one, it sends an additional email to the sender's email address as well.
This is my code: Any help is appreciated.
...
$from = "$from_email_name <$from_email_addr>";
$html_body = "$html";
$crlf = "\r\n";
$hdrs = array(
'From' => $from,
'Subject' => $subject
);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setTXTBody($text_body);
$mime->setHTMLBody($html_body);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail = Mail::factory('mail',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail->send($to_email_addr, $hdrs, $body);
....
Also, on the email it is sending to my sender address, its saying the correct recipient name and sender information, but its like the sender is getting a copy of each one it sends out.
It could be a "service" of your mail server provider to automatically put mails sent via the SMTP server in your IMAP/POP mail box. Ask him if that's the case.

PHP Email Sending Error

I am trying to edit PHP code that sends an email using PEAR. The below code has worked on the company server, but doesn't seem to work on my computer when I am using localhost.
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
//Send the Actual Mail
$smtp = Mail::factory('smtp',
array ( 'host' => $host,
'auth' => false,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail))
{
echo($mail->getMessage());
}
else
{
echo("<p>Message successfully sent!<p>");
}
The items in $headers are defined earlier, don't worry.
The error returned by echo($mail->getMessage()); is as follows:
Failed to connect to localhost:25 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]
I'm on a machine running Ubuntu. Not sure if any other information is needed.

Categories