I am writing a simple PHP script that will retrieve feedback entries from a MySQL database and send the information to an email address. I am using mail(), but I suspect that this is too simple for what I need to do.
<?php
function setupMailServerConnection() {
try {
ini_set('SMTP', SMTP_HOST);
ini_set('smtp_port', SMTP_PORT);
ini_set('sendmail_from', FROMEMAIL);
} catch (Exception $e) {
$msg = ERROR_MESSAGE . ' setupMailServerConnection() ' . date('Y-m-d H:i:s') . ' ' . $e->getMessage();
toLogDB($msg);
throw $e;
}
}
function generateBodyHeader($json) {
return 'From: ' .
$json->first_name .
' ' .
$json->last_name .
'<' .
$json->email .
'>\\r\\n\\r\\n';
}
function doMail($jsonArray) {
$result = '0';
$headers = '';
$json;
setupMailServerConnection();
try {
foreach ($jsonStrArray as $row) {
$json = json_decode($row);
$headers = 'From: ' . FROMEMAIL;
$msg = str_replace('\\n.', '\\n..', $json->question);
if (!mail(TO_EMAIL, $json->email_subject, generateBodyHeader($json) . wordwrap($msg, 70), $headers)) {
$result = '-1';
break;
}
}
} catch (Exception $e) {
$msg = ERROR_MESSAGE . ' doMail() ' . date('Y-m-d H:i:s') . ' ' . $e->getMessage();
toLogDB($msg);
$result = '-1';
} finally {
return $result;
}
}
Unfortunately, this constantly throws a 554 error "Email rejected". I know my values are correct, because I have the exact same functionality in an off-site Java application that successfully works every time, however, it allows for the sending of the email server username and password, both of which I have, but I don't know how to send them using mail() or how to set them in setupMailServerConnection() using ini_set().
So, basically, I want the equivalent of this Java line in PHP:
private void doMail()
throws AuthenticationFailedException, AddressException, MessagingException,
SQLException, UnsupportedEncodingException, Exception {
String name = "";
StringBuilder sb = new StringBuilder();
MimeMessage message = new MimeMessage(this.session);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(FeedbackMailer.toEmail));
for (FeedbackBean bean : this) {
sb.append(StringUtilities.cleanXSS(StringUtilities.stripHTML(bean.getFirstName())))
.append(" ")
.append(StringUtilities.cleanXSS(StringUtilities.stripHTML(bean.getLastName())));
name = sb.toString();
sb.delete(0, sb.length());
message.setFrom(new InternetAddress(FeedbackMailer.dummyFromEmail, name));
message.setSubject(StringUtilities.cleanXSS(StringUtilities.stripHTML(bean.getEmailSubject())));
sb.append("From: ")
.append(name)
.append(" <")
.append(StringUtilities.cleanXSS(StringUtilities.stripHTML(bean.getEmail())))
.append(">")
.append(StringUtilities.NEW_LINE)
.append(StringUtilities.NEW_LINE)
.append(StringUtilities.cleanXSS(StringUtilities.stripHTML(bean.getQuestion())));
message.setText(sb.toString());
Transport.send(message, FeedbackMailer.user, FeedbackMailer.password);
sb.delete(0, sb.length());
}
}
Any help appreciated.
Thanks