cron 작업을 값 1 (전자 메일 전송)을 반환하지만 실제로 전자 메일을 보내지 않는 hostgator 서버에 설정했습니다. 브라우저에서 PHP 파일을 수동으로 호출하면 전자 메일이 전송됩니다. cron을 통해 실행하는 경우에는 작동하지 않습니다.Cron 작업이 전자 메일을 보내지 않습니다.
이번 달 초 저는 SSL을 사용하고 전용 IP 주소를 얻을 수 있도록 hostgator의 한 서버에서 다른 서버 (hostgator)로 사이트를 옮겼습니다. 사이트 이동 후 cron 작업은 전자 메일을 보내는 부분에 대해 을 제외한 을 작동합니다 (예 : 데이터베이스 기능이 정상적으로 작동 함). 나는 hostgator 기술 지원부에 연락했지만 문제는 제 코드에 있다고 생각합니다.
내 서버 정보가 올바르지 않다고 생각하여 smtp.gmail.com을 사용하고 Gmail 계정을 사용하여 메일을 보내도록 전환했으나 작동하지 않았습니다. 도와주세요!
은 cron 작업은 다음과 같이 설정 :
가 여기에 sendmailcron.php의 :
이* 7 * * * /opt/php56/bin/php /home/user/public_html/somefolder/sendmailcron.php
(*/2 * * * * 테스트 동안, 나는이 2 분마다 실행되도록 변경) 스크립트 :
<?php
$now = date("Y-m-d H:i:s");
$msgcontent = [];
$msgcontent['email'] = "[email protected]";
$msgcontent['name'] = "Recipient Name";
$msgcontent['textpart'] = "This is the text version of the email body.";
$msgcontent['htmlpart'] = "<p>This is the <strong>HTML</strong> version of the email body.</p>";
$msgcontent['subject'] = "Test email sent at " . $now;
$result = sendMyMail($msgcontent, "HTML");
exit();
function sendMyMail($msgcontent, $format="HTML") {
require_once '/home/user/public_html/somefolder/swiftmailer/lib/swift_required.php';
$result = 0;
$subject = $msgcontent['subject'];
$email = $msgcontent['email'];
if (strlen($email) == 0) {
return 0;
}
$name = $msgcontent['name'];
$emailbody = $msgcontent['textpart'];
$emailpart = $msgcontent['htmlpart'];
switch($format) {
case "TEXT":
$msgformat = 'text/plain';
break;
case "HTML":
$msgformat = 'text/html; charset=UTF-8';
break;
default:
$msgformat = 'text/html';
break;
}
$adminemailaddress = "[email protected]";
$adminemailpwd = 'myadminpwd';
$sendername = 'My Real Name';
$transport = Swift_SmtpTransport::newInstance('mygator1234.hostgator.com', 465, "ssl")
->setUsername($adminemailaddress)
->setPassword($adminemailpwd)
;
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
if($format == "TEXT") {
$message = Swift_Message::newInstance($subject)
->setFrom(array($adminemailaddress => $sendername))
->setTo(array($email => $name))
->setBody($emailbody)
->setContentType($msgformat)
;
}
else {
$message = Swift_Message::newInstance($subject)
->setFrom(array($adminemailaddress => $sendername))
->setTo(array($email => $name))
->setBody($emailpart)
->setContentType($msgformat)
;
}
//This is where we send the email
try {
$result = $mailer->send($message); //returns the number of messages sent
} catch(\Swift_TransportException $e){
$response = $e->getMessage() ;
}
return $result; //will be 1 if success or 0 if fail
}
?>
반환 값은 항상 1이지만 전자 메일은 보내지 않습니다.
의견을 보내 주시면 대단히 감사하겠습니다.
메일 로그 파일을 확인 했습니까? 명령 줄에서 스크립트를 실행하면 어떻게됩니까? – nogad
브라우저에서 실행하면 메일이 전송됩니다. PuTTY 쉘 또는 cron 작업에서 실행할 때 전자 우편은 보내지지 않습니다. – rottlover
해결 방법 : SMTP 서버의 스팸 필터가 전자 메일에 장애가 발생했습니다. hostgator 기술 지원을받은 후 2.5 시간이 지나면 문제가 해결 된 것으로 보입니다. 나는 그것이 코딩 문제가 아니기 때문에 기쁘다. ;) – rottlover